【发布时间】:2017-08-17 21:03:14
【问题描述】:
我正在尝试显示一个带有多个选项卡的 navlistPanel,并为每个选项卡显示一个带有多个选项卡的 tabsetPanel。我设法使用函数 do.call 和两个 lapply 来在 navlistPanel 和 tabsetPanel 中显示所需数量的选项卡。但是,当上传 .csv 文件时,我无法再显示表格。
有谁知道我在这里做错了什么? 这是我的代码:
library(shiny)
library(shinydashboard)
moduleUI <- function(id){
ns <- NS(id)
tagList(
sidebarLayout(
sidebarPanel(
fileInput(ns("file"), label = "", multiple = TRUE,
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv'))
),
mainPanel(div( dataTableOutput(ns('table')), style = "font-size: 70% ;width: 70"))
))}
module <- function(input, output, session){
output$table <- renderDataTable({
inFile <- input$file
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath)
})
}
moduleUI2 <- function(id){
ns <- NS(id)
tagList(
do.call(navlistPanel, args = c( id = "tabs", lapply(1:4, function(i) {
tabPanel(title = paste("tab", i), style = 'overflow-x: scroll',
mainPanel(
do.call(tabsetPanel, c(id = paste0("versions",i), lapply(1:5, function(n){
tabPanel(title = paste("version", n),
moduleUI(paste("base",i, n, sep = "_")),
h4(paste("tab", n))
)
}))),
width = 12)
)
})))
)}
module2 <- function(input, output, session){
lapply(1:4,function(i) {
lapply(1:5, function(n) {
callModule(module, paste("base",i,n, sep = "_"))
})
})
}
ui <- dashboardPage(
dashboardHeader(title = "App"),
dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Tab1", tabName = "Tab1")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "Tab1",
moduleUI2("base")
))
))
server <- function(input, output, session){
callModule(module2, "base")
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
看起来您在
tabsetPanel上/内部重复 ID。试试do.call(tabsetPanel, c(id = paste("versions", i), ...和moduleUI(paste("base",i,n,sep="_"))。 -
非常感谢您的帮助,您的解决方案运行良好。但是,我现在对我的应用程序进行了一些更改(请参阅编辑的代码),添加了另一个模块。再一次 dataTable 没有显示。这次你能看出什么问题了吗?
-
尚未阅读您的编辑,但我认为您可能对此感兴趣:github.com/jcheng5/shiny-partials
-
您对
do.call(tabsetPanel, c(id = name, lapply(...)))的实施为我提供了很多帮助,感谢您提出问题!
标签: r shiny shiny-server