1

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)
user3245256
  • 1,842
  • 4
  • 24
  • 51
  • the min and max only works when you change the input with the arrows, but if you type it it won´t work as in your example. You must use if statements in the server. – Alejandro Andrade Feb 08 '18 at 02:10

3 Answers3

2

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))
Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
  • Thank you, but it's not really working from the user's perspective. First, the user can still manually enter a value above the allowed max in the FIRST box - and nothing happens. Second - for the second box - the user who enters values above the allowed max, can't see the warning messages because they go to the console, not to the US. – user3245256 Feb 08 '18 at 14:29
  • You could use the same trick for the first box (i.e. cap the value to `max_number1`). Additionally, you can print the messages in shiny on the server rather than in the R console. See these two topics: https://stackoverflow.com/questions/24873961/printing-text-in-shiny and https://stackoverflow.com/questions/27139348/shiny-printing-console-output-to-a-text-object-without-waiting-for-a-function-t. – Thomas Guillerme Feb 08 '18 at 23:34
1

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.

A Duv
  • 393
  • 1
  • 17
0

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)
        }
    })
iMSQ
  • 21
  • 4