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

NSE on shiny.tag

A strange use case: I'm trying to allow the text specifier on a shiny tag to be changed, while remaining a shiny.tag. So for example, this is the output I desire: library(htmltools) library(rlang) tags$a("boom") #> boom Notice that this is a…
Amit Kohli
  • 2,860
  • 2
  • 24
  • 44
2
votes
3 answers

Iterate over symbols defined in some inner environment using non-standard evaluation

I'd like to make below program work. I know, the error lies in list(f1, f2), but I didn't find a way to make it work so far. The problem is that f1 and f2 are not known outside func's environment, but I'd like to just pass them as a name /…
2
votes
0 answers

dplyr: how to unquote in `bind_rows`?

My surprising new experience with dplyr is if I do various manipulations on tibbles involving unquotes (!!x or rlang::UQ(x)), then assign them to variables and finally merge them by bind_rows it works all fine; while the same manipulations embedded…
deeenes
  • 4,148
  • 5
  • 43
  • 59
2
votes
1 answer

Accessing New Variables in a dplyr function

I am writing a function to calculate odds ratios for a table of counts that requires NSE evaluation with dplyr and tidyr. As may be apparent, this is my first venture into the NSE world. For example, with a dataframe 'foo': # A tibble: 4 x 3 …
Jeff Bannon
  • 233
  • 1
  • 3
  • 11
2
votes
2 answers

How to select, duplicate, and rename multiple columns in tibble with tidy evaluation semantics?

I'd like to copy a set of variables in my tibble so I can have variable_unmodified and variable values in downstream evaluation. I've come up with a hacky version using the old-style underscore NSE select_() function and .dots, but would like to use…
bheavner
  • 519
  • 1
  • 4
  • 20
2
votes
1 answer

Using dplyr's enquo to access Spark table columns via sparklyr

I would like to be ebale to use dplyr's enquo within lapply call while jumping through Spark table columns. lapply(tbl_vars(sprkTbl), function(col_nme) { print(col_nme) # Enquoe column name quo_col_nme <-…
Konrad
  • 17,740
  • 16
  • 106
  • 167
2
votes
3 answers

Adjust function to work with dplyr/magrittr

I have: df <- data_frame( a = 1:2, b = list(1:10, 4:40) ) and foo <- function(x) mean(unlist(x)) The following works as expected: df$b %>% foo However, I was not able to figure out which modifications of foo are needed in order for df %>%…
johannes
  • 14,043
  • 5
  • 40
  • 51
2
votes
3 answers

How to deal with NSE and usemethod

Have a look to this "simple" functions : test <- function(x,...){ UseMethod("test",x) } test.default<-function(x,y,data){ message("default") print(deparse(substitute(x))) print(deparse(substitute(y))) print(deparse(substitute(data))) …
Vincent Guyader
  • 2,927
  • 1
  • 26
  • 43
2
votes
1 answer

R function with expression as parameter for dplyr summarise

Okay, this is something that feels like it should be relatively easy, but although I have tried literally dozens of approaches with quote, eval, substitute, enquote, parse, summarize_ etc... I haven't gotten it to work. Basically I am trying to…
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
2
votes
1 answer

Non-standard evaluation of dot parameters

I have a function that I want to wrap another function around, passing the arguments through as the ... arguments parameters. I'm having trouble learning how to structure the underlying function call with lazyeval Here's a decent…
gregmacfarlane
  • 2,121
  • 3
  • 24
  • 53
2
votes
2 answers

Creating a function with multiple arguments using dplyr

I am struggling with using dplyrfunctions in own functions. I am closer to understand but still missing full understanding. Here I have df containing type and D10 variables. df <- data.frame(type = c("KL", "KL", "A", "A", "B", "B", "9999", "-1"), …
Mateusz1981
  • 1,817
  • 17
  • 33
2
votes
3 answers

How to use nesting_ in SE case

I am struggling to learn how to program using the hadleyverse. I've read the NSE and lazyeval vignettes, but I'm still lost... I'm trying to translate the example given on the tidyr::complete help page to an SE case. df <- data_frame( group =…
ap53
  • 783
  • 2
  • 8
  • 19
2
votes
1 answer

NSE on complex expressions with dplyr's do()

Can someone help me understand how NSE works with dplyr when the variable reference is in the form ".$mpg" . After reading here, I thought using as.name would do it, since I have a character string that gives a variable name. For example, this…
Trisha
  • 23
  • 5
2
votes
1 answer

R make function robust to both standard and non-standard evaluation

I have a little function that searches though user defined column of a dataframe relying on dplyr. In the current form below it accepts the column argument in non-standard evaluation - without quotes (e.g. scenario instead of "scenario" in standard…
roming
  • 1,165
  • 1
  • 9
  • 22
2
votes
1 answer

Using ggplot within a function with facet and multiple geoms

I am trying to write a function that uses ggplot but allows user specification of several of the plotting variables. However I'm having trouble getting it to work as a function (receiving an error message: see below). A small example dataset and…
nickb
  • 319
  • 2
  • 12