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

dplyr in package creation: group_by with string without group_by_

I am currently developing a package in which I try to implement a function that internally uses dplyr::group_by. However, upon package checking (devtools CRAN checks), I get a NOTE stating that no visible binding for global variable Levels. foo <-…
Dominique Makowski
  • 1,511
  • 1
  • 13
  • 30
0
votes
1 answer

Function issue. Tidyeval filtering

What is wrong here? This works: iris %>% filter(Species == "setosa") %>% summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width)) and produces: msl msw 1 5.006 0.246 But this function doesn't work: means <- function(data, value){ …
william3031
  • 1,653
  • 1
  • 18
  • 39
0
votes
2 answers

error when using NSE (in dplyr) : object 'value' not found

I'm trying to get familiar with using NSE in my code where warranted. Let's say I have pairs of columns and want to generate a new string variable for each pair indicating whether the values in that pair are the…
lost
  • 1,483
  • 1
  • 11
  • 19
0
votes
2 answers

Dplyr Non Standard Evaluation -- Help Needed

I am making my first baby steps with non standard evaluation (NSE) in dplyr. Consider the following snippet: it takes a tibble, sorts it according to the values inside a column and replaces the n-k lower values with "Other". See for…
larry77
  • 1,309
  • 14
  • 29
0
votes
1 answer

dplyr: create new column with values from other specified columns

I have a tibble: library(tibble) library(dplyr) ( data <- tibble( a = 1:3, b = 4:6, mycol = c('a', 'b', 'a') ) ) #> # A tibble: 3 x 3 #> a b mycol #> #> 1 1 4 a #> 2 2 5 b #> 3 …
Greg
  • 487
  • 5
  • 15
0
votes
1 answer

Weird dplyr+NSE: using NSE in custom function inside mutate

I stumbled over an issue when using NSE in a custom function inside dplyr::mutate. Consider the following code: require(tidyverse) f <- function(var) { varname <- deparse(substitute(var)) v1 <- as.name(sprintf("%s.Width", varname)) v2 …
Jack
  • 195
  • 2
  • 10
0
votes
1 answer

Clean data using function that calls variable names

I'm trying to compose a function that cleans my variables, replacing "*" with "1" and NAs with "0". I can do this easily with a ifelse, but I wanted it to be clean and use functional programming, but I clearly am not there yet... An example database…
GVianaF
  • 59
  • 7
0
votes
1 answer

Why do I need to wrap function returning a character vector into c() when passing to tidyr ... argument?

See example: df <- data.frame(month=rep(1:3,2), student=rep(c("Amy", "Bob"), each=3), A=c(9, 7, 6, 8, 6, 9), B=c(6, 7, 8, 5, 6, 7)) cnames<-function(){c(month="month",student="student")} When…
witek
  • 984
  • 1
  • 8
  • 25
0
votes
1 answer

Using aes_ instead of aes_string for ggplot

I'm trying to loop through a subset of columns of the data frame btest (data below) and plot some data. I still do not quite understand the tidyeval system, though it seems this is not fully implemented in ggplot2 yet anyway? I can do this using…
Brandon
  • 1,722
  • 1
  • 19
  • 32
0
votes
1 answer

dplyr evaluation: select() vs. mutate()

Can anyone explain why the dplyr approach that works with select() does not work with mutate()? Minimal Working Example: data <- as.tibble(cbind(c(1,2,3,4),c(5,6,7,8))) func <- function(data, var){ data %>% select(!!var) } func2 <- function(data,…
PaoloCrosetto
  • 600
  • 1
  • 7
  • 16
0
votes
1 answer

Dplyr conditional column ifelse with vector input

I am trying to use dplyr's new NSE language approach to create a conditional mutate, using a vector input. Where I am having trouble is setting the column equal to itself, see mwe below: df <- data.frame("Name" = c(rep("A", 3), rep("B", 3), rep("C",…
Nick
  • 3,262
  • 30
  • 44
0
votes
2 answers

Using 'mutate' with passed in parameter name to create new column

I'd like to add a new data column and name the column using a parameter I pass in. I'm used to using dplyr's mutate line-by-line and other methods that directly hard-code the name, but I'm unsure where to start here. How would I use mutate or…
dad
  • 1,335
  • 9
  • 28
0
votes
1 answer

Non standard evaluation in R for loop: Unquoted input variable in a function containing dplyr summarise always returns NA, but filter function works

SHORT SUMMARY dplyr unquoting is failing as an argument of function summarise where the quoted object is the argument of a function the use of summarise, and that argument is assigned in a for loop. For Loop for(j in 1:1){ sumvar <-…
Rick Pack
  • 1,044
  • 10
  • 20
0
votes
1 answer

Use NSE in dplyr::case_when

I've read the Programming with dplyr document and tried to write a simple function wrapping around the case_when() function. library(dplyr) data_test <- data.frame( a = rep(c("a", "b", "c"), each = 5), b = rnorm(15) ) fun_test <-…
moho wu
  • 471
  • 4
  • 13
0
votes
2 answers

dplyr NSE - how pass column names to mutate function call?

I'd like to mutate columns of a data frame using a gsub argument on columns specified in a variable, but am struggling with nonstandard evaluation. In this toy example, I'd like to use columns[[1]] and columns[[2]] instead of .$name_A and .$name_B…
bheavner
  • 519
  • 1
  • 4
  • 20