Zack
Zack

Reputation: 31

R Shiny Radio Buttons not Updating with updateRadioButtons

I'm working on a quiz type program in Shiny, which needs to have radio buttons that update with answers from a table. I have the code below, but the radio buttons don't update, and the answers remain 2, 3, 4, 5, and 6 despite changing questions. Any ideas on why this might be happening, and what would fix this?

library(shiny)


ui <- fluidPage(

  selectInput("numberchoice",label = "Choose an image", choices = c(1:6), selected = 1)
  ,
  imageOutput("image")
  ,
  radioButtons("answerchoice", "Answers", choices = c(2:6))

)

server <- function(input,output,session) {
  answers <- read.csv("~/Answers.csv")
  questions <- read.csv("~/Answers.csv")
  output$image <- renderImage(list(src = paste("~",".png", sep = "")
  ,contentType = "image/png", alt = "Face"),deleteFile = FALSE)
  eventReactive(input$numberchoice,{updateRadioButtons(session,"answerchoice",choices = questions[input$numberchoice,2:6])})
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 2299

Answers (1)

Gregor de Cillia
Gregor de Cillia

Reputation: 7635

Try replacing eventReactive with observeEvent. The following code works for me.

library(shiny)

ui <- fluidPage(
  selectInput("numberchoice", label = "Choose an image", choices = 1:6, selected = 1),
  radioButtons("answerchoice", "Answers", choices = 1:6 )

)

server <- function(input, output, session) {
  observeEvent(input$numberchoice,{
    updateRadioButtons(session, "answerchoice",
      choices = letters[1:input$numberchoice])})
}

shinyApp(ui = ui, server = server)

It seems like eventReactive didn't trigger so updateRadioButtons was not the problem.

Upvotes: 1

Related Questions