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

dplyr NSE mode in a function: nested conditions

The objective is to transform column(s) of a data frame. Here is the example: df <- data.frame( fact=c("dog",2,"NA",0,"cat",1,"Cat"), value=c(4,2,6,0,9,1,3) ); df$fact <- as.factor(df$fac) func <- function(data,fac,val){ …
remi
  • 781
  • 2
  • 13
  • 22
1
vote
1 answer

Why are these error messages inconsistent when using NSE in R?

Question Why do I get inconsistent error messages in R from a function that uses non-standard evaluation, and what is the best way to control this / write the function? Initial setup that works as intended: library(tidyverse) df <- tibble(a =…
ScottyJ
  • 945
  • 11
  • 16
1
vote
2 answers

Non-standard evaluation: any benefits for *programming* (not interactive R)?

I do not understand the cost-benefit of NSE (non-standard evaluation) in R for programming. I can see why NSE may be useful for interactive R, but for programming -- i.e. writing reusable scripts and functions -- my experience is that it mainly adds…
jessexknight
  • 756
  • 7
  • 20
1
vote
2 answers

Define ggplot2 aesthetics from a list programatically without aes_string

I have a list of aesthetics constructed programmatically and passed from other functions. They may contain any combination of aesthetics like alpha, colour, fill, etc., which is not know beforehand. my_aes_list <- list( x = "cty", y =…
Calimo
  • 7,510
  • 4
  • 39
  • 61
1
vote
1 answer

Can you use a parameter in a function name with tidy eval/NSE/metaprogramming?

I am writing a function that makes ggplots with given data, and I am interested in setting a parameter that can call the different scales::label_* functions to change the outputs. For example, I want to be able to call create_pie_chart(starwars,…
mfg3z0
  • 561
  • 16
1
vote
1 answer

Find missing arguments of a calling function

I am trying to write a function that can return which arguments from the calling function are missing. find_missing_args <- function(...) { args <- rlang::enquos(...) lapply(args, rlang::is_missing) } func <- function(x, y, z) { …
Dylan Russell
  • 936
  • 1
  • 10
  • 29
1
vote
2 answers

How can I make my function access variables in this dataframe?

Taking into consideration the following database (called data), I want to make a function that retrieves the first "Start" position of a given "Letter". I want my function to take the dataframe and Letter as inputs. And I'd like to do this without…
1
vote
3 answers

How to reference objects in a different environment inside a formula

I'm using fixest::feols() and I have a function I want to pass an argument to in order to subset the data using the subset = argument. However when keep getting the error: The argument 'subset' is a formula whose variables must be in the data set…
cach1
  • 105
  • 8
1
vote
2 answers

Accessing an NSE in two function-levels in R

I'm passing a column-name to one function. I want to reference in the function AND in another it calls. This works as I expected library(dplyr) updown <- function(df, columnName){ COL <- enquo(columnName) df %>% mutate(UP =…
David T
  • 1,993
  • 10
  • 18
1
vote
3 answers

paste()ing columns whose names are stored in a variable

How do I create a new column from columns whose names are contained in a character vector? Given these two variables: data <- tibble(numbers = 1:10, letters = letters[1:10]) columns <- c("numbers","letters") What command would produce this…
Michael Henry
  • 599
  • 2
  • 4
  • 17
1
vote
2 answers

Given an expression of the form `fn(NULL)` how do I replace the null with `lhs=rhs`?

Consider ex1 = quote(fn(NULL)) and suppose I wish to make ex1 equals to fn(lhs=rhs) in expression form, how do I do that? since ex1[[2]] = quote(lhs=rhs) gives ex1 = fn((lhs=rhs)) and I can't seem to get rid of the parenthesis.
xiaodai
  • 14,889
  • 18
  • 76
  • 140
1
vote
2 answers

R: In Non-standardard evaluation (NSE), how do I determine the right calling environment when using magrittr? `parent.frame()` doesn't work

Consider proper_filter <- function(.data, ...) { code = substitute(filter(.data, ...)) print(names(parent.frame())) } fn_wo_magrittr <- function(dfr, val) { proper_filter(dfr, speed < val) } fn_w_magrittr <- function(dfr, val) { dfr %>% …
xiaodai
  • 14,889
  • 18
  • 76
  • 140
1
vote
3 answers

Inner_join on NSE

I want to write a function which joins two tibbles, with the 2nd tibble's joined column specified in the function's args. I have df1 <- tibble(NUMBER = c(1,4)) df2 <- tibble(ORDER = 1:5, DISORDER = 5:1, WORD = c("The",…
David T
  • 1,993
  • 10
  • 18
1
vote
4 answers

How to run an arbitrary expression in an environment, storing all results in the environment?

In R, running the expression x <- 1 defines a variable x in the global environment with the value 1. Doing the same within a function defines the variable within the function's environment instead. Using rlang::with_env, we can also do the same…
Wasabi
  • 2,879
  • 3
  • 26
  • 48
1
vote
0 answers

Non-standard evaluation with fitting a distribution to truncated data

I would like to create a function that fits a gamma distribution to truncated data. Let's say I have the following truncated data: # Simulate some truncated data from gamma distribution library(truncdist) library(fitdistrplus) set.seed(1) e <-…
mharinga
  • 1,708
  • 10
  • 23