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
4
votes
2 answers

Why do quosures work in group_by() but not filter()?

I'm working on building a function that I will manipulate a data frame based on a string. Within the function, I'll build a column name as from the string and use it to manipulate the data frame, something like this: library(dplyr) orig_df <-…
crazybilly
  • 2,992
  • 1
  • 16
  • 42
4
votes
2 answers

R object's name carrying through multiple functions

In line with my reading of Hadley's advice on building S3 objects I am using a helper function, a constructor function, and a validator function. A simple reproducible example: test_object <- function(x, y, z) { new_test_object(x, y,…
jamse
  • 344
  • 2
  • 14
4
votes
3 answers

dplyr concat columns stored in variable (mutate and non standard evaluation)

I would like to concatenate an arbitrary number of columns in a dataframe based on a variable cols_to_concat df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9]) cols_to_concat = c("a", "b", "c") To achieve the desired…
RobinL
  • 11,009
  • 8
  • 48
  • 68
3
votes
2 answers

R Language indirection

I'm a long-term developer but somewhat new to the R language. I'm trying to write some clean and maintainable code. I know how to do this in multiple languages but not R. I've got an R function that performs the same action for different fields. #…
3
votes
1 answer

I'm trying to create a function with expression() inside, but I get an error

I want to create this function library(sde) myfunction <- function(r,a,K,vi){ set.seed(123) d <- expression(r*x*(K-x)) s <- expression(a*x*(K-x)) sde.sim(X0=1,drift=d, sigma=s,M=3) -> X plot(X, main="Multiple") } but when I run the…
José
  • 33
  • 4
3
votes
2 answers

Passing a quoted function argument to a three-dots argument inside another function using base R

I would like to pass quoted variables in the group argument of geom_col_wrap to the split_group function. # I deleted the rest of the function for readability geom_col_wrap = function(data, mapping, group, ...) { data |> split_group(group)…
arturhgq
  • 31
  • 2
3
votes
2 answers

how to use rlang::as_string() inside of a function?

I'm writing a function where I supply a variable name as a symbol. At a different step in the function, I want to use the variable name as a string. Per the documentation, rlang::as_string "converts symbols to character strings." Here is a basic…
John J.
  • 1,450
  • 1
  • 13
  • 28
3
votes
2 answers

R use bang-bang within a glue statement

I'd like to make a simple function that takes a data frame and user supplied names for two columns in that data frame. The purpose would be to allow this to work easily with dplyr pipes. It will produce a character vector of glued strings: func <-…
Dylan Russell
  • 936
  • 1
  • 10
  • 29
3
votes
2 answers

r- How to use iteration on a custom function that uses dplyr

I want to create a custom function to calculate grouped percentages in a large dataset with 100+ columns. Because I have so many columns I want to do a loop or lapply or something to avoid typing the function out 100+ times. The function I wrote…
kellyd
  • 33
  • 5
3
votes
1 answer

R: Looping over custom dplyr function

I want to build a custom dplyr function and iterate over it ideally with purrr::map to stay in the tidyverse. To keep things as easy as possible I replicate my problem using a very simple summarize function. When buildings custom functions with…
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
3
votes
1 answer

How do I pass a variable name to conditionally sum in dplyr pipe?

The crux of the problem is how to pass in a column variable into a grouped df to conditionally sum data. Data for the example follows: library(dplyr) library(rlang) set.seed(1) # dummy dates date_vars <- purrr::map(c('2018-01-31', '2018-02-28',…
cavamic
  • 69
  • 7
3
votes
2 answers

Storing and calling variables in a column in dplyr within a function

I want to store some variables within a column cell within a tibble. I then want to call that column and either paste the names of those variables or call that column and paste the columns which those variables correspond to together. In addition,…
Margot J
  • 33
  • 1
  • 5
3
votes
0 answers

use variable name in mutate_if predicate

I can use the vars() wrapper inside mutate_at to perform a mutation on variables whose names are in a character vector: library(tidyverse) varnames <- c("mpg", "am") mtcars.mod <- mtcars %>% mutate_at(vars(varnames), …
lost
  • 1,483
  • 1
  • 11
  • 19
3
votes
3 answers

Unquoting the loop variable in `rlang::expr`

I came across unexpected behavior when using operator !! to iteratively construct expressions that involve a loop variable. Creating an expression from the loop variable itself, e.g., X <- list() for( i in 1:3 ) X[[i]] <- rlang::expr( !!i…
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
3
votes
1 answer

Multiple column names to quos in dplyr NSE

I'm writing functions to automate a workflow for analyzing a lot of demographic data. I can get what I need from a regular pipe-stream of dplyr functions, but I need to abstract this into NSE functions. I'm supplying a column name to a series of…
camille
  • 16,432
  • 18
  • 38
  • 60