问题描述
在R Shiny应用程序中考虑以下数字小部件:
Consider the following numeric widget in an R Shiny app:
numericInput("val", "Enter value:", value = 50, min = 0, step = 5)
如果在运行应用程序时单击窗口小部件中的向上/向下箭头,则该值将按预期增加或减少5(0、5、10、15 ...).
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.
现在考虑将最小值更改为1:
Now consider changing the min value to 1:
numericInput("val", "Enter value:", value = 50, min = 1, step = 5)
如果现在单击向上/向下箭头,该值仍会增加/减少5,但从1开始,创建顺序1、6、11、16 ...
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,...
当最小值为1时,是否可以维持5的增减,但从0开始(因此顺序为0、5、10、15 ...)?
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?
一个可能需要这样做的示例(例如,在我的情况下)是您希望用户输入一个(严格地)正数,但由于5的倍数很好,很容易,所以递增/递减值为5,四舍五入的数字(而不是1、6、11、16等)
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.)
推荐答案
您可以使用updateNumericInput
来防止numericInput
中的空值.这是一个示例:
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)
这篇关于R闪亮的数值输入步长和最小值交互作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!