Using rlang Package to Parse Quoted Argument
I'm hoping to string-split a single argument into two arguments and use each in different sections of a function.
Is it possible to do this using quasiquotation (!!) or other rlang functions?
Thanks!
Data:
person <- tibble(id = 1, age = 20)
friends <- tibble(id = c(2, 3, 4, 5), age = c(48, 29, 20, 48))
(unfunctional) Function:
different_age_friends <- function(condition, person = person, friends = friends )
person <- person
friends <- friends
condition <- str_split(condition, " ~ ", simplify = T)
condition_statement <- condition[1]
filter_statement <- condition[2]
if(!!condition_statement)
different_age_friends <- friends %>%
filter(!!filter_statement)
return(return_same_age_friends)
Call:
different_age_friends(condition = "age == 20 ~ age == 48")
Desired Output
id age
2 48
5 48
r tidyverse rlang
add a comment |
I'm hoping to string-split a single argument into two arguments and use each in different sections of a function.
Is it possible to do this using quasiquotation (!!) or other rlang functions?
Thanks!
Data:
person <- tibble(id = 1, age = 20)
friends <- tibble(id = c(2, 3, 4, 5), age = c(48, 29, 20, 48))
(unfunctional) Function:
different_age_friends <- function(condition, person = person, friends = friends )
person <- person
friends <- friends
condition <- str_split(condition, " ~ ", simplify = T)
condition_statement <- condition[1]
filter_statement <- condition[2]
if(!!condition_statement)
different_age_friends <- friends %>%
filter(!!filter_statement)
return(return_same_age_friends)
Call:
different_age_friends(condition = "age == 20 ~ age == 48")
Desired Output
id age
2 48
5 48
r tidyverse rlang
1
This doesn't fix the issue, but you've gotcondition = TRUEwhere you wantcondition == TRUE, or justcondition, to do a comparison
– camille
Nov 13 '18 at 2:32
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition againstperson, and then if true, use the second piece to filterfriends?
– camille
Nov 13 '18 at 2:36
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19
add a comment |
I'm hoping to string-split a single argument into two arguments and use each in different sections of a function.
Is it possible to do this using quasiquotation (!!) or other rlang functions?
Thanks!
Data:
person <- tibble(id = 1, age = 20)
friends <- tibble(id = c(2, 3, 4, 5), age = c(48, 29, 20, 48))
(unfunctional) Function:
different_age_friends <- function(condition, person = person, friends = friends )
person <- person
friends <- friends
condition <- str_split(condition, " ~ ", simplify = T)
condition_statement <- condition[1]
filter_statement <- condition[2]
if(!!condition_statement)
different_age_friends <- friends %>%
filter(!!filter_statement)
return(return_same_age_friends)
Call:
different_age_friends(condition = "age == 20 ~ age == 48")
Desired Output
id age
2 48
5 48
r tidyverse rlang
I'm hoping to string-split a single argument into two arguments and use each in different sections of a function.
Is it possible to do this using quasiquotation (!!) or other rlang functions?
Thanks!
Data:
person <- tibble(id = 1, age = 20)
friends <- tibble(id = c(2, 3, 4, 5), age = c(48, 29, 20, 48))
(unfunctional) Function:
different_age_friends <- function(condition, person = person, friends = friends )
person <- person
friends <- friends
condition <- str_split(condition, " ~ ", simplify = T)
condition_statement <- condition[1]
filter_statement <- condition[2]
if(!!condition_statement)
different_age_friends <- friends %>%
filter(!!filter_statement)
return(return_same_age_friends)
Call:
different_age_friends(condition = "age == 20 ~ age == 48")
Desired Output
id age
2 48
5 48
r tidyverse rlang
r tidyverse rlang
edited Nov 13 '18 at 4:17
asked Nov 12 '18 at 23:45
CFB
544
544
1
This doesn't fix the issue, but you've gotcondition = TRUEwhere you wantcondition == TRUE, or justcondition, to do a comparison
– camille
Nov 13 '18 at 2:32
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition againstperson, and then if true, use the second piece to filterfriends?
– camille
Nov 13 '18 at 2:36
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19
add a comment |
1
This doesn't fix the issue, but you've gotcondition = TRUEwhere you wantcondition == TRUE, or justcondition, to do a comparison
– camille
Nov 13 '18 at 2:32
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition againstperson, and then if true, use the second piece to filterfriends?
– camille
Nov 13 '18 at 2:36
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19
1
1
This doesn't fix the issue, but you've got
condition = TRUE where you want condition == TRUE, or just condition, to do a comparison– camille
Nov 13 '18 at 2:32
This doesn't fix the issue, but you've got
condition = TRUE where you want condition == TRUE, or just condition, to do a comparison– camille
Nov 13 '18 at 2:32
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition against
person, and then if true, use the second piece to filter friends?– camille
Nov 13 '18 at 2:36
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition against
person, and then if true, use the second piece to filter friends?– camille
Nov 13 '18 at 2:36
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19
add a comment |
1 Answer
1
active
oldest
votes
Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.
Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.
different_age_friends <- function(condition, p = person, f = friends)
stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
if( eval(stmts[[1]], p) ) # Effectively: eval(age == 20, person)
f %>% filter(!!stmts[[2]]) # Effectively: friends %>% filter(age == 48)
else
f
different_age_friends(condition = "age == 20 ~ age == 48")
# # A tibble: 2 x 2
# id age
# <dbl> <dbl>
# 1 2 48
# 2 5 48
Minor note:
- You didn't provide a value for
different_age_friendswhen the condition is false. I made an assumption that in this case, the entire friends list is to be returned.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271734%2fusing-rlang-package-to-parse-quoted-argument%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.
Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.
different_age_friends <- function(condition, p = person, f = friends)
stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
if( eval(stmts[[1]], p) ) # Effectively: eval(age == 20, person)
f %>% filter(!!stmts[[2]]) # Effectively: friends %>% filter(age == 48)
else
f
different_age_friends(condition = "age == 20 ~ age == 48")
# # A tibble: 2 x 2
# id age
# <dbl> <dbl>
# 1 2 48
# 2 5 48
Minor note:
- You didn't provide a value for
different_age_friendswhen the condition is false. I made an assumption that in this case, the entire friends list is to be returned.
add a comment |
Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.
Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.
different_age_friends <- function(condition, p = person, f = friends)
stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
if( eval(stmts[[1]], p) ) # Effectively: eval(age == 20, person)
f %>% filter(!!stmts[[2]]) # Effectively: friends %>% filter(age == 48)
else
f
different_age_friends(condition = "age == 20 ~ age == 48")
# # A tibble: 2 x 2
# id age
# <dbl> <dbl>
# 1 2 48
# 2 5 48
Minor note:
- You didn't provide a value for
different_age_friendswhen the condition is false. I made an assumption that in this case, the entire friends list is to be returned.
add a comment |
Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.
Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.
different_age_friends <- function(condition, p = person, f = friends)
stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
if( eval(stmts[[1]], p) ) # Effectively: eval(age == 20, person)
f %>% filter(!!stmts[[2]]) # Effectively: friends %>% filter(age == 48)
else
f
different_age_friends(condition = "age == 20 ~ age == 48")
# # A tibble: 2 x 2
# id age
# <dbl> <dbl>
# 1 2 48
# 2 5 48
Minor note:
- You didn't provide a value for
different_age_friendswhen the condition is false. I made an assumption that in this case, the entire friends list is to be returned.
Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.
Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.
different_age_friends <- function(condition, p = person, f = friends)
stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
if( eval(stmts[[1]], p) ) # Effectively: eval(age == 20, person)
f %>% filter(!!stmts[[2]]) # Effectively: friends %>% filter(age == 48)
else
f
different_age_friends(condition = "age == 20 ~ age == 48")
# # A tibble: 2 x 2
# id age
# <dbl> <dbl>
# 1 2 48
# 2 5 48
Minor note:
- You didn't provide a value for
different_age_friendswhen the condition is false. I made an assumption that in this case, the entire friends list is to be returned.
edited Nov 13 '18 at 4:30
answered Nov 13 '18 at 4:02
Artem Sokolov
4,70221936
4,70221936
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271734%2fusing-rlang-package-to-parse-quoted-argument%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This doesn't fix the issue, but you've got
condition = TRUEwhere you wantcondition == TRUE, or justcondition, to do a comparison– camille
Nov 13 '18 at 2:32
Also, I'm unclear on the difference between the condition and the filter. I don't get what's substantially different between these two types of statements. Are you trying to test the condition against
person, and then if true, use the second piece to filterfriends?– camille
Nov 13 '18 at 2:36
yes exactly, thanks for clarifying!
– CFB
Nov 13 '18 at 4:19