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

Understanding assignment when using rlang::eval_tidy

I want to understand why the following happens when evaluating <- assignment operations using rlang::eval_tidy. Let's use rlang::expr to create an expression to assign the variable x a value 1. x_expr <- rlang::expr(x <- 1) We also create an…
M. Rodo
  • 448
  • 4
  • 12
1
vote
1 answer

Environmental problems using testthat

I have some delicate issues with environments that are currently manifesting themselves in my unit tests. My basic structure is this I have a main function main that has many arguments wrapper is a wrapper function (one of many) that pertains only…
hejseb
  • 2,064
  • 3
  • 18
  • 28
1
vote
1 answer

specify variable names when grouping

I am using dplyr v1.0.2 to manipulate tibbles. I would like to use group_by(), using a function or a regular expression to specify the relevant variable names (the ... argument). The only solution that I've found is clunky. Is there a relatively…
user697473
  • 2,165
  • 1
  • 20
  • 47
1
vote
1 answer

How to fix the issue with {{}} and all_off() if when I want to make a dplyr-like function able to work both with NSE and SE?

I want to write a function, which will take both symbolic names of column and names passed as a variable (string). Let me show you an example: The data: > ( d <- data.frame(A=1:3, B=3:1) ) A B 1 1 3 2 2 2 3 3 1 Now my function: fn <-…
Bastian
  • 313
  • 1
  • 13
1
vote
2 answers

purrr: Hand over column names as parameter list to pmap()

I would like to use purr::pmap() to hand over column names as parameters to a function using those names in dplyr functions. Example: library(purrr) library(dplyr) tib <- tibble(A1 = 0:9, A2 = 10:19, A3 = 20:29, B1 = 30:39, B2 =…
Timm S.
  • 5,135
  • 6
  • 24
  • 38
1
vote
2 answers

R dplyr pass expression as argument to function

I have a simple silly question, but sorry for so simple. How do I pass an arithmetic expression as a function argument? test_func <- function(data, myexpr){ data %>% filter(!! myexpr) } mtcars %>% test_func(myexpr = "cyl > 6") Thanks in…
Aureliano Guedes
  • 767
  • 6
  • 22
1
vote
1 answer

NSE within plotly color factors

I am trying to design a function where there are multiple inputs on what factors can control how a chart is colored. I am trying to use a character string to desegnate the column of the data.frame to use, but it is not working within the color =…
Joe
  • 349
  • 1
  • 2
  • 11
1
vote
1 answer

adding new columns to a data.table within a function in R

as a part of a bigger function, i need to create two new columns in a data.table (which is later on used to create a plot). these are the names of my columns: names(freqSevDataAge) [1] "ag5" "claims" …
Nneka
  • 1,764
  • 2
  • 15
  • 39
1
vote
2 answers

R: How to write a function that replaces a function call with another function call?

E.g. I want to transform the code mean(x) to fn(x) everytime I see mean in the code. replace_mean <- function(code) { substitute(code, list(mean = fn)) # doesn't work substitute(substitute(code), list(mean = fn)) # doesn't work } the above…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
1
vote
2 answers

looping over a list of filter expressions: problem with NSE in map2 call within mutate

I have defined a list of expressions containing arguments I want to pass to a dplyr::filter call. library(tidyverse) # using tidyr 1.0.0 cond_filter <- list(expr(1 > 0), # condition to select all rows expr(Species == "setosa"), …
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
1
vote
1 answer

How to perform NSE on the left hand side of a dplyr function?

Consider library(dplyr) assign_rhs <- function(df, rhs) { df %>% mutate(lhs = {{rhs}}) } a = data.frame(b = 1, c = 2) assign_rhs(a, b) will yields: b c lhs 1 1 2 1 and I can do assign_rhs(a, c) as well to assign lhs to c instead of…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
1
vote
4 answers

Dynamic Columns in Dplyr using NSE on the RHS

I am attempting to reference existing columns in dplyr through a loop. Effectively, I would like to evaluate the operations from one table (evaluation in below example) to be performed to another table (dt in below example). I do not want to…
Jst2Wond3r
  • 311
  • 2
  • 11
1
vote
0 answers

Functional programming: use broom nest->tidy->unnest and map within a function

I need to turn a (working) bit of dplyr/broom code into a function, since I'll call it several (dozen) times. I am stuck -- and this has likely to do with Non Standard Evaluation being mixed with Standard Evaluation. Here I take code directly from…
PaoloCrosetto
  • 600
  • 1
  • 7
  • 16
1
vote
1 answer

Non standard evaluation setting names R

I am trying to repeatedly call a function (specifically Seurat::DimPlot), where one of the the arguments is a named list (cells.highlight). I am planning to use purrr::imap to do make the call. I have a named list, where each of the element of the…
HowYaDoing
  • 820
  • 2
  • 7
  • 15
1
vote
1 answer

Programming Functions: NSE in DPLYR and PURRR

currently I am running into some problems with Non-Standard Evaluation when trying to wrap a function around some calculations done with dplyr und purrr that I use on multiple occasions. I have read about NSE and also think I know the point where…