首页 > 解决方案 > 如何将 R 闪亮的应用程序分解为模块?

问题描述

因此,在我之前的一个问题中,我遇到了关于如何恢复书签和运行模型的问题。这只是一个可重现的示例,但作为一个应用程序,我想随着应用程序大小的不断增加对其进行模块化。我有下面的代码。在模块 1 中,当用户单击书签时,我想调用以呈现数据表并调用模块 2。到目前为止,我在模块 1 中的代码不起作用。模块 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)

标签: rshiny

解决方案


不幸的是,提供的代码既不是完全可重复的,也不是最小的,所以我继续尝试删除我认为不必要的内容,并df从你的另一篇文章中添加。我还将模块服务器名称从更改为opFunctableMod因为尝试使用具有不同 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")

推荐阅读