Home > Blockchain >  In R/Shiny how to create others panels with purrr::map
In R/Shiny how to create others panels with purrr::map

Time:11-23

This is my code.

Why isnt working?

I think I should use do.call but idon't know how. The output should be the first 3 panels and the 4 panels,with a totalof 7 Panels

Any help?

library(shiny)
library(tidyverse)

tabpanel_function <- function(x){
  tabPanel(paste0("Panel",x))
}

ui <- fluidPage(
  
  tabsetPanel(
    tabPanel("Panela"),
    tabPanel("Panelb"),
    tabPanel("Panelc"),        
     
      1:4 %>% map(~ tabpanel_function(.x))
  )
)

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

shinyApp(ui, server)

CodePudding user response:

It could work to do call appendTab in the server function:

library(shiny)
library(tidyverse)

tabpanel_function <- function(x){
  tabPanel(paste0("Panel",x))
}

ui <- fluidPage(
  
  tabsetPanel(id = "x",
    tabPanel("Panela"),
    tabPanel("Panelb"),
    tabPanel("Panelc"),        
    
  )
)

server <- function(input, output, session) {
  1:4 %>% map(~ tabpanel_function(.x) %>% appendTab("x", .))
}

shinyApp(ui, server)

Edits

I removed my earlier erroneous assertion that it wouldn't work to do a loop in the ui construction. See here for a full demonstration of one way (using lapply which has same output as map).

  • Related