2
votes

I'm filtering a dataset using dplyr's filter_ function in a Shiny app. I'm surrounding the evaluation with a try() so that I can catch errors. This works as expected in some situation like, for example, with an empty filter string. But if the filter string includes a variable that is not in the data.frame Shiny crashes entirely.

Take the following code as an example -- if the filter string is just "" Shiny returns an error (as expected). But if you include "z==1" where z does not exist then the app crashes (not expected!).

library(shiny)
library(dplyr)
testapp <- shinyApp(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    plotOutput('plot'),
    uiOutput("whyfail")
  ),

  server = function(input, output) {
    output$plot <- renderPlot({ hist(runif(input$n)) })


    output$whyfail<-renderUI({
      input$n
      dat<-data.frame(a=LETTERS[1:5], b=1:5)
      xx<-try(filter_(dat, ""), silent = TRUE)# returns an error as expected
      #xx<-try(filter_(dat, "z==1"), silent = TRUE) # crashes Shiny
      HTML(paste(attr(xx, "class"), collapse = '<br/>'))
    })



  }
)
runApp(testapp)

Update

My version of dplyr is 0.4.2 and shiny is 0.12.1. Two updates: first, if I run in base R GUI (not RStudio) I don't see the crash. Also, if I don't load dplyr at all the code runs fine with no crash. I am surprised that the code runs given that I would have expected filter_() to depend on dplyr and Shiny does not seem to import dplyr or filter_()).

But I need dplyr and I'd prefer to continue using RStudio so any help would be appreciated.

Update 2

I'm using RStudio in Windows 7 v 0.99.446

Update 3

Not sure the best place to put this. But this appears to be a genuine bug and is being tackled by RStudio as you can see in this link https://github.com/rstudio/shiny/issues/879

1
What do you mean by crashing? - hadley
If you actually print the error, it shows Error in try(filter_(dat, ""), silent = TRUE) : could not find function "filter_". I suspect you have not given us a fully reproducible example - hadley
I added the library calls to the top, is this what you meant by fully reproducible? I'm surprised that this code will run without explicitly loading dplyr but I need dplyr and get the error with dplyr. Is there something else I'm missing to make this reproducible -- a colleague reproduced the error with this code. By crash I mean that the Shiny window disappears immediately and returns to console. If Shiny is running in a browser it gets greyed out. - ZRoss

1 Answers

0
votes

Turns out this was an issue with the package Rcpp and RStudio has now resolved it. You can see the discussion and resolution here.