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
1
vote
1 answer

Custom functions: How to refer to another column using sym() and !! (bang bang) without using funs() in dplyr 0.8.0?

In dplyr 0.8.0, the wrapper funs() (used within mutate_at() or summarise_at()) is soft-deprecated. The new recommended syntax is to use ~sum(.) instead of funs(sum(.)), which is the "old style" I would have previously used. My aim is to translate…
cyun
  • 95
  • 1
  • 2
  • 7
1
vote
0 answers

Using dplyr within a function throws error despite enquo()

I tried to translate a working dplyr-code into a function, but fail because of enquo()/!!. The function is for a dataset with 160 variables and is applied repeatedly over a changing number of variables (sometimes over 3, like in the reprex,…
tobin_lab
  • 185
  • 1
  • 14
1
vote
2 answers

Dynamic select expression in function

I am trying to write a function that will convert this data frame library(dplyr) library(rlang) library(purrr) df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4), S1=rep(c("a","b"),length.out=10),PR1=rep(c(3,7),length.out=10), …
see24
  • 1,097
  • 10
  • 21
1
vote
1 answer

Select all but some columns in dplyr with unquoting

I want to use select(data, -var) but keep var flexible. This works if var is of length one using select(data, -!!var) (with var <- quo(var)) , but it does not if it is of length > 1. Any ideas why? library(dplyr) #> Warning: package 'dplyr' was…
Lorenz Walthert
  • 4,414
  • 1
  • 18
  • 24
1
vote
3 answers

R: Substitute variables bound in all parent environments

The function base::substitute(expr, env), as per its documentation page, returns the parse tree for the (unevaluated) expression expr, substituting any variables bound in env. I am looking for a way of substituting any variables bound not in one…
nbenn
  • 591
  • 4
  • 12
1
vote
2 answers

Scale value inside of aes_string()

I want to scale my y-var by multiplying it by a number, say 10, in ggplot. The problem is this is in a Shiny app and the variable must be passed as a character string, i.e. input$variable. How can I multiply one of the variables in aes_string() the…
conv3d
  • 2,668
  • 6
  • 25
  • 45
1
vote
2 answers

Get object and indices from subsetting call

Basically, I would like to create an R function separate_call that gets an argument like x[ind] and returns x and ind (so, from the parent environment): x <- 1:10 ind <- 2:3 separate_call(x[ind]) ## should return `list(1:10, 2:3)` I know that I…
F. Privé
  • 11,423
  • 2
  • 27
  • 78
1
vote
1 answer

Reverse code function, dealing with quotation marks

I'm looking for some help in defining a function to reverse code an ordinal variable. I'm using survey data in which theoretically related variables are coded in different directions i.e, perceptions of a concept are measured with several different…
dj20b22
  • 73
  • 6
1
vote
1 answer

Using strings as arguments in custom dplyr function using non-standard evaluation

I'm trying to write a function that takes a metric and computes relative difference from the first value of that metric with dplyr functions and non-standard evaluation. Following these instructions…
M4RT1NK4
  • 433
  • 1
  • 4
  • 9
1
vote
1 answer

combining mutate_at with case_when

I have to transform the following code: labels = 'x' dataX <- data.frame(x = colors()) special <- sample(colors(),5) dataX <- dataX %>% mutate("names2" := dplyr::case_when(UQ(sym(labels)) %in% special ~ UQ(sym(labels)))) Into some code which will…
witek
  • 984
  • 1
  • 8
  • 25
1
vote
1 answer

Using quos with empty argument in R NSE

I'm trying to figure out how I can use optional arguments in an NSE function in my tidyverse workflow. This is a little toy function that I'd like to be able to build upon. I want to be able to operate on a grouped data frame; in this example, I'd…
camille
  • 16,432
  • 18
  • 38
  • 60
1
vote
2 answers

Moving from deprecated summarize_ to new summarize in dplyr

I have a function that calculates the means of a grouped database for a column which is chosen based on the content of a variable VarName. The current function uses dplyr::summarize_, but now I see this is deprecated, and I want to replace it before…
iod
  • 7,412
  • 2
  • 17
  • 36
1
vote
2 answers

How to pass formulas or quosures in dplyr verbs R as arguments

I am not able to use formulas or quosures in filter expressions in dplyr. a_table <- data_frame(key = rep(letters[1:2], each = 2), value = replace(runif(4), mod(1:4, 2) == 1, NA)) a_cond <- quo(not(is.na(value))) filter(a_table,…
Diego-MX
  • 2,279
  • 2
  • 20
  • 35
1
vote
3 answers

Wrapping function around purrr::pmap using column names

I try to write a simple function wrapping around the purrr::pmap_dbl() function. I have the following data: df <- data.frame( col1 = 1:5, col2 = 2:6, col3 = 3:7 ) And the following function: addfn <- function(x,…
mharinga
  • 1,708
  • 10
  • 23
1
vote
1 answer

Pass multiple arguments to ddply

I am attempting to create a function which takes a list as input, and returns a summarised data frame. However, after trying multiple ways, I am unable to pass a list to the function for the aggregation. So far I have the following, but it is…
Abel Riboulot
  • 158
  • 1
  • 8