standard-evaluation

R - Defining a function which recognises arguments not as objects, but as being part of the call

会有一股神秘感。 提交于 2021-02-11 16:46:16
问题 I'm trying to define a function which returns a graphical object in R. The idea is that I can then call this function with different arguments multiple times using an for loop or lapply function, then plotting the list of grobs in gridExtra::grid.arrange . However, I did not get that far yet. I'm having trouble with r recognising the arguments as being part of the call. I've made some code to show you my problem. I have tried quoting and unquoting the arguments, using unqoute() in the

Dplyr Multiple Lags Tidy Eval?

Deadly 提交于 2020-01-04 05:55:24
问题 I am trying to make multiple lags using the least amount of code possible in dplyr, while sticking to tidy eval. The following Standard Evaluation (SE) code works: #if(!require(dplyr)) install.packages("dplyr"); library(dplyr) a=as_tibble(c(1:100)) lags=3 lag_prefix=paste0("L", 1:lags, ".y") multi_lag=setNames(paste("lag(.,", 1:lags, ")"), lag_prefix) a %>% mutate_at(vars(value), funs_(multi_lag)) #final line # A tibble: 100 x 4 value L1.y L2.y L3.y <int> <int> <int> <int> 1 1 NA NA NA 2 2 1

standard eval with ggplot2 without `aes_string()`

こ雲淡風輕ζ 提交于 2019-12-19 07:36:05
问题 I'd like to pass a quoted string to a function that calls ggplot2. library(magrittr); library(ggplot2) g1 <- function( variable ) { ggplot(mtcars, aes_string("wt", variable, size="carb")) + geom_point() } g1("mpg") This works well, but the v3.1.0 documentation advocates quasiquotation and the NSE aes() . All these functions are soft-deprecated. Please use tidy evaluation idioms instead (see the quasiquotation section in aes() documentation). But the aes() examples use NSE ( ie , g1(mpg)

standard eval with ggplot2 without `aes_string()`

那年仲夏 提交于 2019-12-19 07:36:03
问题 I'd like to pass a quoted string to a function that calls ggplot2. library(magrittr); library(ggplot2) g1 <- function( variable ) { ggplot(mtcars, aes_string("wt", variable, size="carb")) + geom_point() } g1("mpg") This works well, but the v3.1.0 documentation advocates quasiquotation and the NSE aes() . All these functions are soft-deprecated. Please use tidy evaluation idioms instead (see the quasiquotation section in aes() documentation). But the aes() examples use NSE ( ie , g1(mpg)

Scoping of variables in aes(…) inside a function in ggplot

大憨熊 提交于 2019-11-27 23:11:21
Consider this use of ggplot(...) inside a function. x <- seq(1,10,by=0.1) df <- data.frame(x,y1=x, y2=cos(2*x)/(1+x)) library(ggplot2) gg.fun <- function(){ i=2 plot(ggplot(df,aes(x=x,y=df[,i]))+geom_line()) } if(exists("i")) remove(i) gg.fun() # Error in `[.data.frame`(df, , i) : object 'i' not found i=3 gg.fun() # plots df[,3] vs. x It looks like ggplot does not recognize the variable i defined inside the function, but does recognize i if it is defined in the global environment. Why is that? Note that this gives the expected result. gg.new <- function(){ i=2 plot(ggplot(data.frame(x=df$x,y

Scoping of variables in aes(…) inside a function in ggplot

 ̄綄美尐妖づ 提交于 2019-11-26 23:16:43
问题 Consider this use of ggplot(...) inside a function. x <- seq(1,10,by=0.1) df <- data.frame(x,y1=x, y2=cos(2*x)/(1+x)) library(ggplot2) gg.fun <- function(){ i=2 plot(ggplot(df,aes(x=x,y=df[,i]))+geom_line()) } if(exists("i")) remove(i) gg.fun() # Error in `[.data.frame`(df, , i) : object 'i' not found i=3 gg.fun() # plots df[,3] vs. x It looks like ggplot does not recognize the variable i defined inside the function, but does recognize i if it is defined in the global environment. Why is that