Home > Net >  Add a choice to the radioButtons() every time the actionButton() is pressed
Add a choice to the radioButtons() every time the actionButton() is pressed

Time:03-31

In the shiny app below I would like every time I press the actionButton() to be added a choice to the radioButtons() like "Plot 2", "Plot 3" etc.

## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    actionButton("add","Add choice"),
    uiOutput("sil1")
    
    
  ),
  dashboardBody(
    
  )
)

server <- function(session,input, output) {
  
 output$sil1<-renderUI({
   radioButtons("pp","Pick plot",choices =c("Plot 1"))
   
 })
  
}

shinyApp(ui, server)

CodePudding user response:

Perhaps you are looking for this

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    actionButton("add","Add choice"),
    radioButtons("pp","Pick plot",choices = c("Plot 1"))
    
  ),
  dashboardBody(
    
  )
)

server <- function(input, output, session) {
  my <- reactiveValues(choices="Plot 1")
  observeEvent(input$add, {
    my$choices <- c(my$choices,paste("Plot",input$add 1))
    updateRadioButtons(session,"pp",choices=my$choices)
  })
  
}

shinyApp(ui, server)
  • Related