简体   繁体   English

我可以在一个 Shinydashboard 模块中包含多个 UI 元素吗?

[英]Can I include multiple UI elements in a single shinydashboard module?

In there is the amazing convenience of putting s into the menuItem(menuSubItems()) portion of the dashboardSidebar() .有推杆的惊人方便S插入menuItem(menuSubItems())的部分dashboardSidebar() But I want the several elements of my UI and Server coded into modules so I can adhere to the framework... and I'm not seeing a clear way to do this without creating multiple UI functions for a single module.但是我希望我的 UI 和服务器的几个元素编码到模块中,这样我就可以坚持框架......而且我没有看到一种明确的方法来做到这一点,而无需为单个模块创建多个 UI 功能。 I've seen the shinydashboard golem example on github and it's too simple of an example that doesn't help.我见过的shinydashboard golem上例github ,它太简单没有帮助的例子。

For example, Is there a way I can do this?例如,有没有办法做到这一点?

In a module format:在模块格式中:

 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)

Is there any way to make this kind of thing work?有没有办法让这种事情起作用? The widget isn't showing up, likely because I don't think the modular framework allows for me to make two different UI elements for one piece of functionality.小部件没有出现,可能是因为我认为模块化框架不允许我为一项功能制作两个不同的 UI 元素。

Alright, so I got this working... surprisingly didn't take too much.好的,所以我开始工作了......令人惊讶的是并没有花太多时间。 I don't know if the complexity of my app will break this, but for anyone who was hoping to do this, maybe this is helpful:我不知道我的应用程序的复杂性是否会破坏这一点,但对于任何希望这样做的人来说,这可能会有所帮助:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM