Questions tagged [non-standard-evaluation]
156 questions
2
votes
2 answers
Programmatic Pie Charts with ggplot2
I'm using the following code to create two side-by-side pie-charts
library(ggplot2)
mtcars$cyl <- factor(mtcars$cyl) # converts to a categorical variable
mtcars$gear <- factor(mtcars$gear) # converts to a categorical variable
p <-…

Zhaochen He
- 610
- 4
- 12
2
votes
1 answer
What is an escape hatch in R?
I was reading the book Advanced R and confused with the concept of "escape hatch" repeatedly mentioned in the chapter on Non-standard evaluation. For example, the first time the author mentioned this word, it has the following definition:
As a…

Leon Smith
- 97
- 6
2
votes
1 answer
In R, how to indicate an argument by name when that name is the value of a character vector?
Let's say I need to do this:
foo <- list(`a+b` = 5)
but I have 'a+b' (a string) saved in a variable, let's say name:
name <- 'a+b'
How to create that list with an element whose name is the value in the variable name?
Note: I am aware of other ways…

Ramiro Magno
- 3,085
- 15
- 30
2
votes
2 answers
dplyr NSE: Centering multiple columns in a dataframe
I am trying to "center" multiple columns in a dataframe using dplyr but I keep getting a "non-numeric argument to binary operator" evaluation error. I think it is because I am trying to pass a string in when my function expects a bare variable name.…

Hank Lin
- 5,959
- 2
- 10
- 17
2
votes
1 answer
Passing dplyr filter conditions from shiny inputs into a function
I have been trying to create a general function to use in Shiny apps that will allow a dataframe to be filtered by an arbitrary list of conditions using a sidebar menu. So you can use the sidebar menu to pick both the columns you want to filter on…

Keith McNulty
- 952
- 1
- 6
- 17
2
votes
1 answer
Magic in the way R evaluates function arguments
Consider the following R code:
y1 <- dataset %>% dplyr::filter(W == 1)
This works, but there seems to some magic here. Usually, when we have an expression like foo(bar), we should be able to do this:
baz <= bar
foo(baz)
However, in the presented…

Yatharth Agarwal
- 4,385
- 2
- 24
- 53
2
votes
1 answer
How to user NSE inside fct_reorder() in ggplot2
I would like to know how to use NSE (Non-Standard Evaluation) expression in fct_reorder() in ggplot2 to replicate charts for different data frames.
This is an example of data frame that I use to draw a chart:
travel_time_br30 travel_time_br30_int…

HSJ
- 687
- 6
- 16
2
votes
2 answers
Passing a function as an argument to FUN in lapply
I'm designing a package which fits a model that involves basis expansions of columns of a matrix. I want the expansion to be user defined, so that any expansion is possible such as splines::bs, splines::ns, stats::poly. The same expansion is to be…

sahir
- 336
- 3
- 8
2
votes
1 answer
Standard evaluation and non-standard evaluation in R
I am confused about dplyr functions' arguments and not quite clear about standard evalution (SE) or non-standard evaluation (NSE).
I just want to pass a variable to dplyr::arrange() but it failed. However, passing to dplyr::select() works.
>…

Jackman Li
- 65
- 1
- 8
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 /…

PeterPancake
- 95
- 8
2
votes
0 answers
passing `cbind()` as a function argument
Suppose I have a nice little data frame
df <- data.frame(x=seq(1,5),y=seq(5,1),z=c(1,2,3,2,1),a=c(1,1,1,2,2))
df
## x y z a
## 1 1 5 1 1
## 2 2 4 2 1
## 3 3 3 3 1
## 4 4 2 2 2
## 5 5 1 1 2
and I want to aggregate a part of…

StasK
- 1,525
- 10
- 21
2
votes
2 answers
R passing RSelenium driver environment as function argument
i'm probably not seeing something obvious, anyway i'd like to create functions to automatically extract text from an url already handled by a remote driver. I'd like to pass as a function arguments the xpath expression and the environment into which…

Malacoda
- 23
- 3
1
vote
0 answers
What mistake would trigger a diagnostic using this option in R Studio?
In the Global Options of R Studio, under the Code Panel and Diagnostics Tab, there's an option that reads "Enable diagnostics within R function calls."
I had assumed that this option would trigger diagnostic warnings any time one made a mistake…

Bajcz
- 433
- 5
- 20
1
vote
1 answer
How to save an expression with multiplication to later call it?
I'm looking to save an expression like this:
expression <- c(beta_a*a + beta_b*b + beta_d*d)
And I want to return this expression:
expression
> beta_a*a + beta_b*b + beta_d*d
But when I try to run the first line, it just says the error "Object…

Victor Nielsen
- 443
- 2
- 14
1
vote
2 answers
Accessing an NSE in two function-levels in R
I'm passing a column-name to one function. I want to reference in the function AND in another it calls.
This works as I expected
library(dplyr)
updown <- function(df, columnName){
COL <- enquo(columnName)
df %>%
mutate(UP =…

David T
- 1,993
- 10
- 18