raistlin raistlin - 4 months ago 27
R Question

Calling Shiny function in Viewer

In RStudio, I want a Shiny function

hi.app
to be displayed in the Viewer. The default behaviour is to open the app in the browser.

hi.app <- function() {
require(shiny)
shinyApp(
ui = fluidPage(
textInput("name", "Write your name", value = "stranger"),
verbatimTextOutput("greeting")
),
server = function(input, output) {
output$greeting <- renderPrint({
greeting <- paste("Hi,", input$name)
greeting
})
}
)
}
hi.app()


Once I call
hi.app()
, the app is opened in the browser.

What I want: To define a parameter so that this particular app is always opened in RStudio's Viewer when I call it.

What I have tried : adding (without success) the following before
shinyApp
:

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

Answer

UPDATE: using shiny gadgets instead of shiny apps seems infallible for this if the shiny app is meant to be run locally. The parameter con be set via the viewer argument in runGadget(...):

hi_app <- function() {
  ui = miniPage(
    gadgetTitleBar("My Gadget"),
    textInput("name", "Write your name", value = "stranger"),
    verbatimTextOutput("greeting")
  )
  server = function(input, output) {
    output$greeting <- renderPrint({
      greeting <- paste("Hi,", input$name) 
      greeting
    })
    observeEvent(input$done, {
      stopApp()
    })
  }
  runGadget(ui, server,viewer = paneViewer())
}

say_hi <- function() {
  library(shiny)
  library(miniUI)
  hi_app()
}