shinyvalidate icon indicating copy to clipboard operation
shinyvalidate copied to clipboard

`sv_numeric()` produces unexpected validation error message

Open Teebusch opened this issue 2 years ago • 3 comments

Context: Using add_rule(..., sv_numeric())) with numericInput(). User interaction: Enter non-numeric value, e.g., some combination of - , e . Issue: The message shown to the user is not the one set in message and cannot be configured.

Example app:

library(shiny)  # version 1.7.1
library(shinyvalidate)   # version 0.1.2

ui <- fluidPage(
  numericInput("number", "Number", value = 0),
)

server <- function(input, output, session) {
  iv <- InputValidator$new()
  iv$add_rule("number", sv_numeric(message = "A number is required."))
  iv$enable()
}

shinyApp(ui, server)

Created on 2022-07-15 by the reprex package (v2.0.1)

Expected shinyvalidate output: A number is required Actual shinyvalidate output: Must not contain NA values

The error message should be the one set by the user, or the NA-specific error message should be configurable, because

  • For apps developed in other languages an English error message sticks out.
  • For non-technical users this error messsage may not be helpful.

I believe the message is created by sv_basic() here: https://github.com/rstudio/shinyvalidate/blob/575f5865fcb028656b58760ba357b6247b97d866/R/rules.R#L1417

sv_numeric() composes a rule, starting with sv_basic() and adds the numeric-specific rules on top of it:https://github.com/rstudio/shinyvalidate/blob/575f5865fcb028656b58760ba357b6247b97d866/R/rules.R#L500

The basic rules are checked first and validations of the basic rules are triggered first. This includes the "no NA" validation check. The corresponding error message is hard coded https://github.com/rstudio/shinyvalidate/blob/575f5865fcb028656b58760ba357b6247b97d866/R/rules.R#L1448.

Teebusch avatar Jul 15 '22 07:07 Teebusch

Hi Teebusch, Have you found a workaround for this? I have the same issue.

Regards, Maria

maria-VL avatar Sep 15 '22 13:09 maria-VL

Hi @maria-VL, No, I ended up using another package instead, because I was in a bit of a hurry. I wonder if a workaround might be to allow NAs in sv_numeric() and use additional validation with sv_regex() to capture non-numeric or empty inputs? Or only use sv_regex()?

Teebusch avatar Sep 15 '22 15:09 Teebusch

Hi all.

I am probably very late to answer this one, but just for reference.

The add_rule method also accepts custom functions to makes this checks. Find below a functional example that I think covers your case. In the case of the numericInput I suppose covering the NA case is enough, but you could modify the function to emit any other message or cover more cases.

library(shiny)  # version 1.7.1
library(shinyvalidate)   # version 0.1.2

ui <- fluidPage(
  numericInput("number", "Number", value = 0),
)

server <- function(input, output, session) {
  iv <- InputValidator$new()
  iv$add_rule("number", function(x){
    if(is.na(x)) "Please input a numeric value" else NULL
  })
  iv$enable()
}

shinyApp(ui, server)

zsigmas avatar Nov 29 '23 21:11 zsigmas