rlang

Parse and Evaluate Column of String Expressions in R?

丶灬走出姿态 提交于 2021-01-27 11:39:58
问题 How can I parse and evaluate a column of string expressions in R as part of a pipeline? In the example below, I produce my desired column, evaluated . But I know this isn't the right approach. I tried taking a tidyverse approach. But I'm just very confused. library(tidyverse) df <- tibble(name = LETTERS[1:3], to_evaluate = c("1-1+1", "iter+iter", "4*iter-1"), evaluated = NA) iter = 1 for (i in 1:nrow(df)) { df[i,"evaluated"] <- eval(parse(text=df$to_evaluate[[i]])) } print(df) # # A tibble: 3

rlang: Error: Can't convert a function to a string

霸气de小男生 提交于 2021-01-27 10:47:30
问题 I created a function to convert a function name to string. Version 1 func_to_string1 works well, but version 2 func_to_string2 doesn't work. func_to_string1 <- function(fun){ print(rlang::as_string(rlang::enexpr(fun))) } func_to_string2 <- function(fun){ is.function(fun) print(rlang::as_string(rlang::enexpr(fun))) } func_to_string1 works: > func_to_string1(sum) [1] "sum" func_to_string2 doesn't work. > func_to_string2(sum) Error: Can't convert a primitive function to a string Call `rlang:

What is the “embracing operator” `{{ }}`?

前提是你 提交于 2021-01-21 11:59:12
问题 I just came across the "embracing operator" {{ }} in section 2.2.3 of the tidyverse style guide. What does the embracing operator {{ }} do in R? 回答1: It's called curly-curly operator (see ?"{{}}" ). It's useful when passing an argument that has to be substituted in place before being evaluated in another context. See this simple example (although a bit awkward as we could simple quote the "cyl" when calling the function here): library(dplyr) # does not work get_var <- function(data, column) {

What is the “embracing operator” `{{ }}`?

我的梦境 提交于 2021-01-21 11:58:29
问题 I just came across the "embracing operator" {{ }} in section 2.2.3 of the tidyverse style guide. What does the embracing operator {{ }} do in R? 回答1: It's called curly-curly operator (see ?"{{}}" ). It's useful when passing an argument that has to be substituted in place before being evaluated in another context. See this simple example (although a bit awkward as we could simple quote the "cyl" when calling the function here): library(dplyr) # does not work get_var <- function(data, column) {

Deparse, substitute with three-dots arguments

天涯浪子 提交于 2021-01-21 09:13:36
问题 Let consider a typical deparse(substitute( R call: f1 <-function(u,x,y) {print(deparse(substitute(x)))} varU='vu' varX='vx' varY='vy' f1(u=varU,x=varX,y=varY) That results in [1] "varX" which is what we expect and we want. Then, comes the trouble, I try to get a similar behaviour using the ... arguments i.e. f2 <- function(...) { l <- list(...) x=l$x print(deparse(substitute(x))) ### this cannot work but I would like something like that } That, not surprisingly, does not work : f2(u=varU,x

Deparse, substitute with three-dots arguments

谁都会走 提交于 2021-01-21 09:12:35
问题 Let consider a typical deparse(substitute( R call: f1 <-function(u,x,y) {print(deparse(substitute(x)))} varU='vu' varX='vx' varY='vy' f1(u=varU,x=varX,y=varY) That results in [1] "varX" which is what we expect and we want. Then, comes the trouble, I try to get a similar behaviour using the ... arguments i.e. f2 <- function(...) { l <- list(...) x=l$x print(deparse(substitute(x))) ### this cannot work but I would like something like that } That, not surprisingly, does not work : f2(u=varU,x

combine formula and tidy eval (plotly)

眉间皱痕 提交于 2020-12-31 07:57:03
问题 I'm struggling to understand this. The below lets me filter my data.frame in a "tidy" way, and draw a plot using plotly. In this case, I'm using plotly's formula-based API to say which columns of a data frame to use: library(plotly) tidy_filter = function(data, x) { x = enquo(x) filter(data, !!x > 5) } mtcars %>% tidy_filter(wt) %>% plot_ly(x = ~wt, y = ~wt) I can wrap this in a single function to get the same result: tidy_ply = function(data, x) { x = enquo(x) data = filter(data, !!x > 5)

combine formula and tidy eval (plotly)

本秂侑毒 提交于 2020-12-31 07:53:00
问题 I'm struggling to understand this. The below lets me filter my data.frame in a "tidy" way, and draw a plot using plotly. In this case, I'm using plotly's formula-based API to say which columns of a data frame to use: library(plotly) tidy_filter = function(data, x) { x = enquo(x) filter(data, !!x > 5) } mtcars %>% tidy_filter(wt) %>% plot_ly(x = ~wt, y = ~wt) I can wrap this in a single function to get the same result: tidy_ply = function(data, x) { x = enquo(x) data = filter(data, !!x > 5)

combine formula and tidy eval (plotly)

五迷三道 提交于 2020-12-31 07:49:13
问题 I'm struggling to understand this. The below lets me filter my data.frame in a "tidy" way, and draw a plot using plotly. In this case, I'm using plotly's formula-based API to say which columns of a data frame to use: library(plotly) tidy_filter = function(data, x) { x = enquo(x) filter(data, !!x > 5) } mtcars %>% tidy_filter(wt) %>% plot_ly(x = ~wt, y = ~wt) I can wrap this in a single function to get the same result: tidy_ply = function(data, x) { x = enquo(x) data = filter(data, !!x > 5)