0

I want to filter iris using the %in% operator based on the values of the vector fl <- c("virginica","setosa").

I other words, something like

tbl_df(iris) %>% 
  filter_('Species %in% c("virginica","setosa")')

but since this code will go in a shiny app and fl will be generated dynamically, I need to pass fl into my previous script rather than manually typing the values.

Dambo
  • 3,318
  • 5
  • 30
  • 79

1 Answers1

0

(Edit: I'm undeleting this based on the comment that, when pulling from SQL, you have to use the filter_ function or it breaks. That's a separate issue and perhaps a bug in your code, but perhaps this work-around let's you get done what you need. @alistaire's comment works better, but if you need to choose a column dynamically like I initially mis-inferred, then you'll need something like this. Lacking that, this is not an answer so much as an expansion.)

As shown here: https://stackoverflow.com/a/26509961/3358272, instead using %in%:

cn <- "Species"
fl <- c("virginica", "setosa")
filt <- lazyeval::interp(~ col %in% fl, col = as.name(cn))
tbl_df(iris) %>% filter_(filt)
# Source: local data frame [100 x 5]
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#           <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
# 1           5.1         3.5          1.4         0.2  setosa
# 2           4.9         3.0          1.4         0.2  setosa
# 3           4.7         3.2          1.3         0.2  setosa
# 4           4.6         3.1          1.5         0.2  setosa
# 5           5.0         3.6          1.4         0.2  setosa
# ..          ...         ...          ...         ...     ...
Community
  • 1
  • 1
r2evans
  • 141,215
  • 6
  • 77
  • 149