R shiny numericInput step and min value interaction

我的梦境 提交于 2019-12-14 01:26:40

问题


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.)


回答1:


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)


来源:https://stackoverflow.com/questions/50313005/r-shiny-numericinput-step-and-min-value-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!