5

I have this:

cond <- "cyl == 4"
mtcars %>% filter_(cond)

which throws warning messege:

filter_() is deprecated. Please use filter() instead

The problem is that I have no idea how to do the same thing with filter() function.

I'm using quoting and unquoting functions with select/mutate, but it seems they only work on columns. How to quote the logical condition?

3
  • Where are you getting the condition from? You should generally not attempt to perform such operations using strings, R has better ways of performing dynamic operations. Of course you can parse such a string as shown in Ronak’s answer but this is conceptually convoluted and has implications for performance and security. Commented Mar 18, 2020 at 10:46
  • it's for my own usage. Trying to build report from data with some basic set of instructions - from an excel file for start. More for learning purposes. But I'm sure interested in other ways of collecting instructions if you can give me some links Commented Mar 18, 2020 at 11:16
  • The general way for dealing with this is to create expressions, see the tidyeval tutorial. However, your described use-case probably benefits from using strings in the way you’re doing it, so carry on. Commented Mar 18, 2020 at 12:12

1 Answer 1

6

We can use rlang::parse_expr with eval

library(dplyr)
mtcars %>% filter(eval(rlang::parse_expr(cond)))

#    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#1  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#2  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#3  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#4  32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#5  30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#6  33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#7  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#8  27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#9  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#10 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#11 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Or using eval and parse

mtcars %>% filter(eval(parse(text = cond)))
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. What about when I have 2 conditions, lets say i want to arrange result by gear and carb, so my string is "gear, carb". How to use this string in arrange function to get the same as with mtcars %>% arrange(gear, carb)
@h1427096 Actually, this isn't the right way to pass parameters. You can use arrange_at here. Assume you have string <- "gear,carb" you can then do mtcars %>% arrange_at(strsplit(string, ",")[[1]])
thanks, but I have also formulas in my strings, like: "wt > 5, hp" (sort by hp, but list those with wt > 5 at the end). Maybe I should open another question for this one. Right now I arrange conditions one by one backwards.
Yes, probably better to open a new question explaining detailed possibilities string can take and how you want to process them.
already got the answer. !!!rlang::parse_exprs(expr) did the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.