Shiny Tip: Choose Where to Run App with an Option

By Garrick Aden-Buie in Blog

October 13, 2019

Posted on:
October 13, 2019
2 minute read, 216 words
R Shiny Tips Tricks
See Also:
countdown v0.4.0 – Now on CRAN!
Process Profile Pictures with magick
xaringanExtra v0.6.0 — Now on CRAN!

When you’re running a Shiny app from a source file, like app.R in RStudio, you can choose to run the app in the Viewer Pane, a new Window, or in an External browser window.

RStudio 'Run App' drop down menu available for Shiny apps, for example in source files named 'app.R'

This works well for typical Shiny apps in app.R or {global,ui,server}.R, but if you’re building a Shiny app inside an R package — and if you are, then definitely check out ThinkR’s golem package — then that little Run App button won’t be available to choose where to run your Shiny apps.

The Shiny runApp() help documentation mentions the global option shiny.launch.browser but this helpful StackOverflow answer provided a helpful hint as to how to actually pick the Viewer, Window or External location for newly launched Shiny apps.

The following options only work in RStudio, and definitely in RStudio 1.2 (I’m running 1.2.1511). If you want to set these options globally in your ~/.Rprofile, then I’d recommend adding a conditional guard to check that RStudio is running first.

if (
  # Make sure that {rstudioapi} is available
  requireNamespace("rstudioapi", quietly = TRUE) &&
  # Returns TRUE if RStudio is running
) {
  options(shiny.launch.browser = your_choice_here)

RStudio shiny.launch.browser Options #

Run in Viewer #

options(shiny.launch.browser = .rs.invokeShinyPaneViewer)

Run in Window #

options(shiny.launch.browser = .rs.invokeShinyWindowViewer)

Run in External Browser #

options(shiny.launch.browser = .rs.invokeShinyWindowExternal)