Reputation: 10548
My super awesome Shiny app looks like this:
library(shiny)
ui <- fluidPage(
numericInput(inputId = "A", label = "A", value = 5, step = 1),
uiOutput("slider"),
textOutput(outputId = "value")
)
server <- function(input, output) {
output$value <- renderText(paste0("A + B = ", input$A + input$B))
output$slider <- renderUI({
sliderInput(inputId = "B", label = "B", min = 0, max = 2*input$A, value = 5)
})
}
shinyApp(ui = ui, server = server)
The sliderInput for B is dynamic (S/O to HubertL & BigDataScientist) but now I need to protect the input for A from negative numbers.
How might I accomplish this?
Upvotes: 0
Views: 1098
Reputation: 61
Setting min argument in numericInput() does not solves the problem entirely. It works for mouse input but not for keyboard input. You can create a reactive validation to check if numeric input fit your standards like this:
output$value <- if(isValid_num) renderText(paste0("A + B = ", input$A + input$B))
isValid_num <- reactive({
input$num > 0
})
i would add is.integer(input$num) to logic condition in isValid_num so users wont input characters or float...
Upvotes: 4
Reputation: 1118
You need to just add min=0
argument to numericInput()
. After that it won't allow user to get over 0.
Upvotes: 0