How to Run Shiny App in RStudio
In this article
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
shinyR package installed (install.packages("shiny")) - A Shiny app consisting of at least one R script: either
app.Ror bothui.Randserver.R
Step-by-Step
- 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.Rfile or separateui.Randserver.Rfiles in the same folder. - Create or open your Shiny app file. If using a single file, name it
app.R. This file must contain both theui(user interface) andserver(logic) components, and end with a call toshinyApp(ui, server). - Verify that the
shinypackage is loaded at the top of your script withlibrary(shiny). Without it, functions likefluidPage()oractionButton()will not be recognized. - Save your
app.Rfile (orui.Randserver.R) to ensure all changes are applied before running. - 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.Ror containingshinyApp()). - 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.
- 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. - 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.
- 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 likeshiny::runApp(launch.browser = TRUE). - For apps in a subdirectory, use
shiny::runApp("path/to/app"), where the path points to the folder containingapp.Ror theui.R/server.Rpair.
Common Mistakes
- Forgetting to include
library(shiny)at the top of the script, causing undefined function errors. - Misnaming files: using
App.R(capitalized) instead ofapp.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.Randserver.Rin different directories, which breaks the expected project structure. - Running
runApp()from outside the app directory, leading to file not found errors.
Tips
- Use
app.Rfor simpler apps; reserveui.Randserver.Rfor 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()andsetwd("path/to/app")if needed. - Restart RStudio if the Run App button is unresponsive. Sometimes background R sessions interfere.
- Reinstall the
shinypackage if functions are not found:remove.packages("shiny")theninstall.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.Rwith just a title and a button to isolate environment issues.
Frequently Asked Questions
Can I run a Shiny app without RStudio?
Yes. UseR -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 namedapp.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. UsenavbarPage(), 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. Usefuture and promises packages to run tasks asynchronously and keep the interface responsive.