我如何将闪亮的应用程序分解为模块



因此,在我以前的一个问题中,我遇到了如何还原书签和运行模型的问题。这只是一个可复制的示例,但是作为一个应用程序,我想将其模块化,因为应用程序的大小不断增加。我有以下代码。在模块1中,我想致电以渲染数据表并在用户单击书签时调用模块2。模块2的代码在服务器部分。我如何模块化此应用程序。

" 闪亮应用程序的用例,用户可以在其中输入一些值,然后单击运行时,它将运行模型并在表中显示值。现在,当我单击书签时,它会捕获输入值。当我单击还原书签时,它确实填充了输入值。我要做的是恢复输入值后,它也应该再次运行模型并填充表中的值。在简短的还原书签中应填充值然后单击运行按钮运行模型。"

library(shiny)
library(RSQLite)
library(data.table)
library(DT)
library(dplyr)
#### Module 1 renders the first table
opFunc <- function(input, output, session, modelRun,modelData,budget){
  output$x1 <- DT::renderDataTable({
    modelRun()
      datatable(
        df %>% mutate(Current  = as.numeric(Current)*(budget())), selection = 'none', editable = TRUE
      )
  })
}
  tableUI <- function(id) {
    ns <- NS(id)
    dataTableOutput(ns("x1"))
  }
#### ideally the second module for bookmarks
opBookmark <- function(){}
ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    column(12,
      column(3,tags$div(title="forecast", numericInput("budget_input", label = ("Total Forecast"), value = 2))),
      column(2, textInput(inputId = "description", label = "Bookmark description", placeholder = "Data Summary")),
      column(2, bookmarkButton(id="bookmarkBtn"))),
      column(2, actionButton("opt_run", "Run")),
    tags$style(type='text/css', "#bookmarkBtn { width:100%; margin-top: 25px;}")
  )
}
server <- function(input, output, session) {
  callModule( opFunc,"opfun",modelRun = reactive(input$opt_run),modelData = df,budget = reactive(input$budget_input))
  observeEvent(input$opt_run, {
    cat('HJE')
  })
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })
}
enableBookmarking(store = "url")
shinyApp(ui, server)

不幸的是,所提供的代码并非完全可复制或最少,因此我继续尝试删除我认为不是必需的内容,并从您的其他帖子中添加了df。我还将模块服务器名称从opFunc更改为tableMod,因为我试图与具有不同UI和服务器名称的模块一起工作,这使我感到困惑:)

以下代码按预期工作。

library(shiny)
library(DT)
library(dplyr)
#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,budget){
  output$x1 <- DT::renderDataTable({
    modelRun()
    isolate(
      datatable(
        modelData %>% 
          mutate(Current  = as.numeric(Current)*(budget())),
        selection = 'none', editable = TRUE
      )
    )
  })
}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}
ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    textInput(inputId = "description", "Bookmark description"),
    bookmarkButton(id="bookmarkBtn"),
    actionButton("opt_run", "Run")
  )
}
server <- function(input, output, session) {
  df <- data.frame(Channel = c("A", "B","C"),
                   Current = c(2000, 3000, 4000),
                   Modified = c(2500, 3500,3000),
                   New_Membership = c(450, 650,700),
                   stringsAsFactors = FALSE)
  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))
  observeEvent(input$opt_run, {
    cat('HJE')
  })
  setBookmarkExclude("bookmarkBtn")
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })
}
shinyApp(ui, server, enableBookmarking = "url")

最新更新