Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runapp shiny from other shiny application with actionbutton

Tags:

r

shiny

I am trying to call another shinny app when i push an actionbutton in a very simple shiny app. The other application is in a folder called benefits with ui.R and server.R files, but when i click the button nothing happens. Is it possible what i am trying to do??

Cheers.

ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("RunnApp"),
    mainPanel(
      actionButton("goButton", "Go!")
    )

))

server.R

library(shiny)

shinyServer(function(input, output) {
    ntext <- eventReactive(input$goButton, {
      runApp("benefits")
  })
            })
like image 930
mahdsip Avatar asked Nov 08 '15 10:11

mahdsip


1 Answers

There is no direct way to launch a shiny app from within another shiny app. Calling runApp() inside a shiny app will result in this error,

Warning: Error in shiny::runApp: Can't call `runApp()` from within `runApp()`. If your application code contains `runApp()`, please remove it.

But, with RStudio 1.2 there is a workaround. We can store runApp() of second app in an R script and execute this script as a separate R Studio Job. This will start the second shiny app in a new session without stopping the first one.

Code:

script.R

shiny::runApp(path_to_app, launch.browser = TRUE, port = 875)

ui.R

actionButton("launch_app", "Launch second Shiny App")

server.R

observeEvent(input$launch_app, {
        rstudioapi::jobRunScript(path = path_to_script)
    })

If this is for a package, store the script in inst/ and use system.file() to build paths.

like image 172
Thiloshon Nagarajah Avatar answered Oct 11 '22 15:10

Thiloshon Nagarajah