Consider the following code:
library(shiny)
ui <- shinyUI(
fluidPage(
column(12,
numericInput("test", h5("Test value:"), value = 500, min = 0, max = 10000, step = 100, width = '200px')
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
I would like to change the background colour of the numericInput widget to red based on invalid user input. So if the user enters text, or a value outside the min and max range, then the widget should be coloured red.
Note that I cannot use the solution of using a CSS file with, for example:
input:invalid {
background-color: #FFCCCC !important;
}
The reason is that is will colour the background red if the user enters any value that isn't a multiple of the 'step' value in the numericInput statement (see: R shiny numericInput step and min value interaction for details).
So how can I implement my own manual validation as above by styling conditionally based on validation rules I define? That is, so I can apply any rules stating what's valid, such as:
- if (is.numeric(input$test))
- if (input$test >= 0)
- if (input$test <= 10000)
input:out-of-range {......}
does the job. – Stéphane Laurent