【问题标题】:Can I include multiple UI elements in a single shinydashboard module?我可以在单个闪亮仪表板模块中包含多个 UI 元素吗?
【发布时间】:2020-07-04 07:04:45
【问题描述】:

中,将s 放入dashboardSidebar()menuItem(menuSubItems()) 部分非常方便。但我希望我的 UI 和服务器的几个元素编码到模块中,这样我就可以遵守 框架......而且我没有看到一个明确的方法来做到这一点,而无需为单个模块创建多个 UI 功能。我在github 上看到了shinydashboard golem 示例,这个示例太简单而无济于事。

例如,有没有办法可以做到这一点?

模块格式:

 library(shiny)
 library(shinydashboard)

 ###   The Sidebar Menu with a Widget Subitem
 mod_myAppSidebar_ui<-function(id) {
      ns <- NS(id)
      tagList(menuItem("Attributes", tabName="ourdata",
               textInput("textSearch","SQL Search String", value = "")))
 }

 ###   The Dashboard Body output
 mod_myAppBody_ui<-function(id) {
      ns <- NS(id)
      tagList(box(shiny::dataTableOutput(outputId = "OutputData")))
 }

 mod_myApp_server<-function(input, output, session) {
        ns <- session$ns
        output$OutputData<-shiny::renderDataTable({
              somedata=data.frame(Rows=letters,Indexes=1:length(letters))
              somedata[grepl(tolower(input$textSearch),somedata$Rows),]
              })
 }

 ###   DashboardPage requires separate arguments for the UI elements
 ui <- dashboardPage(header = dashboardHeader(title = "Rosetta"),
                     sidebar = dashboardSidebar(mod_myAppSidebar_ui("MySearch")),
                     body = dashboardBody(mod_myAppBody_ui("MySearch")))

 server <- function(input, output, session) {
           callModule(mod_myApp_server, "MySearch")
 }

 shinyApp(ui,server)

有没有办法让这种事情发挥作用?小部件没有显示,可能是因为我认为模块化框架不允许我为一个功能创建两个不同的 UI 元素。

【问题讨论】:

    标签: shinydashboard widget golem r shiny widget shinydashboard golem


    【解决方案1】:

    好的,所以我得到了这个工作......令人惊讶的是并没有花费太多。我不知道我的应用程序的复杂性是否会破坏这一点,但对于任何希望这样做的人来说,这可能会有所帮助:

    library(shiny)
    library(shinydashboard)
    library(DT)
    
    mod_myAppSidebar_ui<-function(id) {
      ns <- NS(id)
      tagList(menuItem("Attributes", tabName="ourdata",
                       textInput(ns("textSearch"),"SQL Search String", value = ""),
                       actionButton(ns("go"),label = "Search")))
    }
    
    mod_myAppBody_ui<-function(id) {
      ns <- NS(id)
      tagList(fluidRow(title = "Data Selected",
                       box(DT::dataTableOutput(outputId = ns("OutputData")))))
    }
    
    mod_myApp_server<-function(input, output, session, r) {
      ns <- session$ns
    
      observeEvent( input$go , {
        r$textSearch<-input$textSearch
        print(r$textSearch)
        somedata=data.frame(Rows=letters,Indexes=1:length(letters))
        r$chooseData<-somedata[grepl(tolower(input$textSearch),somedata$Rows),]
      })
    
      output$OutputData<-DT::renderDataTable(r$chooseData)
    
    }
    
    ui <- dashboardPage(header = dashboardHeader(title = "Rosetta"),
                        sidebar = dashboardSidebar(mod_myAppSidebar_ui("MySearch")),
                        body = dashboardBody(mod_myAppBody_ui("MySearch")))
    
    server <- function(input, output, session) {
      r<-reactiveValues()
      callModule(mod_myApp_server, "MySearch", r)
    }
    
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 2018-06-17
      • 2015-04-01
      • 2016-01-11
      • 2017-11-30
      • 2021-10-10
      • 1970-01-01
      • 2018-08-05
      相关资源
      最近更新 更多