Search code examples
rshinylapplytabpanel

How to use lapply or another higher order function when calling tabPanel in R shiny


I am using tabPanel in R shiny and would like to make the coder nicer by using a function like lapply

This is the tabBox

tabBox(
  title = mytitle, id = myid, width = "8",
  tabPanel(myTitle[1], icon = icon("adjust"), plotOutput(myPlot[1])),
  tabPanel(myTitle[2], icon = icon("adjust"), plotOutput(myPlot[2])),
  tabPanel(myTitle[3], icon = icon("adjust"), plotOutput(myPlot[3])),
  tabPanel(myTitle[4], icon = icon("adjust"), plotOutput(myPlot[4]))
) 

the vectors myTitle and myPlot are defined as

myTitle=c("A","B","C","D")
myPlot =c("X1","X2","X3","X4")

How can I avoid repeated tabPanel definitions by using a higher order function, e.g. lapply or another one?

Thanks for any help


Solution

  • do.call(function(...){
      tabBox(title = mytitle, id = myid, width = "8", ...)
    }, mapply(function(title, plotid){
      tabPanel(title, icon = icon("adjust"), plotOutput(plotid))
    }, myTitle, myPlot, SIMPLIFY = FALSE, USE.NAMES = FALSE))
    

    Not highly readable, but this avoids the repetitions.