如何组合两个闪亮的应用程序?

How can I combine two shiny apps?

我在 2 个不同的目录中有 2 个不同的 Shinnyapp。让我们假设 Shinyapp1 的 UI and server and Shinnyapp2's UI and server 是给定的。如何在不重写代码的情况下通过组合这 2 个不同的应用程序来创建 1 个 Shinyapp?或者您是否知道任何 GitHub 示例?非常感谢。

 library(shiny)

ui <- navbarPage("Combine 2 Shinyapps",
           tabPanel("Shinyapp1"),
           tabPanel("Shinyapp2")
)

server <- function(input, output, session) { 
}
shinyApp(ui, server)

这是一个使用 iframe 的示例(我已经在您的 duplicated earlier question 中提到过这种方法)。

在全局部分,我正在编写两个虚拟 app.R 文件,它们通过 callr::r_bg:

在单独的 R 进程中启动
library(shiny)
library(callr)

makeDir <- function(dir){
  if(!dir.exists(dir)){
    dir.create(dir, recursive = TRUE)
  }}

lapply(c("app1", "app2"), makeDir)

writeLines(text = 'ui <- fluidPage(p("App 1 content"), 
sliderInput("obs", "Number of observations:",
    min = 0, max = 1000, value = 500
  ))
server <- function(input, output, session) {}
shinyApp(ui, server)', con = "app1/app.R")

writeLines(text = 'ui <- fluidPage(p("App 2 content"),
sliderInput("obs", "Number of observations:",
    min = 0, max = 100, value = 50
  ))
server <- function(input, output, session) {}
shinyApp(ui, server)', con = "app2/app.R")

r_bg(function(){
  shiny::runApp(
  appDir = "app1",
  port = 3939,
  launch.browser = FALSE,
  host = "127.0.0.1"
)}, supervise = TRUE)

r_bg(function(){
  shiny::runApp(
  appDir = "app2",
  port = 4040,
  launch.browser = FALSE,
  host = "127.0.0.1"
)}, supervise = TRUE)


ui <- navbarPage("Combine 2 Shinyapps",
                 tabPanel("Shinyapp1", tags$iframe(src="http://127.0.0.1:3939/", height="900vh", width="100%", frameborder="0", scrolling="yes")),
                 tabPanel("Shinyapp2", tags$iframe(src="http://127.0.0.1:4040/", height="900vh", width="100%", frameborder="0", scrolling="yes"))
)

server <- function(input, output, session) {
  
}

mainApp <- shinyApp(ui, server)

runApp(mainApp)

另一种(更好的)方法 是将两个应用程序包装在单独的 modules 中,并使用它们自己的名称 space。这计算量较小(单个 R 进程而不是 3 个),但需要重新排列代码(OP 不希望这样做:without rewriting the code)。