Search code examples
rshinysurvey

Is it possible to randomize the order of response options with radiobuttons in rshiny?


Say for instance that I want to ask the question below and avoid ordering effects by randomizing. How can I do that in R Shiny?

radioButtons("knowledge1", 
                     label = "On which of the following the does the U.S. federal government currently spend the least?",
                     choices = c("Foreign aid" =  1,
                                 "Medicare" = 2,
                                 "National defense" = 3,
                                 "Social Security" = 4
                     ), selected = 99, width = "100%"),

Solution

  • We can use sample():

    library(shiny)
    
    knowledge1_choices <- 
      c("Foreign aid" =  1,
        "Medicare" = 2,
        "National defense" = 3,
        "Social Security" = 4
      )
    
    ui <- fluidPage(
    
      radioButtons("knowledge1", 
                   label = "On which of the following the does the U.S. federal government currently spend the least?",
                   choices = knowledge1_choices[sample(knowledge1_choices)],
                   selected = 99,
                   width = "100%"),
    )
    
    server <- function(input,output,session){
      
    
    }
    
    shinyApp(ui,server)