【问题标题】:Shiny Dashboard with Modules reactiveness具有模块反应性的闪亮仪表板
【发布时间】:2021-01-17 08:14:18
【问题描述】:

嗨,我有点卡在闪亮的仪表板上,我试图将一些功能剥离到 ui(和服务器)模块和子模块中。 我想要实现的是这个

library(shiny)
runApp(list(
  ui = basicPage(
    selectInput("select", "Select columns to display", names(mtcars), multiple = 
                  TRUE),
    h2('The mtcars data'),
    dataTableOutput('mytable')
  ),
  server = function(input, output) {
    output$mytable = renderDataTable({
      columns = names(mtcars)
      if (!is.null(input$select)) {
        columns = input$select
      }
      mtcars[,columns,drop=FALSE]
    })
  }
))

到目前为止,通过这个嵌入了带有模块(基于魔像骨架)的 Shinydashbaord ...

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)

# app_ui 
app_ui <- function(request) {
  tagList(
    shinydashboardPlus::dashboardPagePlus(
      header = shinydashboardPlus::dashboardHeaderPlus(title = "module_test",
                                                       enable_rightsidebar = FALSE),
      sidebar = shinydashboard::dashboardSidebar(
        shinydashboard::sidebarMenu(id = "tabs",
                                    mod_test_sidebar_ui("test_ui_1"))
      ),
      #
      body =  shinydashboard::dashboardBody(shinydashboard::tabItems(
        mod_test_body_ui("test_ui_1"))
      )
      , rightsidebar = NULL,
      , title = "Testing Shiny modules"
    )
  )
}
# app_server 
app_server <- function(input, output, session) {
  shiny::moduleServer(id = "test_ui_1", module = mod_test_server)
}

##   THE MODULES   #######################################################
# the sidebar module
mod_test_sidebar_ui <- function(id) {
  ns <- NS(id)
  shinydashboard::menuItem("Module Testing",
                           tabName = "tab_testing_mod",
                           icon = icon("th"))
}
#---------------------------------
# the body module b/c wanna use tabs I decided to add one more mod layer 
mod_test_body_ui <- function(id) {
  ns <- NS(id)
  shinydashboard::tabItem(tabName = "tab_testing_mod",
                          mod_test_modules_ui(id)
                          
  )
}
# the ('additional') body_ui "content" module
mod_test_modules_ui <- function(id) {
  ns <- NS(id)
  fluidRow(
    shinydashboard::box(
      title = "Select Cols",
      selectInput("select", "Select columns", names(mtcars), multiple = TRUE)
    )
    , 
    shinydashboard::box(
    title = "Data Viewer",
    width = 10,
    DT::dataTableOutput(ns('data_table'))
    )
  )
}
#---------------------------------
#module server
mod_test_server <- function(input, output, session) {
  ns <- session$ns
  output[['data_table']] <- renderDataTable({
    #output$data_table <- renderDataTable({
    columns = names(mtcars)
    if (!is.null(input$select)) {
      columns = input$select
    }
    mtcars[,columns,drop=FALSE]
  }, filter = 'top')
}
####################################################################
run_app <- function(...) {
  shiny::shinyApp(
    ui = app_ui, 
    server = app_server)
}
#---------------------------------
run_app()

上面的问题归结为最少的代码行,所以它卡在我现在的同一点上。无论我尝试什么,模块版本都不会像第一个示例那样更新(过滤)选定的数据列。 我很确定我只是没有正确掌握该命名空间上下文(尤其是在服务器端)。我猜/希望有人能轻松发现我的错误。

【问题讨论】:

  • 一方面,您的 select 输入需要传递给 ns 。喜欢ns('select')
  • Facepalm -> 这正是我犯的错误!我的“真实”应用程序比这部分大一点,我再也看不到树后的木头了。就像我猜到的那样,一双新鲜的眼睛立即看到了它 - @SmokeyShakers - 非常感谢!

标签: r shiny shinydashboard golem


【解决方案1】:

正如@SmokeShakers 指出的那样

# the ('additional') body_ui "content" module
mod_test_modules_ui <- function(id) {
  ns <- NS(id)
  fluidRow(
    shinydashboard::box(
      title = "Select Cols",
      selectInput("select", "Select columns", names(mtcars), multiple = TRUE)
    )
    , 
    shinydashboard::box(
    title = "Data Viewer",
    width = 10,
    DT::dataTableOutput(ns('data_table'))
    )
  )
}

第 6 行代码中的selectInput("select", ... 应该是selectInput(ns("select"), ...,然后一切运行顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2019-02-12
    • 2021-04-11
    • 2016-10-22
    • 2018-05-30
    相关资源
    最近更新 更多