0
votes

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?

2
You can set the min value in numericInput, for ex numericInput(inputId = "A", label = "A", value = 5, step = 1,min=0)NicE

2 Answers

0
votes

You need to just add min=0 argument to numericInput(). After that it won't allow user to get over 0.

0
votes

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