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

How to refer to an argument as character in dplyr filter inside a function

I am trying to build a function for calculating percentages for certain variables - but I am struggling to refer to an argument as a character value inside quotations as I need to use it inside a filter verb. I have the dataset below. e1_done <-…
T. C. Nobel
  • 465
  • 2
  • 9
2
votes
1 answer

R read target programmatically

I have a set of targets, lets say data_a, data_b, ... I want to iterate over all datasets and load the data. This can be achieved using tar_read(data_a) or tar_read(data_a"). As I want to load the targets programmatically, I would like to use…
David
  • 9,216
  • 4
  • 45
  • 78
2
votes
1 answer

How is {rlang}'s 'curly-curly' operator `{{` implemented?

The {rlang} documentation at help("nse-force") gives the following: The curly-curly operator {{ }} for function arguments is a bit special because it forces the function argument and immediately defuses it. The defused expression is substituted in…
wurli
  • 2,314
  • 10
  • 17
2
votes
1 answer

R function that works with NSE and Shiny Inputs

I am looking for an easy way to have my function work with input the comes from Shiny (i.e. string input) or with typical interactive use that is Tidyverse functions enable with NSE. Without the need to duplicate my code to handle each case…
student
  • 1,001
  • 2
  • 12
  • 24
2
votes
1 answer

Check if a tidyselect is NULL and if so, set a default tidyselect method

I am struggling to set a default selection method if NULL is given. For example, let's say I want to implement a function that squares all values for some tidyselect method, symbol, or string, and if none is given, then by default it squares all…
Baraliuh
  • 2,009
  • 5
  • 11
2
votes
2 answers

Filter dataframe when column name-value pairs are stored in a list?

I have a dataframe like: df <- tibble::rownames_to_column(USArrests, "State") %>% tidyr::pivot_longer(cols = -State) head(df) # A tibble: 6 x 3 State name value 1 Alabama Murder 13.2 2 Alabama Assault 236 3…
LMc
  • 12,577
  • 3
  • 31
  • 43
2
votes
1 answer

Environments and lazy evaluation: getting rid of symbols and getting values

I am trying to wrap my head around a problem I ran into that I think largely revolves around environments, lazy evaluation and issues related to these things. I have a main function (foo1 below) that in my actual use case is quite extensive. For…
hejseb
  • 2,064
  • 3
  • 18
  • 28
2
votes
2 answers

ggplot2 function - checking whether user input variable should be a mapped aesthetic

I've written a function to make a scatter plot that allows the user to input the size of the points as either a numeric value (which is kept outside the aes() call) or as a variable in the data frame to be mapped (which needs to go inside the aes()…
Mooks
  • 593
  • 4
  • 12
2
votes
3 answers

Can you create a function in R from text strings by using substitute()?

I'm trying to write a function that allows me to input text strings for field, operator, and value, and return a simple dplyr::filter function that I can then apply to a dataset. Example: library(dplyr) field <- "Species" operator <- "==" value <-…
Aaron Cooley
  • 438
  • 3
  • 8
2
votes
2 answers

Access the name of an enquoted dots parameter

I have two functions: exclude <- function(df, ...) { dots <- rlang::enquos(...) for(i in 1:length(dots)) { df <- exclude_cycle(df, dots[[i]]) } return(df) } exclude_cycle <- function(df, condition) { df <- dplyr::filter(df,…
Dylan Russell
  • 936
  • 1
  • 10
  • 29
2
votes
1 answer

How to add a function within a function?

I have a function to create a plot. However, I would like to include some preprocessing within the function to convert values to labels using the sjlabelled package. library(haven) data <-…
writer_typer
  • 708
  • 7
  • 25
2
votes
3 answers

How to create a function with "not equal to"?

I have a function that I'm trying to create using filter !=, but it doesn't work. I wonder if it is due to something related with tidy evaluation. This is what I tried: library(haven) library(dplyr) library(labelled) library(sjlabelled) data <-…
writer_typer
  • 708
  • 7
  • 25
2
votes
1 answer

How to use mutate inside a function with formulas instead of .dots and mutate_?

I used to use mutate_ with .dots argument to evaluate a set of formulas inside a function using a mix of user supplied variables (in string format) and variables calculated inside the function (as the b variable below) as follows: require(dplyr) f…
mickkk
  • 1,172
  • 2
  • 17
  • 38
2
votes
3 answers

Simple and efficient way to subset a data frame using values and names in a vector

Given a dataset (let say stored as data frame) in the form: > n <- 10 > set.seed(123) > ds.df <- data.frame(col1 = round(rnorm(n,2,4), digit = 1), col2 = sample.int(2, n, replace = TRUE), col3 =…
Simon C.
  • 1,058
  • 15
  • 33
2
votes
1 answer

How can you use a polynomial function programmatically in mutate?

I want to use mutate to give me predicted values based on pre-specified variables with a quadratic / polynomial function. I can easily do this with a linear formula like this: library(tidyverse) xvar <- "Sepal.Length" yvar <-…
Mark Neal
  • 996
  • 16
  • 52