0

This works (so nice in Shiny):

filter_(mtcars, "mpg > 24")

and this:

filter_(mtcars, "mpg > 24", "disp > 75")

Is there a way to make this work as well?

filter_(mtcars, "mpg > 24, disp > 75")

This works as an alternative

filter_(mtcars, "mpg > 24 & disp > 75")

Edit 1

This works but is there a better way?

filter_(mtcars,unlist(strsplit("mpg > 24, disp > 75", ",")))

EDIT 2

The previous edit may not work as expected. It is equivalent to the following:

filter_(mtcars,c("mpg > 24", "disp > 75"))

Where the 2nd condition is ignored

  mpg cyl  disp  hp drat   wt qsec vs am gear carb
  1 24.4   4 146.7  62 3.69 3.19 20.0  1  0    4    2
  2 32.4   4  78.7  66 4.08 2.20 19.5  1  1    4    1
  3 30.4   4  75.7  52 4.93 1.61 18.5  1  1    4    2
  4 33.9   4  71.1  65 4.22 1.83 19.9  1  1    4    1
  5 27.3   4  79.0  66 4.08 1.94 18.9  1  1    4    1
  6 26.0   4 120.3  91 4.43 2.14 16.7  0  1    5    2
  7 30.4   4  95.1 113 3.77 1.51 16.9  1  1    5    2
Vincent
  • 5,063
  • 3
  • 28
  • 39
  • 3
    The alternative (`&`) is way better IMHO. More general too as you can use other operators like `|` for example. – flodel Oct 29 '14 at 18:33
  • 3
    How about `filter_(mtcars, c("mpg > 24", "disp > 75"))`? What exactly is the problem that you are facing? – MrFlick Oct 29 '14 at 18:33
  • You are probably right @flodel but I would like to have the option that input is in 'standard' dplyr notation – Vincent Oct 29 '14 at 18:39
  • In a locally run Shiny app users can input conditions to filter the data. (&) works fine but a user that knows dplyr may try "mpg > 25 , disp > 75". See my edit about using c(). Doesn't seem to give the same result – Vincent Oct 29 '14 at 18:41
  • 1
    Try `do.call(filter_, c(list(mtcars),unlist(strsplit("mpg > 24, disp > 75", ","))))`. I cannot test as you did not provide a reproducible example. – flodel Oct 29 '14 at 18:58
  • That seems to work. Thanks @flodel! Just out of curiosity, why is using mtcars not a reproducible example? It is a default R dataset isn't it? – Vincent Oct 29 '14 at 19:14
  • I can't find `filter_` although I tried loading `shiny` and `dplyr`. Maybe my versions are too old, or I am doing something wrong. – flodel Oct 29 '14 at 19:15
  • Didn't think about that. I'm using dplyr 0.3.0.2. filter_ was added pretty recently I believe – Vincent Oct 29 '14 at 19:22
  • 1
    FWIW try `filter_(mtcars, .dots = c("mpg > 24", "disp > 75"))` – hadley Oct 30 '14 at 14:24
  • Given how the input arrive from the user it would be: filter_(mtcars, .dots = unlist(strsplit("mpg > 24, disp > 75", ","))) correct? – Vincent Oct 30 '14 at 15:13

2 Answers2

0

So my assumption here is that you are getting the filtering parameters from a textInput so they come through as text. Here is a way that works:

library(dplyr)
Args<-c("mpg>24, disp>75")
eval( parse( text=paste("filter(mtcars,",Args,")")    ))

   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
2 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
3 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
4 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
5 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
6 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2

So in this case Args is the input from the user. I used paste to put together a complete filter function as a text string. Finally the eval(parse (..)) bit just evaluates the string as if it were a normal function. Note that this uses filter instead of filter_

This answer is based on the accepted answer to this question:

Use character string as function argument

Community
  • 1
  • 1
John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Thanks for the input John. I have used this approach with 'subset' before. Works fine but I am trying to cut-down on my intake of eval-parse. When run on a server I assume that the security risks of hadley's or flodel's approach are lower in comparison – Vincent Oct 30 '14 at 15:19
0

For completeness, I'm going to throw out the (perhaps trivial) solution of using gsub to replace commas with ampersands in the string:

filter_(mtcars, gsub(',', '&', "mpg > 24, disp > 75"))

   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
2 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
3 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
4 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
5 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
6 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2

This generates the same output you'd expect if you entered that condition into filter as a bare expression, works just fine if there are no commas or if the user chooses to use &, and doesn't introduce involve the potential risks of using eval on arbitrary expressions.

divibisan
  • 11,659
  • 11
  • 40
  • 58