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

How to pass a symbolic variable name between nested functions in the dplyr-way?

I want to write a function, which takes symbolic names of columns and pass it to subsequent functions doing something specific (sub-tasks). Let me show you an example: The data: > ( d <- data.frame(A=1:3, B=3:1) ) A B 1 1 3 2 2 2 3 3 1 Now my…
Bastian
  • 313
  • 1
  • 13
0
votes
1 answer

Using a data frame column name with glue_data

Hi I have a function in R: myfunction<-function(.data, pvalue, roundto=2){ glue::glue_data(.x=.data, "p={format(round(pvalue,digits=roundto),nsmall=roundto}") } This works fine when I pass .data$pvalue as pvalue to myfunction() Ideally I would…
ab192
  • 5
  • 2
0
votes
2 answers

R passing missing arguments through functions

I can't seem to find an SO post that helps me solve this problem. My minimally reproducible example is: bigfn <- function(x) { smallfn(x) } smallfn <- function(y) { y <- rlang::enquo(y) print(y) print(rlang::quo_is_missing(y)) } The output…
Dylan Russell
  • 936
  • 1
  • 10
  • 29
0
votes
1 answer

Passing Column Name as Parameter to data.table::setkey() --- some columns are not in the data.table: col_name

So, essentially what I'm wanting is similar to these two posts: 1, 2. However, when I try the solutions, I keep getting an error. How my problem differs is that I am using the data.table package and am trying to set a key value. For more details,…
Max Black
  • 13
  • 3
0
votes
2 answers

Dynamically create dataframe and variable names in R similar to macros in SAS and STATA

I've been trying to find a solution for this for over a year already and decided to write a post about it. Any help would be appreciated. Here is the pseudocode that I can do easily in Stata and SAS but I don't know how to do in R. {} is the…
0
votes
1 answer

tidy eval ggplot2 NSE not rendering correctly

I'm trying to write a function to pass quoted items for constructing multiple ggplots.The following code works great and does what I want. fig2.data %>% ggplot(aes(x = Surgery, y = BALF_Protein, fill = Exposure)) + stat_summary(geom =…
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
0
votes
2 answers

Using "summarise" (dplyr) with a function requiring a formula

I am trying to generate a table of regression slopes generated by a custom function based on the mblm package (the function in the example here is a simplified version). The function requires a formula as argument and I would like to use dplyr…
ThomasW
  • 1
  • 3
0
votes
1 answer

R Dynamically filter rows in dataframe based on unique combination of listed columns

I have a large dataset (49 columns with 16000 rows) in long format. Ultimately, I need to filter the dataset by unique combinations of user defined specific columns in order to plot other dynamically chosen columns. The plotting will be done using…
melmo
  • 757
  • 3
  • 15
0
votes
1 answer

functional programming with ggplot2 in R - NSE

I have a plot, that i repeat numerous times, just with different variables, so i want to make it into a function: getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){ sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar))…
Nneka
  • 1,764
  • 2
  • 15
  • 39
0
votes
1 answer

non-standard evaluation (NSE) with dplyr in R

I am trying to write a function to create summary table. The variables i am interested to summarise might change, therefore, I would like to have it in a function. I followed the example from the dpylr vignette about NSE, but it is not working for…
Nneka
  • 1,764
  • 2
  • 15
  • 39
0
votes
2 answers

Dynamically construct function calls with varying arguments using dplyr and NSE

I want to be able to construct function calls dynamically with varying grouping variables/arguments using dplyr. The number of function calls may be quite large, which means the examples in the programming with dplyr vignette are not practical.…
Lauler
  • 179
  • 1
  • 1
  • 7
0
votes
1 answer

How to optimize case_when in a function?

I would like to write a function that creates a binning variable based on some raw data. Specifically, I have a dateset with the age values for each respondent and I would like to write a function that classifies that person into an age group, where…
Tea Tree
  • 882
  • 11
  • 26
0
votes
0 answers

NSE dplyr::left_join failing due to incompatible types

I'm writing a function which helps me clean up field trial data. I'm writing it using tidyeval NSE to challenge myself and ensure that the function is easy to use if I return to it. I think I've nearly got it, but when running my test frame I get a…
0
votes
1 answer

How to use tidy eval NSE to expand a `an expression`

I want to expand the !!! expression just like they do in dplyr-verbs e.g. aggregate_expressions <- list(n = quote(n())) do_something(iris, !!!(aggregate_expressions)) and say I want do_something to perform do_something <- function(...) { iris…
xiaodai
  • 14,889
  • 18
  • 76
  • 140
0
votes
2 answers

Continuous X variable used but still getting Error: StatBin requires a continuous x variable

I am creating a function that uses a forloop to create and return a list of ggplots. When I try to view any of the returned plots, e.g. by calling plots["aClass_20_30"], I get: Error: StatBin requires a continuous x variable: the x variable is…
user699290
  • 41
  • 1
  • 4