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

Standard and non standard evaluation in Dplyr

Dear colleagues I am trying to build a function that interpolates linearly data in a dataframe: The code looks as follows: Linear_Interpolation <- function(df, min_ts, max_ts, target_column, signal_key) { if (exists(deparse(substitute(df))) ==…
tfkLSTM
  • 161
  • 13
0
votes
1 answer

R - function paramter that is list of functions--inspect parameter without evaluating?

EDIT: the initial resonses suggest my write-up focused people's attention on questions of best practices rather than questions of technique. I'd like to focus on a technical issue, however, with the below just as a toy example: If a person passes a…
Joe
  • 119
  • 5
0
votes
1 answer

R / nse / Double-handover of a variable to a sub-function

I have two functions I want to wrap together in a wrapping function for usability. The wrapping function includes a variable (a column name within a dataframe) which should be handed over from the wrapping function to one of the subfunctions.…
Timm S.
  • 5,135
  • 6
  • 24
  • 38
0
votes
1 answer

How do I return the name of an argument in my function as a column value?

I'm generating a reference table from a numerical vector and a vector of start dates. I want one of the columns in my reference table to equal the name of my first argument (it should read "Ekonom" for each row). This is important since I will join…
Magnus
  • 728
  • 4
  • 17
0
votes
0 answers

How do I access/use the name of an R object in a function? (part 2)

For context, this is a continuation for this question I want to create a function that create a number of values from a vector of academic credits. 1.5 academic credits equal one week of studies in my country, that means I can use a vector of…
Magnus
  • 728
  • 4
  • 17
0
votes
1 answer

How to save a `quosure` to be used in another session?

What's the best way to save a quosure so that it can be run on another session? Consider the below, which uses the {rlang}'s quosure concept to quote an expression x+2 and captures its environment which is global and evaluates it to 4. However, if…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
0
votes
1 answer

Grouping a data frame with dplyr and plyr within the same function using prequoted arguments

I have a somewhat complicated function with many arguments. Without going into too many details - it does some calculations on a data.frame using dplyr and plyr and returns a data.frame with several results columns attached. I have an argument for…
M4RT1NK4
  • 433
  • 1
  • 4
  • 9
0
votes
4 answers

Refer a column by variable name

Sample data dat <- data.frame(Sim.Y1 = rnorm(10), Sim.Y2 = rnorm(10), Sim.Y3 = rnorm(10), obsY = rnorm(10), ID = sample(1:10, 10), ID_s = rep(1:2, each = 5)) For the following vector, I want to calculate the…
89_Simple
  • 3,393
  • 3
  • 39
  • 94
0
votes
1 answer

NSE challenge: break out of deparse(substitute(...))

Let's define: f <- function(x) deparse(substitute(x)) The challenge: find so that f() returns "abc". Excluding, of course, f(abc). With "tidy NSE", i.e. quasiquoting, this is very easy. However, according to the NSE…
asachet
  • 6,620
  • 2
  • 30
  • 74
0
votes
1 answer

Escape backslash with eval and parse (non-standard evaluation)

I need to use a regex in combination with non-standard evaluation. The following works fine: library(stringr) > str_replace("2.5", "\\.", ",") # without non-standard evaluation [1] "2,5" > eval(parse(text = 'str_replace("2.5", ".", ",")')) # with…
Annerose N
  • 477
  • 6
  • 14
0
votes
0 answers

Using dplyr in loop with parameterised column names

I have data where field names are not known in advance and want to write some functions to perform basic analysis and transformations. Would like to use dplyr for conformity with other apps. Have done some reading around Programming in dplyr, which…
mer_curius
  • 524
  • 6
  • 12
0
votes
2 answers

Specifying multiple variables to group by via explicit argument with unquoted elements

Based on the section regarding capturing multiple arguments in Programming with dplyr, I am trying to specify multiple variables to group by in dplyr::group_by without relying on ... but using an explicit list argument group_vars instead without…
Rappster
  • 12,762
  • 7
  • 71
  • 120
0
votes
1 answer

NSE vs SE in mutate_

I've been reading and reading, but I cannot understand this NSE vs SE in R. I hope somebody can explain it properly. df=data.frame(a=1:6,b=7:12,c=13:18,d=rep(c("a","b"),each=3)) This is what I'm used to, and it works: df %>% group_by(d) %>%…
Helen
  • 533
  • 12
  • 37
0
votes
2 answers

how to use mutate to assign a list of vectors to a list of new var names in r

I'm building a function that you provide a dataframe and a vector of variable names that are numeric - and i want it to return a dataframe with a corresponding variable for each numeric vector that splits it to intervals. I know how to do it with…
guyx1992
  • 13
  • 3
0
votes
2 answers

Summarize data in base R

I try to write a simple function to obtain the rate between columns in a dataframe on a aggregated level. I would like to obtain the same output as the output obtained by: library(dplyr) set.seed(1) dat <- data.frame(x = rep(1:3, each = 5), a =…
mharinga
  • 1,708
  • 10
  • 23