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

How to define the RHS of case_when() with existing and newly defined columns passed as arguments in user defined function?

Sample code: library(tidyverse) iris <- iris test_tidyeval <- function(data, col_name, col_name_2, column1) { mutate( data, {{col_name}} := case_when(Species == "setosa" ~ column1 + Sepal.Width + Petal.Length, TRUE ~…
Nautica
  • 2,004
  • 1
  • 12
  • 35
0
votes
0 answers

Custom function does not work properly unless the object is stored in the global environment in R

Context I have a custom function myfun1 that fits the cox model. Before fitting the model, I need to do a bit of processing on the data used to fit the model. Specifically, run two lines of code, dd = datadist(data) and options(datadist = 'dd'). If…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
2 answers

inject() function from rlang package cannot work with Predict() function from rms package in R

Context I am learning use tidy eval to write my own function. If I calculate hr1 without using tidy eval, the code works fine, such as: hr1 = Predict(fit, meal.cal = q, fun = exp, ref.zero = TRUE) When I use rlang::inject() and rms::Predict()…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
1 answer

can not use non-standard evaluation in rms pacakge in R

Context I would like to customize a function that uses non-standard evaluation (NSE) to fit the cox model. It works fine when I fit the cox model using the code. But when I wrap the code into a custom function using NSE it reports an…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
1 answer

Using external vector with NSE

Let's say I have the following function: sfn <- function(x){ gsub('"', "", deparse(substitute(x))) } Then sfn() returns "mpg" (for both mpg and "mpg"), this is the result I would like to get. sfn("mpg") #> [1] "mpg" sfn(mpg) #> [1]…
mharinga
  • 1,708
  • 10
  • 23
0
votes
1 answer

Disambiguating a variable name in a function when a column with the same name as the variable exists (data.table)

I have a function with a variable named source. The function works properly, but if the data frame on which the function is applied has a column also named source, it doesn't work. A simple example with dplyr and filtering: These two following lines…
Homard
  • 39
  • 6
0
votes
1 answer

How to use dplyr NSE inside an if condition inside a function?

I have a function which takes an NSE argument. Let's say I have a tibble with a column that needs ordering. I want to create a function that can order the name of the column based on the name inputted in the function argument. However I want it to…
0
votes
1 answer

data.table NSE: escaping local() to surrounding environment

I am curious why data.table's NSE escapes to the surrounding environment even when wrapped in "local()". library(data.table) data <- as.data.table(iris) data[, measure := rep(c('Sepal.Length', 'Sepal.Width'), 75)] # old way: using get (data[,…
0
votes
1 answer

evaluate string expression in ggplot

I would like to pass the label_format expression to ggplot but I'm getting an error #toy df df <- data.frame( x = 1:10, y = seq(0.1,1,by=0.1), label_format = "scales::percent_format(accuracy=0.1)" ) ggplot(df,aes(x=x,y=y))+ geom_point()+ …
Eric
  • 1,381
  • 9
  • 24
0
votes
0 answers

rmarkdown render NSE function fails only inside callr

I seem to have a weird combination of NSE, rmarkdown and callr, similar in flavor to Is it possible to disable `callr` for RMarkdown?. When I set a value to a variable to use it in something similar to a filter call implemented using NSE via nested…
rmflight
  • 1,871
  • 1
  • 14
  • 22
0
votes
1 answer

Force-evaluate function argument in order to "trick" substitute()?

say I have a function from an R package which I want to wrap in a closure for convenience and better code readability. For simplicity, let's assume the function looks as follows: fun <- function(text) { as.character(substitute(text)) } When I…
smokee
  • 3
  • 3
0
votes
3 answers

R: hard code the content of a variable inside a function

I would like to create a function using the value stored in a variable, but in a way that I paste the "content" of that variable rather than the variable call, so that once the variable is re-assigned, the function doesn't change. Is there an…
Dominique Makowski
  • 1,511
  • 1
  • 13
  • 30
0
votes
1 answer

Pass multiple column names in function to dplyr::distinct() with Spark

I want to specify an unknown number of column names in a function that will use dplyr::distinct(). My current attempt is: myFunction <- function(table, id) { table %>% dplyr::distinct(.data[[id]]) } I'm trying the above [.data[[id]]] because…
josephD
  • 142
  • 7
0
votes
1 answer

Non standard evaluation does not work: Error in h(simpleError(msg, call))

Here is a working example of what I want to do. # interactive use subset(iris,Species=="setosa") # as a function fn <- function(level,choice) { x <- paste0(level,"==","'",choice,"'") subset(iris,eval(parse(text=x))) } fn("Species","setosa") As…
mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
0
votes
2 answers

Ordering bar chart in function using NSE?

I am currenty trying to order a bar plot in R. The plot gets created in a function using a passed variable for selecting the column. This is the basic function: plotting <- function(column_to_use) { ggplot(iris, aes(x = Sepal.Length, ))+ …
sonder
  • 25
  • 3