【问题标题】:How can I combine two shiny apps?如何结合两个闪亮的应用程序?
【发布时间】:2021-11-27 07:43:40
【问题描述】:

我在 2 个不同的目录中有 2 个不同的 Shinnyapps。让我们假设 Shinyapp1 的 UI and server 和 Shinnyapp2 的 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)

【问题讨论】:

标签: r shiny shiny-server shinyapps


【解决方案1】:

这是一个使用 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 中,并使用它们自己的名称空间。这计算量较小(单个 R 进程而不是 3 个),但需要重新排列代码(OP 不希望这样做:without rewriting the code)。

【讨论】:

  • 我支持关于模块的评论,这些模块可以成为重用代码和制作多个应用程序/组合它们的好解决方案
猜你喜欢
  • 1970-01-01
  • 2017-07-27
  • 2015-10-13
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 2017-03-20
相关资源
最近更新 更多