firmo23
firmo23

Reputation: 8404

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

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)

Upvotes: 0

Views: 47

Answers (1)

YBS
YBS

Reputation: 21287

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)

Upvotes: 1

Related Questions