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
2 answers

Using dplyr within a loop to summarise several data.frame variables

I want to summarise several columns from a data.frame. The grouping and summary was achieved with dplyr, as in the example below. df = data.frame (time = rep(c("day", "night"), 10) , who =rep(c("Paul", "Simon"), each=10) , var1 = runif(20,…
dudu
  • 528
  • 5
  • 13
1
vote
1 answer

dplyr mixing SE and NSE

I am having difficulties in mixing SE and NSE dplyr evaluation in a function. I have a dataset for which I want to divide each column based on the number of occurrences on a different column. Let me write a small function with the functionality I am…
Jordi Vidal
  • 439
  • 1
  • 6
  • 10
1
vote
0 answers

NSE in dplyr: nesting functions inside mutate

While an avid fan of dplyr, I'm still puzzled by some of the NSE issues that inevitably arise when I try to do something of non-trivial complexity. Case in point: finding row sums of a subset of columns. temp <- data.frame(A = 1:3, B = 4:6, C =…
Victor Kostyuk
  • 621
  • 5
  • 16
1
vote
1 answer

Cannot get dplyr filter_ working with dataset and col_name as variables

I banged my head for the last couple of hours but still unable to resolve this ... I am trying to write a R function which takes a dataframe name and a column name as variables and tries to return a dataframe with all distinct values for the column…
KalC
  • 1,530
  • 3
  • 22
  • 33
1
vote
1 answer

Non standard evaluation of by in data.table

I am lost with evaluation of by in data.table. What will be correct way to merge functionality of LJ and LJ2 into one function? LJ <- function(dt_x_, dt_y_, by_) { merge( dt_x_, dt_y_, by = eval(substitute(by_)), all.x =…
Cron Merdek
  • 1,084
  • 1
  • 14
  • 25
1
vote
1 answer

Evaluation order inconsistency with dplyr mutate

I have 2 functions that I use inside a mutate call. One produces per row results as expected while the other repeats the same value for all rows: library(dplyr) df <- data.frame(X = rpois(5, 10), Y = rpois(5,10)) pv <- function(a, b) { …
Daniel Mahler
  • 7,653
  • 5
  • 51
  • 90
1
vote
1 answer

Writing non-standard evaluation syntax with use of magrittr reverse pipe

I'm looking at simple way of simplifying code. Example The sqrt function could be easily applied to the subset of columns below. require(magrittr) mtcars[,-which(colnames(mtcars) %in% c("mpg", "cyl", "drat", "wt", "carb", …
Konrad
  • 17,740
  • 16
  • 106
  • 167
1
vote
1 answer

How to pass '...' argument into an interp() formula within lazyeval

I'm trying to do some parametrised dplyr manipulations. The simplest reproducible example to express the root of the problem is this: # Data test <- data.frame(group = rep(1:5, each = 2), value = as.integer(c(NA, NA, 2, 3, 3, 5,…
pfabri
  • 885
  • 1
  • 9
  • 25
1
vote
2 answers

Replace variable with a string value in R

I would like to pass the name of a column name as a variable to a function. Specifically, given the following code: #------------------------------------------ # Create mock data #------------------------------------------ DF1 = data.frame(colA =…
Stan
  • 905
  • 9
  • 20
1
vote
1 answer

plyr functions and standard evaluation

I would like to wrap plyr functions in my own functions. I want to pass to the function an object and the variable (unquoted) on which apply a cut function. x <- data.frame(time = seq(Sys.Date() - 99, Sys.Date(), 1)) dlply(x, .(week = cut(time, "1…
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
0
votes
1 answer

Is there a better way to use NSE in a function to concatenate dataframe columns?

Background I'm trying to get my grip on meta-programming methods in Advanced R, and not being a programmer by background, it is taking some effort. I am trying to write functions to manipulate dataframe columns without quoting (tidyverse style). …
ScottyJ
  • 945
  • 11
  • 16
0
votes
1 answer

how to use strings and listed strings in nested functions in R

I am still trying to improve my understanding how to create a grid table and reference columns either in the grid table or columns in a nested dataset but running into issues with NSE. I've read the below and tried new things but still couldn't get…
alejandro_hagan
  • 843
  • 2
  • 13
0
votes
0 answers

Unable to provide `aes` parameters that may be missing

I wish to fill out ggrepel::geom_label_repel such that it works with sf-objects. In order to do this, I need to be able to provide aes arguments and ... as well. geom_sf_label_repel <- function(.data, label, …
Mossa
  • 1,656
  • 12
  • 16
0
votes
2 answers

How to quote() non-standard expressions, e.g. 2A-2B?

Or any other non-parseble expression as in igraph::graph_from_literal(1A +--+ 1B). Function call quote(1A-2B) gives Error: unexpected symbol in quote(1A". How to get a result similar to quote(A-B), quote(1-1)?
clp
  • 1,098
  • 5
  • 11
0
votes
3 answers

How to avoid inlining expressions when combining map with tidyeval modelling wrappers

I am trying to combine flexible modelling functions (using tidyeval) and then mapping over data in a nested dataframe (and attempting to learn tidy evaluation along the way). I am running into the problems of inlining expressions with the captured…
Matt
  • 1
  • 1