Questions tagged [non-standard-evaluation]
156 questions
32
votes
1 answer
Get the argument names of an R function
For an arbitrary function
f <- function(x, y = 3){
z <- x + y
z^2
}
I want to be able take the argument names of f
> argument_names(f)
[1] "x" "y"
Is this possible?

landau
- 5,636
- 1
- 22
- 50
29
votes
1 answer
Determining whether a function has standard evaluation
Is there any way to programmatically tell if a given function in r has standard evaluation, and if not, which component of function evaluation –
parsing,
matching,
scoping,
promise formation,
promise fulfillment,
return,
etc. – is…

andrewH
- 2,281
- 2
- 22
- 32
20
votes
2 answers
Where, if anywhere, are the dangers of non-standard evaluation documented?
Many of R's functions with non-standard evaluation, e.g. with, subset, and transform, contain a warning like this:
For interactive use this is very effective and nice to read. For programming however, i.e., in one's functions, more care is needed,…

J. Mini
- 1,868
- 1
- 9
- 38
17
votes
1 answer
deparse(substitute()) returns function name normally, but function code when called inside for loop
I'm a bit surprised by R's behaviour in a very specific case. Let's say I define a function square that returns the square of its argument, like this:
square <- function(x) { return(x^2) }
I want to call this function within another function, and I…

A. Stam
- 2,148
- 14
- 29
12
votes
4 answers
What does 'Can't use `!!!` at top level.' mean and how to resolve it?
I am trying to create a function for creating lollipop plots using ggplot2. I would like to pass all argument within ... to aes() within geom_point(). However, I'd like to exclude the size argument from passing onto aes() within geom_segment() (for…

Thomas Neitmann
- 2,552
- 1
- 16
- 31
11
votes
3 answers
How to replace the deprecated ggplot2 function aes_string: accepting an arbitrary number of named strings to specify aesthetic mappings?
aes_string had some convenient behaviours that I made use of when programming with ggplot2. But aes_string has been deprecated (noticeably since ggplot2 version 3.4.0 I believe). I am struggling with how to nicely replace it.
Specifically, I…

David Barnett
- 153
- 1
- 9
8
votes
3 answers
Why does `substitute` work in multiple lines, but not in a single line?
I was attempting to answer this nice question about creating a non-standard evaluating function for a data.table object, doing a grouped sum. Akrun came up with a lovely answer which I'll simplify here:
akrun <- function(data, var, group){
var <-…

Gregor Thomas
- 136,190
- 20
- 167
- 294
8
votes
1 answer
curly curly Tidy evaluation and modifying inputs or their names
The new curly curly method of tidy evaluation is explained in this article. Several examples are given demonstrating the use of this style of non-standard evaluation (NSE).
library(tidyverse)
# Example 1 --------------------------
max_by <-…

Display name
- 4,153
- 5
- 27
- 75
7
votes
1 answer
What effect does setting the attribute of a vector have in dplyr::summarize()?
I just ran into some weird behavior of dplyr where summarize kept referring to objects from a previous group.
Here is a simple reproducible example to illustrate the surprising behavior:
library(dplyr, warn.conflicts = FALSE)
tibble(x =…

const-ae
- 2,076
- 16
- 13
7
votes
1 answer
Call to weight in lm() within function doesn't evaluate properly
I'm writing a function that requires a weighted regression. I've repeatedly been getting an error with the weights parameter, and I've created a minimal reproducible example you can find here:
wt_reg <- function(form, data, wts) {
lm(formula =…

be_green
- 708
- 3
- 12
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
How to use rlang operators in a package?
I am writing a package that uses tidyverse functions, i.e. that use non-standard evaluation, like dplyr::filter for example:
setMethod("filter_by_id",
signature(x = "studies", id = "character"),
definition = function(x, id) {
…

Ramiro Magno
- 3,085
- 15
- 30
6
votes
2 answers
Creating dplyr function that can tell if variable input is a string or a symbol
I've been studying the "Programming with dplyr" vignette because I want to create functions that use dplyr functions. I would like to use the functions I make in both shiny applications and interactive R work. For use in shiny, I would like these…

Dave Rosenman
- 1,252
- 9
- 13
5
votes
1 answer
Why are my ... argmuments still evaluated despite me trying to defuse them?
I am struggling to defuse my ... arguments in one context in particular and I cannot understand why.
I can make a function like this and defuse ... appropriately:
library(dplyr)
library(tidyr)
fill_na <- function(.x,...){
dotArgs <-…

Fredrik Karlsson
- 485
- 8
- 21
5
votes
2 answers
Problem with non-standard evaluation in disk.frame objects using data.table syntax
Problem
I'm currently trying to write a function that filters some rows of a disk.frame object using regular expressions. I, unfortunately, run into some issues with the evaluation of my search string in the filter function. My idea was to pass a…

Joshua Entrop
- 73
- 6