user3245256
user3245256

Reputation: 1968

r shiny numericInput allows the entry of values above the max

Just checking if there is yet a solution to this issue: I don't want the user to enter values in "Input number 2" that are above the allowed max (whatever is entered in "Input number 1"). It works when the user uses the spinner, but not when the user just types the values in.

library(shiny)

ui <- fluidPage(
 numericInput(inputId = "inNumber1", label = "Input number 1", 
               value = 100, min = 10, max = 200, step = 10),
  numericInput(inputId = "inNumber2", label = "Input number 2",
               value = 50, min = 10, max = 200, step = 10),
  textOutput("out1"),
  textOutput("out2")
)

server <- function(input, output, session) {

  # Reacting to changes in input 1:
  observeEvent(input$inNumber1, {

    number1 <- input$inNumber1  # value 1
    updateNumericInput(session, inputId = "inNumber2",
                       label = "Input number 2",
                       value = floor(number1/2),
                       min = 10, max = number1, step = 10)
    output$out1 <- renderText({number1})

  })

  # Reacting to changes in input 2:
  observeEvent(input$inNumber2, {
    number2 <- input$inNumber2  # value 2
    output$out2 <- renderText({number2})
  })
}

shinyApp(ui, server)

Upvotes: 1

Views: 3148

Answers (3)

iMSQ
iMSQ

Reputation: 31

Check input for extreme values, if exceeds then have a pop-up warning and change the input value (for example, to max value):

observe({
        if (input$value1 > 100 || input$value1 < 0) {
            showModal(
                modalDialog(
                    title = strong("Warning!", style = "font-size:24px;
                                   color: red;"),
                    p("Input value exceeds the limits.", 
                      style = "font-size:16px"),
                    footer = modalButton("Close")
                ))
            updateNumericInput(session, "value1", value = 100)
        }
    })

Upvotes: 1

A Duv
A Duv

Reputation: 403

I solved something similar by moving the numericInput() to the server, and changing the numericInput() in the UI to uiOutput(). I'm not familiar with observeEvents() yet, but I hope this pseudo-code helps.

Previous UI:

    HTML(paste("Numeric Input Descriptor/instructions"))
    numericInput(inputId = "Num_2", label = "BLA", min = __, max = __) #1

1: These numbers are your original inputs for your numeric input

Changes in UI

    HTML(paste("Numeric Input Descriptor/instructions"))
    uiOutput(outputId = "num_2")

Add to server function:

    output$num_2 <- renderUI({
    numericInput(inputId = "Num_2", label = "BLA", min = __, max = __) #2
    })

2: The min is your original number/value, while the max is based upon the ObserveEvent().

  • When I set the max = () in my case, I didn't need to say max = reactive(length(data())), but just length(data()). Perhaps you just need ObserveEvents()

  • Additionally, the inputId from the renderUI() worked with all of my other parts in the server so no additional changes were needed.

I can give this a closer look if the above psuedo code isn't sufficient.

Upvotes: 1

Thomas Guillerme
Thomas Guillerme

Reputation: 1867

One quick and dirty solution would be to through an error message, a warning or simply cap number2.

  # Reacting to changes in input 2:
  observeEvent(input$inNumber2, {
    number2 <- input$inNumber2  # value 2
    max_number2 <- 200
    if(number2 > max_number2) {
        ## Conditions
        number2 <- max_number2
    }
    output$out2 <- renderText({number2})
  })

Or, for the stop conditions:

## Condition
stop(paste0("number2 must be lower than ", max_number2))

Or, for the warning conditions

## Condition
number2 <- max_number2
warning(paste0("number2 is set to ", max_number2))

Upvotes: 2

Related Questions