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

292 questions
2
votes
2 answers

Problem with a dplyr filter inside a function in R

I have the following data set: dat<-structure(list(X1979 = c(1.26884, 0.75802, 0.35127, -0.0679517, -4.34841, -0.312289, -5.02931, -2.49339, -12.9065, -2.90853, -1.02833, 0.333109, 1.70236, -2.44456, -1.83307, -0.982637, -2.14197, -4.1294,…
Lyndz
  • 347
  • 1
  • 13
  • 30
2
votes
2 answers

R: How to return the exact form of `...` in `fn(...)` without evaluating `...`?

Consider this code fn = function(...) { # print the content of ... without evaluation? } I want the output of fn(a = b) to be "a = b" and fn(a = gn(b), 3~b~a, dd, e = 2 + f, h = hn(jn(cdf))) to be list("a=gn(b)", "3~b~a", "dd", "e=2+f", "h=…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
2
votes
2 answers

using NSE in data.table in R

I have a data.table called client, that has the following columns: "location" "clientID" "gender" "age" "startYear" "ag1" "ag2" There is the age variable and two factorised versions of it: ag1 and ag2. Now i want to…
Nneka
  • 1,764
  • 2
  • 15
  • 39
2
votes
0 answers

R: deparse(substitute(+))

If I use d <- function(x){deparse(substitute(x))} for letters or number all works fine. d(a1) gives "a1", for example. But using special characters results in an error. I want to use d(+) and get "+" as result. From comments: I want "+" == d(+) to…
user11538509
2
votes
1 answer

`getParseData` yields different results in `interactive()` vs Rscript

Please see the below MWE generate_summ_code= function(...) { code = substitute(list(...))[-1] gpd = getParseData(parse(text = deparse(code)), includeText = TRUE) print(gpd) gpd } generate_summ_code(n = n()) If I run that in REPL I get the…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
2
votes
1 answer

Use I string to refer to a variable inside dplyr?

Suppose I have the following data: test_df <- data.frame(a=rnorm(100), b=rnorm(100)) The following works: test_df %>% summarise(y = mean(a)) Now suppose that instead of a i want to pass a character string string_outcome <- "a" # I want to use…
Ignacio
  • 7,646
  • 16
  • 60
  • 113
2
votes
3 answers

understanding rlang: mutate with variable col name and variable column

I'd like to define a function that takes a data.frame and a column name and returns the data.frame in with that column transformed (e.g. to lowercase). When the column name is known in advance, this is simple: diamonds %>% mutate(cut =…
cboettig
  • 12,377
  • 13
  • 70
  • 113
2
votes
3 answers

Non standard evaluation of date condition with filter function

I'm trying to use dplyr::filter() with NSE, but I can't make it work when the filtered variable is as.Date(). Here the function that almost works: foo <- function(d, f, var) { d %>% filter_(paste0(var, ">=", f)) } d <- data.frame(a=1:10,…
RLave
  • 8,144
  • 3
  • 21
  • 37
2
votes
1 answer

when passing df$var to a function, is it possible to get the name of 'var'?

I'm writing a function where I'd like to be able to pass in variables from a data frame as atomic vectors, like df$var (e.g., mtcars$mpg). To keep the example very simple, say the function just returns data.frame(table(df$var)): foo.function <-…
lost
  • 1,483
  • 1
  • 11
  • 19
2
votes
1 answer

Redefine groups in a custom DSL in R

I have a small DSL allowing to group variables by their names: group <- function(.data, ...) { dots <- quos(...) for (i in 1:length(dots)) { in_group <- as.character(dots[[i]])[2] vec <- trimws(unlist(strsplit(in_group, "[+]"))) …
symbolrush
  • 7,123
  • 1
  • 39
  • 67
2
votes
2 answers

How to mix aes_() and arithmetic calculation in ggplot2?

I am trying to adjust x and ymax in the following script in ggplot2 package in R to adjust plotting coordinates of errorbar, however it returns errors. gplot <- function(prd) { ggplot() + …
HSJ
  • 687
  • 6
  • 16
2
votes
2 answers

How to mix non-standard and standard evaluation in arguments to interaction(...) inside ggplot()?

I am seeking for a way to use NSE inside argument (in this case, intersect) in dplyr using rlang package. Data frame: df <- data.frame(time=as.factor(c(10,10,10,10,20,20,20,20,10,10,10,10,20,20,20,20)), …
HSJ
  • 687
  • 6
  • 16
2
votes
1 answer

How to user NSE inside fct_reorder() in ggplot2

I would like to know how to use NSE (Non-Standard Evaluation) expression in fct_reorder() in ggplot2 to replicate charts for different data frames. This is an example of data frame that I use to draw a chart: travel_time_br30 travel_time_br30_int…
HSJ
  • 687
  • 6
  • 16
2
votes
1 answer

Filter based on several columns with similar naming structure

I would like to filter my tibble on several columns with similar names. Specificall I'd like to compare x with x_new, y with y_new and so on but without specifying the explicit name, but by using the structure in columns names. I tried to use…
kath
  • 7,624
  • 17
  • 32
2
votes
1 answer

How to properly address covariates in functions with their names listed into strings?

I have a long list of covariates that I want to store as a string in order to work with it more conveniently in several functions. I think the issue is related to this question, but I can't apply it to cases different to regression formulas. xvar…
jay.sf
  • 60,139
  • 8
  • 53
  • 110