Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can cleave() play nicely with shiny's validate(need(...))? #2

Closed
jonmcalder opened this issue Jul 23, 2020 · 4 comments
Closed

Can cleave() play nicely with shiny's validate(need(...))? #2

jonmcalder opened this issue Jul 23, 2020 · 4 comments

Comments

@jonmcalder
Copy link

Thanks for this really nice package @JohnCoene! Simple, yet powerful & effective.

The below app example behaves as expected if cleave() is commented out in the server function, but with cleave() included, the error "state" A regrettable decision is never revealed in the #thing output, and instead only the other 2 output "states" Required input is required 😂 and Good choice are realisable, or both are visible at the same time (when the error state is to be expected).

Is it possible to get cleave() & validate(need(...)) to work together?

library(shiny)
library(sever)

ui <- fluidPage(
  use_sever(),
  fluidRow(
    selectInput("required_input", "Required input",
      choices = c("Red pill", "Blue pill"), 
      multiple = TRUE
    ),
    uiOutput("thing")
  )
)

server <- function(input, output){
  cleave()
  
  # will generate errors
  output$thing <- renderUI({
    validate(need(input$required_input, "Required input is required 😂"))
    if("Red pill" %in% input$required_input) {
      h1("Good choice!")
    } else {
      stop("A regrettable decision.")
    }
  })
  
}

shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents

Created on 2020-07-23 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Africa/Johannesburg         
#>  date     2020-07-23                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                          
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.2)                  
#>  backports     1.1.8      2020-06-17 [1] CRAN (R 4.0.2)                  
#>  callr         3.4.3      2020-03-28 [1] CRAN (R 4.0.1)                  
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 4.0.1)                  
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 4.0.2)                  
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 4.0.1)                  
#>  devtools      2.3.1.9000 2020-07-22 [1] Github (r-lib/devtools@1dca68b) 
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 4.0.2)                  
#>  ellipsis      0.3.1      2020-05-15 [1] CRAN (R 4.0.2)                  
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.1)                  
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 4.0.2)                  
#>  fastmap       1.0.1      2019-10-08 [1] CRAN (R 4.0.2)                  
#>  fs            1.4.2      2020-06-30 [1] CRAN (R 4.0.2)                  
#>  glue          1.4.1      2020-05-13 [1] CRAN (R 4.0.2)                  
#>  highr         0.8        2019-03-20 [1] CRAN (R 4.0.2)                  
#>  htmltools     0.5.0      2020-06-16 [1] CRAN (R 4.0.1)                  
#>  httpuv        1.5.4      2020-06-06 [1] CRAN (R 4.0.1)                  
#>  knitr         1.29       2020-06-23 [1] CRAN (R 4.0.1)                  
#>  later         1.1.0.1    2020-06-05 [1] CRAN (R 4.0.1)                  
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 4.0.2)                  
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 4.0.2)                  
#>  mime          0.9        2020-02-04 [1] CRAN (R 4.0.2)                  
#>  pkgbuild      1.1.0      2020-07-13 [1] CRAN (R 4.0.2)                  
#>  pkgload       1.1.0      2020-05-29 [1] CRAN (R 4.0.1)                  
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.0.2)                  
#>  processx      3.4.3      2020-07-05 [1] CRAN (R 4.0.1)                  
#>  promises      1.1.1      2020-06-09 [1] CRAN (R 4.0.1)                  
#>  ps            1.3.3      2020-05-08 [1] CRAN (R 4.0.2)                  
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 4.0.2)                  
#>  Rcpp          1.0.5      2020-07-06 [1] CRAN (R 4.0.2)                  
#>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)                  
#>  rlang         0.4.7      2020-07-09 [1] CRAN (R 4.0.2)                  
#>  rmarkdown     2.3        2020-06-18 [1] CRAN (R 4.0.1)                  
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 4.0.1)                  
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.1)                  
#>  sever       * 0.0.5.9000 2020-07-23 [1] Github (JohnCoene/sever@859dfc8)
#>  shiny       * 1.5.0      2020-06-23 [1] CRAN (R 4.0.1)                  
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 4.0.2)                  
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.1)                  
#>  testthat      2.3.2      2020-03-02 [1] CRAN (R 4.0.1)                  
#>  usethis       1.6.1      2020-04-29 [1] CRAN (R 4.0.1)                  
#>  withr         2.2.0      2020-04-20 [1] CRAN (R 4.0.2)                  
#>  xfun          0.15       2020-06-21 [1] CRAN (R 4.0.2)                  
#>  xtable        1.8-4      2019-04-21 [1] CRAN (R 4.0.2)                  
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.2)                  
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
@JohnCoene
Copy link
Owner

Hi Jon,

Thank you very much!

I'm afraid that had nothing to do with validate or need, it was buggy regardless 😄

I just pushed a fix. The issue it correctly removes the "cleave" when a new value is set and hides everything underneath error messages on an error. This should make it a lot more robust!

Thanks again for reporting this! Let me know if this does not actually fix it.

@jonmcalder
Copy link
Author

Thanks for the quick response.

That does fix the underlying bug (with "old output" remaining visible instead of being hidden when an error state arises) so that's a definite improvement 🎉

However, it doesn't fully address the issue I was raising, which is that ideally I'd like to be able to have validate() deal with the predictable error states (e.g. those caused by user error or omission such as missing input values), and have cleave() deal with other errors that may surface later while processing a reactive or output.

Keeping with my example app above, this means that the error message A regrettable decision. is never shown, and instead the validate(need(...)) message is shown for any error.

Put another way - when "Blue pill" is selected (on it's own), then I'd expect to see the A regrettable decision. error message, but instead I see the message defined in validate(need(...))

@JohnCoene
Copy link
Owner

Aaaaah, yes indeed, sorry!

I think this time I have it haha. Shiny raises those as "silent errors" javascript-side. I have added an argument to the cleave function that allows controlling whether you want to cleave those or not. It defaults to FALSE = the behaviour you want.

Let me know if this this what you were after, I would not be surprised I misunderstood again haha

@jonmcalder
Copy link
Author

Excellent! You nailed it 👍

And yes, I think that's a good default - assuming that I'm not alone in having relied heavily on validate() before venturing into the use of {sever} & cleave().

Sorry for not trying to diagnose further or attempt a PR - my javascript ability is too limited at this stage and I had no idea how shiny handled validation errors on the javascript side of things.

When raising the issue I did wonder whether maybe I was just wrong to assume these could be used together and should instead just be cleaving all errors (cos that could might be a reasonable approach for some apps), but I do think they are more powerful when used together - allowing for "validating" of expected errors and "cleaving" of unexpected errors.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants