0

Is it possible to create tabItems and subItems and fluidRow boxes with loops within R for RShiny?

For example, instead of doing all this:

  tabItem(tabName = "table1", 
          textOutput("outputText1"),
          div(style = 'overflow-x: scroll', DT :: dataTableOutput("table1"))
  ),
  tabItem(tabName = "table2", 
          textOutput("outputText2"), 
          div(style = 'overflow-x: scroll', DT :: dataTableOutput("table2"))
  ),
  tabItem(tabName = "table3", 
          textOutput("outputText3"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table3"))
  ),
  tabItem(tabName = "table4", 
          textOutput("outputText4"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table4"))
  ),
  tabItem(tabName = "table5", 
          textOutput("outputText5"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table5"))
  ),
  tabItem(tabName = "table6", 
          textOutput("outputText6"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table6"))
  ),
  tabItem(tabName = "table7", 
          textOutput("outputText7"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table7"))
  ),
  tabItem(tabName = "table8", 
          textOutput("outputText8"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table8"))
  ),

I could just create loop to create tabItems?

extra background info: In university, when I was first learning R, my prof told me that using if and for statements can cause R to be very slow. So I try to avoid using loops as much as possible.

user6214
  • 69
  • 7
  • Sure you can write loops to do this. Or even better use `lapply/map` statements. Badly written loops in R are slow, but as a general rule, loops themselves are not slow so that was bad info. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Right now it's hard to make a suggestion and verify it works. – MrFlick Jun 15 '18 at 15:13
  • 2
    https://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html – Pork Chop Jun 15 '18 at 15:41

1 Answers1

0

You would have to use an lapply and insertUI into a uiOutput component like in the example.

UI

library(shiny)

shinyUI(fluidPage(

sidebarLayout(
  sidebarPanel(
     #Just remove this
  ),
  
  mainPanel(
     uiOutput("dynamicTabset")
     
  )
)
))

SERVER

library(shiny)

shinyServer(function(input, output) {
 
lapply(1:10, 
       function(x) {
         insertUI(
           selector = '#dynamicTabset',
           ui = tagList(
             tabItem(tabName = paste0("table",x), 
                     textOutput("outputText3"), 
                     div(style = 'overflow-x: scroll', DT :: dataTableOutput("table3"))
             )
         ))
       })
 })
Community
  • 1
  • 1