1

I need to create a panels inside a tabset in shiny using map functions .

The output that I need is one tabsetPanels with 3 tab panels This is the app:

library(shiny)

ui <- fluidPage(


  tabsetPanel('one',
              map(1:3,
                 ~ tabPanel(paste0("panel_",.x))
                  )
              )
)


server <- function(input, output, session) {

}

shinyApp(ui, server)

What am I doing wrong?

Laura
  • 675
  • 10
  • 32
  • Not clear why you're using `print()` here. Furthermore, you're printing `x` which is something that the output of the print statement is assigned to, which doesn't make much sense. Perhaps missing some enclosures? Admittedly stylistically, you're also using `glue` as a dependency to do something that's even fewer characters `paste`d: `paste0("chart_", n_panels)` – socialscientist Jul 17 '22 at 22:31
  • @socialscientist thanks for your time. Yes, I've been studying about functions with for loops. My idea with `print` was to print the results panels on the UI/screen of my app. I think the main question here is how to create the panels inside the `tabsetPanel` usinf or loop. – Laura Jul 17 '22 at 22:35
  • If you can provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of what, say, 2 iterations of the loop should produce then I can help. The loop will just automate the creation of an arbitrary number of panels, providing manual examples will help us understand what you're looking for. – socialscientist Jul 17 '22 at 22:40
  • @socialscientist I edited the question. I think this way I can better express what I need to learn. – Laura Jul 17 '22 at 22:54
  • 1
    This looks to me like it might be a good place to use [modules](https://shiny.rstudio.com/articles/modules.html). – Limey Jul 18 '22 at 07:18
  • @Limey thanks for your time. I will use modules on my original code which is really big. But I think my question is simpler than this. How can I create panels inside tabsetPanel functions using purrr::map funciton in UI ? – Laura Jul 18 '22 at 11:49
  • 1
    What would the desired result look like? 3 tabPanels with the names "panel_1","panel_2", "panel_3"? What would the argument "one" stand for? – Jan Jul 18 '22 at 18:53
  • @Jan what I want is one tabset with 3 tab panels using map function. I will include this on the question. – Laura Jul 18 '22 at 19:15

1 Answers1

1

The map function returns a list. But tabsetPanel does not expect a list of tabPanels. It rather expects that each panel is an individual parameter.

You can get around this using do.call. WIth do.call you can call any function and pass it's argument as list.

See below:

library(shiny)

ui <- fluidPage(
  do.call(
    tabsetPanel,
      map(1:3, ~ tabPanel(paste0("panel_",.x)))
    )
)


server <- function(input, output, session) {

}

shinyApp(ui, server)
Jan
  • 4,974
  • 3
  • 26
  • 43