Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell whether sliderInput was moved relative to the initial value?

Tags:

shiny

I am building a simple survey-like shiny app. I record user responses to several questions using sliders. In the case the participant response is equal to the initial value of the slider, I would like to know whether the participant intentionally left it in that position or not.

I'm consider either of the following options, but I don't know how to do neither of both: a- enforce participants to move the slider before submitting the response. b- set starting slider value at 'NULL' or something similar (meaning the slider cursor is hidden until first click on the slider).

like image 761
Guillermo Solovey Avatar asked Dec 06 '25 19:12

Guillermo Solovey


1 Answers

I would go for option 3, make a flag which is set whenever the slider is changed and depending on that you do something different. For instance:

library(shiny)

ui <- fluidPage(sliderInput("sld", "Value", 1, 10, 1), 
                actionButton("go", "Validate"), 
                verbatimTextOutput("out"))

server <- function(input, output, session) {
   has_changed <- reactiveVal(FALSE)

   observeEvent(input$sld, {
         ## whenever the slider changes flag it
         has_changed(TRUE)
      }, 
     ## use ignoreInit to avoid firing on startup
     ignoreInit = TRUE)


   output$out <- renderPrint({
      input$go
      if (isolate(has_changed())) {
         "Changed!"
      } else {
         "Never Changed"
      }})
}

shinyApp(ui, server)
like image 102
thothal Avatar answered Dec 09 '25 06:12

thothal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!