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

do.call() and tidy evaluation

Trying to make do.call() work in the context of tidy evaluation: library(rlang) library(dplyr) data <- tibble(item_name = c("apple", "bmw", "bmw")) mutate(data, category = case_when(item_name == "apple" ~ "fruit", …
Aurèle
  • 12,545
  • 1
  • 31
  • 49
6
votes
2 answers

Creating and using new variables in function in R: NSE programing error in the tidyverse

After reading and re-reading the many "programing with dplyr" guides, I still cannot find a way to solve my particular case. I understand that the use of group_by_, mutate_ and such "string-friendly" versions of tidyverse functions is heading…
Dominique Makowski
  • 1,511
  • 1
  • 13
  • 30
6
votes
3 answers

How can I use dplyr/magrittr's pipe inside functions in R?

I'm trying to write a function which takes as argument a dataframe and the name of the function. When I try to write the function with the standard R syntax, I can get the good result using eval and substitute as recommanded by @hadley in…
PAC
  • 5,178
  • 8
  • 38
  • 62
5
votes
2 answers

R dplyr programatically identify column

For some objects an attribute identifies a special column, for example the geometry column in an sf object. For conducting some calculations in dplyr it would be good to easily identify these columns. I'm searching for a way to create a function…
Bart
  • 1,267
  • 7
  • 18
5
votes
2 answers

Non-standard Evaluation using tidyr::expand

I am having trouble running non-standard evaluation (nse) expressions with the tidyr package. Basically, what I want to do is to expand two columns that may be identical or not to achieve a dataframe with all possible combinations. The problem is…
Elijah
  • 414
  • 3
  • 8
5
votes
3 answers

How to use non-standard evaluation NSE to evaluate arguments on data.table?

Say I have the following library(data.table) cars1 = setDT(copy(cars)) cars2 = setDT(copy(cars)) car_list = list(cars1, cars2) class(car_list) <- "dd" `[.dd` <- function(x,...) { code = rlang::enquos(...) cars1 = x[[1]] …
xiaodai
  • 14,889
  • 18
  • 76
  • 140
5
votes
2 answers

Tidyeval: pass list of columns as quosure to select()

I want to pass a bunch of columns to pmap() inside mutate(). Later, I want to select those same columns. At the moment, I'm passing a list of column names to pmap() as a quosure, which works fine, although I have no idea whether this is the "right"…
Oliver
  • 1,098
  • 1
  • 11
  • 16
5
votes
3 answers

rlang: Get names from ... with colon shortcut in NSE function

I'm writing a package of functions for making tables of demographics data. I have one function, abbreviated below, where I need to take in several columns (...) on which I'll gather a data frame. The trick is I'd like to keep those columns' names in…
camille
  • 16,432
  • 18
  • 38
  • 60
5
votes
1 answer

How to set ggplot x-label equal to variable name during lapply?

I'm making plots of one y variable against multiple x variables. I have a working solution using lapply. However, I can't manage to write the name of the x variable as the x label for each plot. Here's a simplified example of what I have: The goal…
user9195416
5
votes
2 answers

How to evaluate a constructed string with non-standard evaluation using dplyr?

I have read several guides on programming with dplyr now and I am still confused about how to solve the problem of evaluating constructed/concatenated strings with non-standard evaluation (NSE). I realize that there are better ways to solve this…
Jeffrey Girard
  • 761
  • 4
  • 20
5
votes
2 answers

Using the dot operator in dplyr::bind_cols

I'm seeing some unexpected behavior with dplyr. I have a specific use case but I will setup a dummy problem to illustrate my point. Why does this work, library(dplyr) temp <- bind_cols(mtcars %>% select(-mpg), mtcars %>% select(mpg)) head(temp) cyl…
Taran
  • 265
  • 1
  • 11
5
votes
2 answers

Rename in dplyr 0.7+ function

I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting…
Daniel
  • 415
  • 1
  • 6
  • 16
5
votes
1 answer

Using select-like mechanism to select variables for distinct call in dplyr

Desired results Using simple syntax I filter on vs and am columns leaving also the cyl values. data(mtcars) dta <- mtcars[,c("vs", "am", "cyl")] # Desired results dta %>% distinct(vs, am, .keep_all = TRUE) Desired syntax I would like to reverse…
Konrad
  • 17,740
  • 16
  • 106
  • 167
5
votes
3 answers

use dplyr mutate() in programming

I am trying to assign a column name to a variable using mutate. df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } When I run new(name = "newVar") it doesn't…
Kay
  • 2,057
  • 3
  • 20
  • 29
5
votes
1 answer

Dplyr function to compute average, n, sd and standard error

I find myself writing this bit of code all the time to produce standard errors for group means ( to then use for plotting confidence intervals). It would be nice to write my own function to do this in one line of code, though. I have read the nse…
spindoctor
  • 1,719
  • 1
  • 18
  • 42
1 2
3
19 20