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)
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.
I'm using RStudio in Windows 7 v 0.99.446
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
Turns out this was an issue with the package Rcpp and RStudio has now resolved it. You can see the discussion and resolution here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.