简体   繁体   English

R闪亮的numericInput步和最小值交互

[英]R shiny numericInput step and min value interaction

Consider the following numeric widget in an R Shiny app: 考虑R Shiny应用程序中的以下数字小部件:

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. 如果在运行应用程序时单击窗口小部件中的向上/向下箭头,则值将按预期增加或减少5(0,5,10,15,...)。

Now consider changing the min value to 1: 现在考虑将最小值更改为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,... 如果您现在单击向上/向下箭头,该值仍将增加/减少5,但从1开始,创建序列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? 当最小值为1时,是否可以保持5的增量/减量但从0开始(因此序列为0,5,10,15 ......)

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.) 可能需要这样的例子(如我的情况)是你希望用户输入(严格)正数,但增量/减量值为5,因为5的倍数是漂亮,简单,舍入的数字(而不是1,6,11,16 ......等)

You can use updateNumericInput to prevent null value in your numericInput . 您可以使用updateNumericInput ,以防止你的空值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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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