Not able to update the values of radiobuttons

Hello,

I'm trying to predict a value in shiny app after regression. During the training in regression, I converted a few columns from boolean to numeric or character to numeric (by categorizing). Now in shiny app, it says that those column inputs (that require conversion to numeric) are different from the fit.

I looked up the error and it was suggested to use "updateRadioButtons" to update the values of those columns to numeric. But it is giving me errors. Not sure what's wrong with my code.

server <- function(input, output,session) {

output$charges= renderText({

observeEvent({
               a <- as.numeric(ifelse(input$sex == 'male', 1, 0))
               updateRadioButtons(session,"sex",value=a)
               
               b <- as.numeric(ifelse(input$smoker == 'yes', 1, 0))
               updateRadioButtons(session,"smoker",value=b)
               
               c <- as.numeric(ifelse(input$region == 'southwest', 0,
                                      ifelse(input$region == 'southeast', 1,
                                            ifelse(input$region == 'northwest', 2, 3))))
               updateRadioButtons(session,"region",value=c)
             })

  #input$sex <- ifelse(input$sex == 'male', 1, 0)
  #input$smoker <- ifelse(input$smoker == 'yes', 1, 0)
  #input$region <- ifelse(input$region == 'southwest', 0,
  #                 ifelse(input$region == 'southeast', 1,
  #                        ifelse(input$region == 'northwest', 2, 3)))
  
  # Create a data frame with user inputs

  input_data <- data.frame(age = input$age,
                           sex = input$sex,
                           bmi = input$bmi,
                           children = input$children,
                           smoker = input$smoker,
                           region = input$region)
  
  prediction <- predict(first_model, newdata = input_data)
  paste("Your estimated medical insurance for the upcoming year would be $", prediction)

})
}

I suggest that you post a minimal full example (UI and server code) that demonstrates the problem, plus the details of any error message you get.

The updateRadioButtons function does not take value as a parameter. Instead, it uses choices and selected. Assuming you don't want to change the choices being offered by the radio button, and instead just want to automatically change which one is selected, then you should change value in your code to selected. As an example:

updateRadioButtons(session, inputId = "sex", selected = a)

Here's the documentation so you can look into the function's parameters more.

Hope this helps!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.