Questions tagged [standard-evaluation]

24 questions
13
votes
2 answers

standard eval with ggplot2 without `aes_string()`

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…
wibeasley
  • 5,000
  • 3
  • 34
  • 62
7
votes
1 answer

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

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"))…
jlhoward
  • 58,004
  • 7
  • 97
  • 140
6
votes
2 answers

How to pass a string to dplyr filter in a function?

I am looking for a way to pass a string as an input to the filter_ function in dplyr package within my own function. I have set up the data frame as follows: df = data.frame( X1 = LETTERS[1:5], X2 = c("apple", "apple", "apple", "banana",…
Jamesm131
  • 145
  • 1
  • 10
4
votes
1 answer

Standard Evaluation with mutate using lazyeval

I'm trying to make my own function wrapping dplyr functions. I have a list of dataframes and I would like to modify the levels of a specified variable with given labels (both should be function's parameters). This is what I tried so far…
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
3
votes
2 answers

Dplyr standard evaluation using a vector of multiple strings with mutate function

I am trying to supply a vector that contains multiple column names to a mutate() call using the dplyr package. Reproducible example below: stackdf <- data.frame(jack = c(1,NA,2,NA,3,NA,4,NA,5,NA), jill =…
Brandon
  • 1,722
  • 1
  • 19
  • 32
3
votes
1 answer

Standard evaluation for tidyr::complete - a function that completes by all non-numeric columns

I want to make a function that would apply tidyr::complete to all non-numeric columns of an R data.frame. Value zero should be inserted to the new value rows. I understand that this requires standard evaluation solution, but I've thus far had no…
Antti
  • 1,263
  • 2
  • 16
  • 28
3
votes
1 answer

non-standard evaluation, confusion in advanced R book

So in Hadley's advanced R book, there is an example of an issue with using substitute, here is an excerpt of the code: subset2 <- function(x, condition) { condition_call <- substitute(condition) r <- eval(condition_call, x, parent.frame()) x[r,…
3
votes
2 answers

Change variable name in dplyr::count using standard evaluation

How do I change the name of the grouping variable in dplyr::count_ when it's used in a standard evaluation way For example if in the final tbl I don't want the var name "Species" but "Type" : iris %>% group_by("Species") %>% …
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
2
votes
2 answers

standard eval with `dplyr::count()`

How can I pass a character vector to dplyr::count(). library(magrittr) variables <- c("cyl", "vs") mtcars %>% dplyr::count_(variables) This works well, but dplyr v0.8 throws the warning: count_() is deprecated. Please use count()…
wibeasley
  • 5,000
  • 3
  • 34
  • 62
2
votes
2 answers

Dplyr Multiple Lags Tidy Eval?

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))…
John
  • 312
  • 1
  • 8
2
votes
1 answer

Standard evaluation and non-standard evaluation in R

I am confused about dplyr functions' arguments and not quite clear about standard evalution (SE) or non-standard evaluation (NSE). I just want to pass a variable to dplyr::arrange() but it failed. However, passing to dplyr::select() works. >…
2
votes
2 answers

Standard Evaluation vs. Non-Standard Evaluation in R package function

It is suggested, that function calls inside R-package functions should preferably use standard evaluation (see here), especially to avoid utils::globalVariables. If I'm using non-standard evaluation with the dplyr package, what would be the…
Daniel
  • 7,252
  • 6
  • 26
  • 38
1
vote
1 answer

How to use non standard evaluation with dollar sign in r

Context I want to use non-standard evaluation with dollar sign in R. I want to customize a function with two parameters. data is the input data frame, var is the name of the variable in the input data frame. The return value is the value…
zhiwei li
  • 1,635
  • 8
  • 26
1
vote
1 answer

Using binary operator in lazyeval call with rlang

Let's say I want to add 1 to every value of a column using dplyr and standard evaluation. I can do : library(dplyr) data <- head(iris) var <- "Sepal.Length" mutate(data, !!rlang::sym(var) := !!quo(`+`(!!rlang::sym(var), 1))) But what if I would…
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
1
vote
1 answer

sparklyr and standard evaluation (SE) based functions

I'm trying to write a function that performs and sdf_pivot() a creates a Spark DataFrame with column names that includes the name of the original variable or column. set.seed(80) df <- data.frame(id = c(1:5), var1 =…
guzu92
  • 737
  • 1
  • 12
  • 28
1
2