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)
UPDATE
with the help of @Andy Baxter managed to solve the problem and additionally added plots to each tabpanel. This is the code:
library(shiny)
library(tidyverse)
tabpanel_function <- function(x,id){
tabPanel(paste0("Panel",x),
plotOutput(paste0("chart_",id))
)
}
ui <- fluidPage(
tabsetPanel(id = "x",
tabPanel("Panela"),
tabPanel("Panelb"),
tabPanel("Panelc")
)
)
server <- function(input, output, session) {
1:4 %>% map(~ tabpanel_function(.x, id = .x) %>% appendTab("x", .))
output$chart_1 <- renderPlot({
ggplot(mtcars, aes(cyl,mpg)) + geom_line(color ='red')
})
output$chart_2 <- renderPlot({
ggplot(mtcars, aes(cyl,mpg)) + geom_line(color ='green')
})
output$chart_3 <- renderPlot({
ggplot(mtcars, aes(cyl,mpg)) + geom_line(color ='blue')
})
output$chart_4 <- renderPlot({
ggplot(mtcars, aes(cyl,mpg)) + geom_line(color ='yellow')
})
}
shinyApp(ui, server)
>Solution :
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)
The UI construction parts of a shiny app are (as far as I understand it) not being run as code when constructing the frontend. The fluidPage function is compiling each part as an HTML element rather than evaluating the output of the code. So generally, use this part as a static thing for building the page, and any code evaluation have that done in the server.
I might have missed some technicalities of how Shiny functions though, so if anyone wants to correct me please do!