TechBuzzPro

How to Run Shiny App in RStudio

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can run a Shiny app directly in RStudio by using the built-in Run App button or the shiny::runApp() function. This launches a local web server that displays your app in RStudio’s viewer pane, a separate window, or your default browser, allowing you to test and interact with your interactive web application during development.

What You'll Need

  • R installed on your system
  • RStudio (Desktop or Posit Cloud)
  • The shiny R package installed (install.packages("shiny"))
  • A Shiny app consisting of at least one R script: either app.R or both ui.R and server.R

Step-by-Step

  1. Open RStudio and create a new project or navigate to an existing directory containing your Shiny app files. Ensure the app has either an app.R file or separate ui.R and server.R files in the same folder.
  2. Create or open your Shiny app file. If using a single file, name it app.R. This file must contain both the ui (user interface) and server (logic) components, and end with a call to shinyApp(ui, server).
  3. Verify that the shiny package is loaded at the top of your script with library(shiny). Without it, functions like fluidPage() or actionButton() will not be recognized.
  4. Save your app.R file (or ui.R and server.R) to ensure all changes are applied before running.
  5. Click the Run App button located at the top of the R script editor pane in RStudio. This button appears only when editing a file that defines a Shiny app (e.g., named app.R or containing shinyApp()).
  6. When prompted, choose where to display the app: Viewer Pane, Window, or Browser. The Viewer Pane is ideal for quick testing, while Browser allows full interaction like a real web app.
  7. Observe the app launch. A local server starts on a random port (e.g., http://127.0.0.1:4321) and your app loads. You can interact with inputs and see outputs update in real time.
  8. To stop the app, click the Stop button (square icon) in the R console or viewer tab. Do not close RStudio abruptly, as this may leave background processes running.
  9. To run the app programmatically instead of using the button, place your cursor in the script and execute shiny::runApp() in the console. This is useful for debugging or passing arguments like shiny::runApp(launch.browser = TRUE).
  10. For apps in a subdirectory, use shiny::runApp("path/to/app"), where the path points to the folder containing app.R or the ui.R/server.R pair.

Common Mistakes

  • Forgetting to include library(shiny) at the top of the script, causing undefined function errors.
  • Misnaming files: using App.R (capitalized) instead of app.R, which prevents RStudio from recognizing it as a Shiny app.
  • Missing the final shinyApp(ui, server) call, resulting in no app launch despite no visible errors.
  • Placing ui.R and server.R in different directories, which breaks the expected project structure.
  • Running runApp() from outside the app directory, leading to file not found errors.

Tips

  • Use app.R for simpler apps; reserve ui.R and server.R for larger projects requiring modularity.
  • Enable Run in Viewer Pane for rapid iteration during development, then switch to Browser for final testing.
  • Add options(shiny.port = 8888, shiny.host = "0.0.0.0") to set a fixed port or allow external access (use cautiously on secure networks).
  • Use shiny::runApp(launch.browser = TRUE) to always open in the default browser, useful for presentations.
  • Include error checking with tryCatch() around reactive expressions to prevent app crashes during user input.

Troubleshooting

If the app fails to launch:
  • Check the R console for error messages. Common issues include missing commas in UI definitions, undefined objects in server logic, or syntax errors.
  • Ensure the working directory is set to the app’s folder. Use getwd() and setwd("path/to/app") if needed.
  • Restart RStudio if the Run App button is unresponsive. Sometimes background R sessions interfere.
  • Reinstall the shiny package if functions are not found: remove.packages("shiny") then install.packages("shiny").
  • Verify file encoding: save scripts in UTF-8 format to avoid parsing issues with special characters.
  • Test with the minimal example: create a new app.R with just a title and a button to isolate environment issues.

Frequently Asked Questions

Can I run a Shiny app without RStudio?

Yes. Use R -e "shiny::runApp('path/to/app')" from the command line. RStudio is not required, but it simplifies development.

Why doesn’t the Run App button appear?

RStudio shows the button only for files named app.R or those containing shinyApp(). Rename your file or ensure the function call exists.

How do I share my Shiny app with others?

Deploy it using shinyapps.io, Shiny Server, or Posit Connect. Running locally only allows access on your machine.

Can I use multiple tabs or pages in one app?

Yes. Use navbarPage(), tabsetPanel(), or sidebarLayout() to organize content across tabs or panes within a single app.

What if my app freezes or becomes unresponsive?

Long-running computations can block the app. Use future and promises packages to run tasks asynchronously and keep the interface responsive.

Do I need internet to run a Shiny app locally?

No. Local Shiny apps run entirely on your machine. Internet is only needed to install packages or deploy online.