rshinygetplumber

Is there a way to request a Shiny app via GET?


I'd like to execute a Shiny application via GET request, i.e. all scripts that are triggered when the Shiny app is initially called via a browser should be executed by the GET request. Is there a good way to handle this?

Could I solve this with httr package?

Example

library(httr)
GET("link_to_a_shiny_app")

Solution

  • This can be done via plumber (httr can be used on the client side).

    The following creates plumber API which runs a shiny app in a background R process and returns the according port of the shiny app once the port endpoint is accessed:

    library(plumber)
    library(shiny)
    library(httpuv)
    library(callr)
    
    #* @apiTitle Shiny runApp API
    #* @apiDescription runs a shiny app and returns the port.
    
    #* Echo back the input
    #* @param port The shiny app port
    #* @get /port
    function() {
      ui <- fluidPage(
        h1("This is a test app")
      )
      server <- function(input, output, session) {}
      app <- shinyApp(ui, server)
      port <- httpuv::randomPort()
      r_bg(function(app, port){
        shiny::runApp(appDir = app,
               port = port,
               launch.browser = FALSE,
               host = "0.0.0.0")
        }, args = list(app = app, port = port), supervise = TRUE)
    
      port
    }
    
    # plumb(file='plumber.R')$run()
    # http://127.0.0.1:6107/port
    

    Also see my answer here on how to handle http verbs directly with a shiny app.