在 shinydashboard 中,将 widgets 放入
menuItem(menuSubItems())
的 dashboardSidebar()
部分非常方便。但是我希望将我的 UI 和服务器的几个元素编码到模块中,这样我就可以遵守 golem 框架......而且我没有看到一个明确的方法来做到这一点,而不是为单个模块创建多个 UI 功能。我已经在 shinydashboard
上看到了 golem
github
的例子,这个例子太简单了,没有帮助。
例如,我有办法做到这一点吗?
在模块格式中:
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 元素。
好吧,所以我开始工作了……令人惊讶的是没有花太多时间。我不知道我的应用程序的复杂性是否会打破这个,但对于任何希望这样做的人来说,也许这会有所帮助:
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)
您是否发现此方法在应用程序中出现其他问题?我刚才在一个带有嵌套模块的应用程序中尝试了相同的方法,我相信它会因为命名空间而破坏应用程序。