shiny icon indicating copy to clipboard operation
shiny copied to clipboard

Perform type check before value check

Open hadley opened this issue 4 years ago • 6 comments

If you don't validate the input types before validating the values, you get uninformative errors from min/max. (Which comes up in Mastering Shiny).

I also moved getSliderType() into input-slider.R since it's the only place that it's used, and mildly improved the error message.

I'm happy to add some tests, but I think the existing tests will need some refactoring, as there are acres of tests that are neither particularly sensitive nor specific.

hadley avatar Jan 28 '21 14:01 hadley

After #3274, I could take a look the input checking tests if you wanted.

hadley avatar Jan 28 '21 18:01 hadley

Suppose someone calls sliderInput() without min/max/value. Before the change, this is the error:

Error in validate_slider_value(min, max, value, "sliderInput") : 
  argument "min" is missing, with no default

After the change this is the error:

Error in dropNulls(list(value, min, max)) : 
  argument "value" is missing, with no default

The old error message isn't perfect, but at least it lets the user know it's coming from the slider. Maybe just force() those arguments at the top of sliderInput()?

wch avatar Feb 12 '21 20:02 wch

Hm, it looks like using force() also results in an uninformative error:

sliderInput("x", "x")
#> Error in force(min) : argument "min" is missing, with no default

But simply evaluating them does result in something useful. For example, if the function has inputId; label; min; max; value at the top, then the error is:

sliderInput("x", "x")
#> Error in sliderInput("x", "x") : 
#>   argument "min" is missing, with no default

force() is more informative when reading the code, but the bare variable names result in a better error message.

wch avatar Feb 12 '21 21:02 wch

I can take a look at that specific case.

Do you want better tests too?

hadley avatar Feb 12 '21 21:02 hadley

Sure, better tests wouldn't hurt. We should also have tests for the most common case, which is when the args are missing.

wch avatar Feb 12 '21 21:02 wch

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 08 '23 16:11 CLAassistant