简体   繁体   中英

R shiny numericInput step and min value interaction

Consider the following numeric widget in an R Shiny app:

numericInput("val", "Enter value:", value = 50, min = 0, step = 5)

If you click on the up/down arrows in the widget when the app is run, the value will increase or decrease by 5 (0, 5, 10, 15,...) as expected.

Now consider changing the min value to 1:

numericInput("val", "Enter value:", value = 50, min = 1, step = 5)

If you now click on the up/down arrows, the value will still increase/decrease by 5, but start from 1, creating the sequence 1, 6, 11, 16,...

Is it possible to maintain increments/decrements of 5 but starting from 0 (so the sequence is 0, 5, 10, 15,...) when the min value is 1?

An example where this might be needed (as in my case) is where you wish to have the user enter a (strictly) positive number, but have an increment/decrement value of 5 since multiples of 5 are nice, easy, rounded numbers (as opposed to 1, 6, 11, 16,... etc.)

You can use updateNumericInput to prevent null value in your numericInput . Here is an example:

library(shiny)

ui <- fluidPage(
  sidebarPanel(
    numericInput("val", "Enter value:", value=50, min = 0, step = 5)
  )
)

server <- function(input, output, session) {
  observeEvent(input$val, {
    x <- input$val
    if (x == 0 | is.na(x)){
      updateNumericInput(session, "val", value = 1)
    }
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM