闪亮的do.call,lapply和模块

时间:2017-08-16 14:06:55

标签: r shiny shiny-server

我正在尝试显示带有多个选项卡的navlistPanel,并为每个选项卡中的每个选项卡添加了几个选项卡。我设法使用函数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)

1 个答案:

答案 0 :(得分:0)

我发现了错误,只需在下一行添加ns(),数据就会正常显示:

ns(paste("base",i, n, sep = "_"))