Question

How to turn off scrolling for a numericInput?

Does anyone know how to turn off the slider feature of a numericInput in R Shiny?. When I run my app and am entering an input into the numeric input field, if the field is still selected and the user scrolls down, it changes what's entered into the input field. Is there a way to make it to where scrolling doesn't change the input field?

I tried using shinyjs but couldn't get it to work.

 3  55  3
1 Jan 1970

Solution

 2

We can inject this JS solution to prevent scrolling for numericInputs:

library(shiny)

ui <- fluidPage(
  tags$script(HTML(
    "document.addEventListener('wheel', function(event){
        if(document.activeElement.type === 'number'){
            document.activeElement.blur();
        }
     });"
  )),
  numericInput("blur_on_scroll", "blur_on_scroll", 0L, 10L)
)

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

shinyApp(ui, server)

Please see this related article.

2024-07-03
ismirsehregal

Solution

 0

This may not be what you are after, but you can disable them entirely if you want (via geeksforgeeks):

library(shiny)

ui <- fluidPage(
  tags$head(tags$style(
    'input::-webkit-outer-spin-button,
     input::-webkit-inner-spin-button {
                -webkit-appearance: none;
                margin: 0;
            }

            input[type="number"] {
                -moz-appearance: textfield;
            }'
  )),
  numericInput("a", "A", 0L, 10L)
)

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

shinyApp(ui, server)
2024-07-03
smartse