0

Do you know how to have a nested appendTab in R. I want to be able to append the tabs with one for loop and in each tab I append other tabs with another for loop.

Thank you for your answers

library(shiny)

ui <- fluidPage(
      column(
        3,
        box(
          title = "Validation studies",
          actionButton("add", "add tab"),
          width = 12
        )
      ),
      column(
        9,
        box(
          title = "Validation studies",
          tabsetPanel(id = ns("tabs")),
          width = 12
        )
      )
    )

server <- function(input, output, session) {
  n <- 0
  observeEvent(input$add, {
    n <<- n + 1
    appendTab(inputId = "tabs",
      tab = tabPanel(title = "Test",
      box(
          tabsetPanel(id = ns(paste0("tabs_", n))),
          width = 12
        )
      ),
      select = TRUE
    )
    for(i in 1:3){
      print(i)
      appendTab(inputId=paste0("tabs_",n),
        tabsetPanel(
          tabPanel(
            "Dynamic",
            paste("Content for dynamic tab", i)
          )
        )
      )
    }
  })
}

shinyApp(ui, server)

But I get Warning: Error in ns: function "ns" could not found [No stack trace available]

bretauv
  • 7,756
  • 2
  • 20
  • 57
cedrik24
  • 31
  • 7
  • Generally, `ns()` is used in [modules](https://shiny.rstudio.com/articles/modules.html), and the first line is defining `ns()` as `ns <- NS(id)`. Here, you didn't write this line so `ns()` is unknown. I think that you should either create modules or not use `ns()` in your example. – bretauv Jun 14 '20 at 08:22
  • [This post](https://stackoverflow.com/questions/62341353/saving-dynamic-ui-to-global-r-workspace/62342118#62342118) includes an example of how to construct a dynaimc UI in which widgets appear and disappear in response to user input. You could easily adapt the technique to your situation. Although you wouldn't be using a `for` loop... – Limey Jun 14 '20 at 15:22

0 Answers0