首页 > 解决方案 > 将多个 rshiny 应用程序合二为一

问题描述

我有几个闪亮的应用程序,我想将它们全部组合成一个应用程序。但是,我不想使用该 source()功能,而是想为每个闪亮的应用程序复制粘贴代码。现在,如果其他人运行此应用程序,则会出现错误。知道如何做到这一点吗?

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("first_app", tabName = "first_app"),
      menuItem("second_app", tabName = "second_app")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "first_app",
              h2(source("sir.shiny.R", local = TRUE)$value)
      ),
      tabItem(tabName = "second_app",
              h2(source("seir.shiny.R", local = TRUE)$value)
    )
  )
)
)
server <- function(input,output,server){}

shinyApp(ui,server)

标签: rshiny

解决方案


也许您正在寻找这个:在上面定义所有函数ui(或将它们放入global.R),并在服务器函数中绘制每个模型。侧边栏允许用户选择感兴趣的模型。见下文。

##  Define your own functions above ui or inside global.R
mySEIR <- function(t, y, parms) {
  with(as.list(c(y, parms)),{
    
    # Change in Susceptibles
    dS <- - beta * S * I  
    # Change in Exposed
    dE <- beta * S * I - sigma * E
    # Change in Infecteds
    dI <- sigma * E - gamma * I
    # Change in Recovereds
    dR <- gamma * I  
    
    return(list(c(dS, dE, dI, dR)))
  })
}

ui <- dashboardPage(
  dashboardHeader(title = div("Test SEIR app")),
  dashboardSidebar(
    sidebarMenu(id="tabs",
                menuItem("Home", tabName = "home", icon = icon("home")),
                menuItem("SIR", tabName = "first_app"),
                menuItem("SEIR", tabName = "second_app")
                )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "first_app", plotOutput("plotSIR")
      ),
      tabItem(tabName = "second_app", plotOutput("plotSEIR")
      )
    )
  )
)
server <- function(input, output, session){
  
  output$plotSIR <- renderPlot({qplot(rnorm(500),fill=I("red"),binwidth=0.2)})
  output$plotSEIR <- renderPlot({qplot(rnorm(500),fill=I("blue"),binwidth=0.2)})
  
}

shinyApp(ui,server)

推荐阅读