Questions tagged [nse]

This tag covers questions about non-standard evaluation, which deals with the creation and manipulation of unevaluated expressions. This includes base R functions like call() and substitute(), as well as the more sophisticated tools provided by the rlang package. The latter are often also tagged with "tidyeval".

Non-Standard Evaluation (NSE) is a form of that focuses on the creation and manipulation of unevaluated expressions in . This is in contrast to Standard Evaluation (SE), where each expression encountered by the interpreter is immediately evaluated in its surrounding context. Using NSE tools, programmers can capture an expression and delay its evaluation, thus allowing the expression to reference variables and functions that may not yet exist when the expression is first defined. This is useful for parameterizing function calls, accessing data frame columns, and referencing variables, all with deferred interpretation.

Base NSE functionality in R is substantially extended by the package, which introduces the "immediate evaluation" operator !! and the ability to capture quosures, which consist of expressions along with their surrounding context.

Vignettes

Related tags

238 questions
138
votes
6 answers

How to use a variable to specify column name in ggplot

I have a ggplot command ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) ) inside a function. But I would like to be able to use a parameter of the function to pick out the column to use as colour and group. I.e. I would like…
Theodore Norvell
  • 11,939
  • 6
  • 27
  • 42
47
votes
4 answers

Looping over variables in ggplot

I want to use ggplot to loop over several columns to create multiple plots, but using the placeholder in the for loop changes the behavior of ggplot. If I have this: t <- data.frame(w = c(1, 2, 3, 4), x = c(23,45,23, 34), y = c(23,34,54, 23), z =…
Tom
  • 4,230
  • 5
  • 37
  • 50
47
votes
4 answers

dplyr: How to use group_by inside a function?

I want to use use the dplyr::group_by function inside another function, but I do not know how to pass the arguments to this function. Can someone provide a working example? library(dplyr) data(iris) iris %.% group_by(Species) %.% summarise(n = n())…
31
votes
4 answers

How to dplyr rename a column, by column index?

The following code renames first column in the data set: require(dplyr) mtcars %>% setNames(c("RenamedColumn", names(.)[2:length(names(.))])) Desired results: RenamedColumn cyl disp hp drat wt qsec vs am gear…
Konrad
  • 14,406
  • 15
  • 86
  • 141
30
votes
4 answers

Why is enquo + !! preferable to substitute + eval

In the following example, why should we favour using f1 over f2? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option. library(dplyr) d = data.frame(x = 1:5, y =…
mbiron
  • 3,237
  • 1
  • 12
  • 14
19
votes
2 answers

Where, if anywhere, are the dangers of non-standard evaluation documented?

Many of R's functions with non-standard evaluation, e.g. with, subset, and transform, contain a warning like this: For interactive use this is very effective and nice to read. For programming however, i.e., in one's functions, more care is needed,…
J. Mini
  • 1,288
  • 4
  • 23
18
votes
1 answer

rlang::sym in anonymous functions

I recently notices that rlang::sym doesn't seem to work in anonymous functions and I don't understand why. Here an example, it's pretty clumsy and ugly but I think it illustrates the point require(tidyverse) data <- tibble(x1 = letters[1:3], …
Jonas
  • 1,399
  • 1
  • 13
  • 26
18
votes
3 answers

dplyr::select() with some variables that may not exist in the data frame?

I have a helper function (say foo()) that will be run on various data frames that may or may not contain specified variables. Suppose I have library(dplyr) d1 <- data_frame(taxon=1,model=2,z=3) d2 <- data_frame(taxon=2,pss=4,z=3) The variables I…
Ben Bolker
  • 173,430
  • 21
  • 312
  • 389
15
votes
4 answers

dplyr: select columns by position in NSE

I am trying to create a function that will select columns in a DF based on their position. I will always need the first column and then a subset of the DF. I have 1 object for each subset I need to select. So far I have tried the following: position…
Jordi Vidal
  • 279
  • 1
  • 4
  • 10
13
votes
3 answers

Use of tidyeval based non-standard evaluation in recode in right-hand side of mutate

Consider a tibble where each column is a character vector which can take many values -- let's say "A" through "F". library(tidyverse) sample_df <- tibble(q1 = c("A", "B", "C"), q2 = c("B", "B", "A")) I wish to create a function which takes a…
aaron
  • 305
  • 1
  • 6
13
votes
4 answers

how to rename a variable using a dynamic name and dplyr?

I want to rename a column inside a function with a name passed as an argument for this function. Basically, I have a function produce_data_frame <- function(name) { return(iris) } And I want that this function change the Sepal.length column name…
Malta
  • 1,413
  • 2
  • 13
  • 25
13
votes
3 answers

Tidy evaluation programming with dplyr::case_when

I try to write a simple function wrapping around the dplyr::case_when() function. I read the programming with dplyr documentation on https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html but can't figure out how this works with…
mharinga
  • 1,200
  • 8
  • 20
12
votes
2 answers

Why does !! (bang-bang) combined with as.name() give a different output compared to !! or as.name() alone?

I use a dynamic variable (eg. ID) as a way to reference a column name that will change depending on which gene I am processing at the time. I then use case_when within mutate to create a new column that will have values that depend on the dynamic…
Yuka Takemon
  • 145
  • 8
10
votes
2 answers

R: Using a string as an argument to mutate verb in dplyr

I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to…
Sasha
  • 4,503
  • 8
  • 30
  • 30
10
votes
1 answer

How to pass a named vector to dplyr::select using quosures?

Using the old select_() function, I could pass a named vector into select and change position and column names at once: my_data <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30) my_newnames <- c("newbar" = "bar", "newfoo" = "foo") move_stuff …
crazybilly
  • 2,652
  • 13
  • 38
1
2 3
15 16