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

R: Using a string as an argument to mutate verb in dplyr

I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to…
Sasha
  • 5,783
  • 8
  • 33
  • 37
10
votes
1 answer

How to pass a named vector to dplyr::select using quosures?

Using the old select_() function, I could pass a named vector into select and change position and column names at once: my_data <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30) my_newnames <- c("newbar" = "bar", "newfoo" = "foo") move_stuff …
crazybilly
  • 2,992
  • 1
  • 16
  • 42
9
votes
1 answer

Understanding when to use ensym, sym vs enquo in a function

I'm trying to wrap my head around the different quo/unquo syntaxes and when each should be used. I am mostly writing functions that pass a dataframe and columns to use as argument -- to plot using ggplot or summarize/manipulate data with dplyr…
Jmac
  • 233
  • 1
  • 8
9
votes
2 answers

R quo_name equivalent of quos

Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <-…
Geyer Bisschoff
  • 221
  • 2
  • 9
9
votes
3 answers

Creating a function with an argument passed to dplyr::filter what is the best way to work around nse?

Non standard evaluation is really handy when using dplyr's verbs. But it can be problematic when using those verbs with function arguments. For example let us say that I want to create a function that gives me the number of rows for a given…
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
8
votes
4 answers

Evaluate different logical conditions from string for each row

I have a data.frame like this: value condition 1 0.46 value > 0.5 2 0.96 value == 0.79 3 0.45 value <= 0.65 4 0.68 value == 0.88 5 0.57 value < 0.9 6 0.10 value > 0.01 7 0.90 value >= 0.6 8 0.25 value < 0.91 9 0.04 value >…
Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34
8
votes
2 answers

dplyr mutate using variable columns

I am trying to use mutate to create a new column with values based on a specific column. Example final data frame (I am trying to create new_col): x = tibble(colA = c(11, 12, 13), colB = c(91, 92, 93), col_to_use = c("colA",…
burger
  • 5,683
  • 9
  • 40
  • 63
8
votes
2 answers

Extract column name in mutate_if call

I would like to extract the column name in the function call to mutate_if. With this, I then want to look up a value in a different table and fill in missing values with the lookup value. I tried using quosure syntax, but it is not working. Is there…
kath
  • 7,624
  • 17
  • 32
7
votes
1 answer

use assign() inside purrr:walk()

I have a number of dataframes and a series of changes I want to make to each of them. For this example, let to the desired change be simply making each data frame a tibble using as_tibble. I know there are various ways of doing this, but I'd like to…
lost
  • 1,483
  • 1
  • 11
  • 19
7
votes
1 answer

How to write nested functions with dplyr and dots elipse?

I am trying to put this as simple as possible Some sample data : library(magrittr) library(dplyr) library(rlang) # sample data tib <- tibble( a = 1:3, b = 4:6, c = 7:9 ) Now a function that makes the sum of two columns: foo = function(df, x,…
pietrodito
  • 1,783
  • 15
  • 24
7
votes
2 answers

String based filtering in dplyr - NSE

I'd like to use dplyr's new NSE notations (version >= 0.6) for a dynamic filter on my data. Let's say I have the following dummy dataset: df = data_frame(x = 1:10, y = 10:1, z = 10 * runif(10)) If now I want to filter column tofilter = "x" for…
Lorenzo Rossi
  • 1,481
  • 1
  • 9
  • 16
7
votes
1 answer

Scoping of variables in aes(...) inside a function in ggplot

Consider this use of ggplot(...) inside a function. x <- seq(1,10,by=0.1) df <- data.frame(x,y1=x, y2=cos(2*x)/(1+x)) library(ggplot2) gg.fun <- function(){ i=2 plot(ggplot(df,aes(x=x,y=df[,i]))+geom_line()) } if(exists("i"))…
jlhoward
  • 58,004
  • 7
  • 97
  • 140
6
votes
1 answer

NSE in nested function calls

I'd like to use a utility function to check whether a given column exists within a given data.frame. I'm piping within the tidyverse. The best I've come up with so far is library(magrittr) columnExists <- function(data, col) { tryCatch({ …
Limey
  • 10,234
  • 2
  • 12
  • 32
6
votes
2 answers

Use both empty and string filters in dplyr's filter

I'm updating an old script using the deprecated dplyr::filter_() to use dplyr::filter(). But I can't get it to work for empty filter strings anymore: Example: library(dplyr) my_df <- tibble::tibble(x = sample(c(0:9), 100, replace =…
Timm S.
  • 5,135
  • 6
  • 24
  • 38
6
votes
1 answer

Sequential evaluation of named arguments in R

I am trying to understand how to succinctly implement something like the argument capture/parsing/evaluation mechanism that enables the following behavior with dplyr::tibble() (FKA dplyr::data_frame()): # `b` finds `a` in previous…
lefft
  • 2,065
  • 13
  • 20
1
2
3
19 20