MaxPlank
MaxPlank

Reputation: 185

Shiny do.call, lapply and modules

I'm trying to display a navlistPanel with several tabs and for each of those tabs a tabsetPanel with again several tabs. I manage to do so using the function do.call and two lapply to display the required number of tabs in the navlistPanel and tabsetPanel. However I can no longer display a table when a .csv file is uploaded.

Do anyone knows what I'm doing wrong here? Here's my code:

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)

Upvotes: 1

Views: 1193

Answers (1)

MaxPlank
MaxPlank

Reputation: 185

I've found the error, just add a ns() to the following line and the data will display properly:

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

Upvotes: 0

Related Questions