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

mixing constant and variable column names in dplyr::mutate/transmute

I am trying to change the class of a dataframe column using dplyr. The name of the target column is contained in a variable my_df<-data.frame(colour=c("red","blue","green"), val1=as.character(c(1,12,13)), …
Joost Keuskamp
  • 125
  • 1
  • 9
1
vote
1 answer

dplyr::mutate unquote RHS

I am wondering how to properly UQ string created variable names on the RHS in dplyr methods like mutate. See the error messages I got in comments in the wilcox.test part of this MWE: require(dplyr) dfMain <- data.frame( base = c(rep('A', 5),…
deeenes
  • 4,148
  • 5
  • 43
  • 59
1
vote
1 answer

User-defined function with dplyr - mutate columns based on combining arguments

I am developing a shiny app using the following sample data: library(tidyr) library(dplyr) df <- data.frame(Year = rep(2014:2017, each = 10), ID = rep(1:10, times = 4), Score1 = runif(40), Score2 =…
Dave Gruenewald
  • 5,329
  • 1
  • 23
  • 35
1
vote
1 answer

Using parameters as col names in dplyr

I am struggling to mix referencing column names using parameters and directly from the dataframe. please help me correct the second function to return the same result as the first install.packages("dplyr", version = "0.5.0")` library(dplyr) df <-…
Max888
  • 3,089
  • 24
  • 55
1
vote
1 answer

purrr and ggplot function are not displaying plot (NSE issue)

I suspect this is a problem dealing with NSE. But why isn't these two approaches work and how can I get them to work. temp1 <- function(x){ iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point() + …
student
  • 1,001
  • 2
  • 12
  • 24
1
vote
1 answer

How can you give a character name to a mutate result in dplyr 0.7.x?

Following from a previous question involving filter, I thought the pattern would be... library(dplyr) library(rlang) conversion_scale_name <- "kph" conversion_scale_ratio <- 1.60934 conversion_scale_sym <- sym(conversion_scale_name) cars %>% …
russellpierce
  • 4,583
  • 2
  • 32
  • 44
1
vote
3 answers

Computing total rank for multiple columns using NSE syntax

My goal is to write a function take_by_rank that can operate on arbitrary selection of numeric columns within a data frame; uses non-standard evaluation like base::subset or dplyr verbs; understands the minus sign naturally, so that -foo means "the…
tonytonov
  • 25,060
  • 16
  • 82
  • 98
1
vote
2 answers

programming with dplyr::arrange in dplyr v.0.7

I am trying to get my head around the new implementations in dplyr with respect to programming and non standard evaluation. So the verb_ functions are replaced by enquo of the argument and then applying !! in the regular verb function. Translating…
Edwin
  • 3,184
  • 1
  • 23
  • 25
1
vote
2 answers

group_by by a vector of characters using tidy evaluation semantics

I used to do it, using group_by_ library(dplyr) group_by <- c('cyl', 'vs') mtcars %>% group_by_(.dots = group_by) %>% summarise(gear = mean(gear)) but now group_by_ is deprecated. I don't know how to do it using the tidy evaluation framework.
danilinares
  • 1,172
  • 1
  • 9
  • 28
1
vote
1 answer

Function not evaluating variables in an expected manner

I'm trying to write an R function to produce a frequency table so I can standardise formatting etc without typing it out repeatedly. The only problem is that I can't get it to evaluate a grouping variable correctly. Here is some code to get a mini…
Nick
  • 799
  • 1
  • 7
  • 18
1
vote
0 answers

Am I using NSE and rlang correctly/reasonably?

I've been reading through programming with dplyr and trying to apply the ideas it describes in my work. I have something that works, but it's unclear to me whether I've done it in the "right" way. Is there something more elegant or concise I could…
rcorty
  • 1,140
  • 1
  • 10
  • 28
1
vote
2 answers

How to access the source data when manipulating/updating fitted models

I'm trying to write some functions to ease refitting multiple models, but find it painful, as R is unable to locate proper data, when it plunges deeper into evaluation tree. Despite an effort was made to store the formula environment inside the…
mjktfw
  • 840
  • 6
  • 14
1
vote
1 answer

How does foreach package scope R Environments when using as.formula, SE dplyr, and lapply?

I have a function where I dynamically build multiple formulas as strings and cast them to a formulas with as.formula. I then call that function in a parallel process using doSNOW and foreach and use those formulas through dplyr::mutate_. When I use…
bigfoot56
  • 71
  • 1
  • 8
1
vote
1 answer

How to build a function with arguments listed as a string vector in R?

How might I write a function that takes a string vector and returns a function with arguments having the names of the string vector? In addition, I'd like to use my string of arguments to make a list or data.frame named with the input columns inside…
wdkrnls
  • 4,548
  • 7
  • 36
  • 64
1
vote
1 answer

Correct syntax for using special function call in call

I'm interested in making use of a special call within call/eval as in the code: eval(call("mean", c(2,3))) which will correctly produce result 2.5. Now, I would like to use the same syntax with a special call. Example: + Call: eval(call("`+`",…
Konrad
  • 17,740
  • 16
  • 106
  • 167