I am currently modifying a shiny dashboard in R and need to create radio buttons dynamically based on the content of a vector that is created in my server class:
available_years <- c("2019", "2020", "2021")
output$available_years <- reactive(available_years)
outputOptions(output, "available_years", suspendWhenHidden = FALSE)
In the UI class I want to use this to create the radio buttons like this:
fluidRow(
#Input Buttons for years
column(3,
radioButtons("radioYear", h4(paste("Available years:")),
choices = list("2019" = "2019",
"2020" = "2020",
"2021" = "2021")))
),
But instead of hard-coding it, I would like to take the length of the vector instead of the 3 and the content of the vector instead of of the hard-coded list. In terms of pseudo-code it would look like this:
fluidRow(
#Input Buttons for years
column(length(output.available_years),
radioButtons("radioYear", h4(paste("Available years:")),
choices = list(for(i in length(output.available_years){
output.available_years[i] = output.available_years[i]
}))))
),
I am thankful for any help!
CodePudding user response:
You can provide the dataset in server function along with render
commands and call them using uiOutput
in ui function. This will enable you to get the values dynamic based on the vector.
Code with sample data:
Data <- c('2019', '2020', '2021', '2022')
ui <- fluidRow(
#Getting values from server function
uiOutput('radioYear')
)
server <- function(input, output) {
output$radioYear <- renderUI({
Data <- Data
column(length(Data),
radioButtons("radioYear", h4(paste("Available years:")),
choices = Data))
})
}
shinyApp(ui, server)