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
3
votes
1 answer

function for dplyr with argument that defaults to "."

Let's say I want to sum over all columns in a tibble to create a new column called "total". I could do: library(tibble) library(dplyr) set.seed(42) N <- 10 Df <- tibble(p_1 = rnorm(N), p_2 = rnorm(N), q_1 = rnorm(N), …
mjandrews
  • 2,392
  • 4
  • 22
  • 39
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
2 answers

Input dplyr::filter() as argument to function

Input dplyr::filter to function How to create a function which takes any dplyr::filter as input, and returns the number of rows satisfying the filter? I have tried something like this, which does not work: library(tidyverse) filter_function <-…
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
3
votes
1 answer

Dplyr programming pattern for mutate

I have been using a standard pattern when using dplyr mutate inside functions. Here is a toy example(only to prove a point): myFunction = function(colname) { dots <- setNames(list(lazyeval::interp(~ifelse(x>25, x*10, x/10), x =…
Sid
  • 420
  • 1
  • 6
  • 11
3
votes
2 answers

Using dplyr group_by in a function

I am trying to use dplyr's group_by in a local function, example: testFunction <- function(df, x) { df %>% group_by(x) %>% summarize(mean.Petal.Width = mean(Petal.Width)) } testFunction(iris, Species) and I get an error "... unknown variable to…
RoseS
  • 185
  • 2
  • 12
3
votes
1 answer

How to correctly use dplyr verbs inside a function definition in r?

I want to use filter and summarise from dplyr inside my function. Without a function it works like following: library(dplyr) > Orange %>% + filter(Tree==1) %>% + summarise(age_max = max(age)) age_max 1 1582 I want to do the same…
umair durrani
  • 5,597
  • 8
  • 45
  • 85
3
votes
1 answer

Moving mean as a function in dplyr

I'd like to create a function that can calculate the moving mean for a variable number of last observations and different variables. Take this as mock data: df = expand.grid(site = factor(seq(10)), year = 2000:2004, …
1053Inator
  • 302
  • 1
  • 15
3
votes
2 answers

Dplyr non standard evaluation with function name passed as a string

While working with dplyr pipeline, I would like to pass a function to mutate using NSE with the function name being passed from a vector. Example Given vector of two function names: funs <- c("sum", "mean") I would like to use first value to obtain…
Konrad
  • 17,740
  • 16
  • 106
  • 167
3
votes
2 answers

Can someone explain the behaviour of named vectors in manual ggplot scales?

When using a named vector to set colours in a manual scale in ggplot2, if you use a variable as one of the names, the colour will not appear in the final plot. library(ggplot2) first_species <- 'setosa' colours <- c(first_species = 'black', …
Mhairi McNeill
  • 1,951
  • 11
  • 20
3
votes
1 answer

Call to ggplot in a function with NSE

The idea is to patch a call to ggplot in a function. The example: library(dplyr) library(ggplot2) library(lazyeval) df <- data.frame(A=letters[1:10], B=2:11, C=3:12)) func <- function(name, dat=df) { output <- dat %>% select_(~A,name) %>% …
remi
  • 781
  • 2
  • 13
  • 22
2
votes
1 answer

Create a new column using non-standard evaluation in R

I am working with non-standard evaluation in R. I have done group by and summarize in a dataframe using rlang as explained here To follow the same example, I am left with a dataframe that looks like this: x y q p 1 0 7 325.8 Inf 2 1 7 317.1…
2
votes
3 answers

How to create new variables based on an external named list/vector of computations dplyr

Imagine I want to do the following operation: library(dplyr) mtcars %>% group_by(x = am) %>% summarize(y = sum(vs), q = sum(mpg), p = sum(mpg/vs)) which yields: #> # A tibble: 2 × 4 #> x y q …
2
votes
2 answers

How can I mutate using across and a dynamically-generated list of functions

I have a data frame which encapsulates a number of statistics for an exam, tracked over different years and groups. I would like to construct a function which adds new columns giving the change in these statistics for each group from a dynamically…
Stephen Morgan
  • 327
  • 1
  • 9
2
votes
1 answer

How do I quote a newly created variable in a function to a helper function?

Question What is the proper way to quote a parameter in a function that will be used to create a new variable that will be passed to another function? Background Ultimate goal is to create labels in a dataframe for a treemap with 2 levels of…
ScottyJ
  • 945
  • 11
  • 16
2
votes
1 answer

Passing variables to ggpubr from function call

I am looking to wrap the following formula into a function for easier end use: df %>% group_by(a, b) %>% summarize(avg=mean(c)) %>% ggline(x="a", y="avg", color='b') however the following returns the error "Error in is.factor(x) : object 'b'…