Questions tagged [non-standard-evaluation]
156 questions
0
votes
1 answer
Use nonstandard evaluation and mutate in r dataframe with dplyr
I'm having trouble figuring out how to use a string containing R data frame column names to do some basic calculations to mutate into new columns. For example, I have columns of baseline values and other columns with post-treatment timepoints. I…

user42485
- 751
- 2
- 9
- 19
0
votes
2 answers
R user-defined/dynamic summary function within dplyr::summarise
Somewhat hard to define this question without sounding like lots of similar questions!
I have a function for which I want one of the parameters to be a function name, that will be passed to dplyr::summarise, e.g. "mean" or "sum":
data(mtcars)
f <-…

dez93_2000
- 1,730
- 2
- 23
- 34
0
votes
0 answers
(R, purrr) iteration over pairs of columns by name, one constant & one from a list
I am trying to produce a list of graphical objects (grobs), suitable for passing to gridExtra::grid.arrange (or some ggequivalent if that is easier). I have redrafted my example to eliminate multiple problems that were kindly pointed out by @MrFlick…

andrewH
- 2,281
- 2
- 22
- 32
0
votes
1 answer
How do I pass a variable name to st_sample() argument in a function using pipes (%)?
I want to create a function that takes a simple feature layer and a variable name and creates random points based on the variable values. I can do this without a problem sequentially using pipes(%), but I'm getting stuck on setting up a function…

Marcos
- 444
- 4
- 9
0
votes
1 answer
Changing list names dynamically in R using non-standard evaluation
What I try to do is to set the name of a list element dynamically in R, from a predefined string and using non-standard evaluation.
See the example below:
library(rlang)
dynamic.listname <- "important.name"
# this works (as was also demonstrated…

Leon Samson
- 405
- 3
- 12
0
votes
1 answer
R - function paramter that is list of functions--inspect parameter without evaluating?
EDIT: the initial resonses suggest my write-up focused people's attention on questions of best practices rather than questions of technique. I'd like to focus on a technical issue, however, with the below just as a toy example:
If a person passes a…

Joe
- 119
- 5
0
votes
1 answer
Is there a way to input dplyr::summarise variables?
This is a non-standard-evaluation problem I can't seem to solve. I want to make a function that inputs a column name and outputs a summary. For this function, it's important that the summarise(name,...) can be assigned as input like so:
mtcars %>%
…

Magnus Nordmo
- 923
- 7
- 10
0
votes
1 answer
ggplot2: Hand over variable to function (nonstadndard eval)
I'm trying to write a function in which a grouping variable gets handed over to a ggplot2 expression.
# sample data
df <- data.frame(x = rep(1:20, 5), y = rnorm(100),
grp1 = c(rep("A", 20), rep("B", 20), rep("C", 20), rep("D",…

Timm S.
- 5,135
- 6
- 24
- 38
0
votes
1 answer
Use a function's unnamed parameters ... in a glue::glue call
I want a function to glue two character variables together even when the name of one (or many) are not know until the function is called.
f <- function(t, str1, ...) {
t %>%
mutate(name = glue::glue("{str1} {...}"))
}
s <- tidyr::tribble(
…

Harlan Nelson
- 1,394
- 1
- 10
- 22
0
votes
2 answers
mutate_ deprecated, easy and understandable alternatives?
I'm trying to create a function that creates a variable. Like this:
Add_Extreme_Variable <- function(dataframe, variable, variable_name){
dataframe %>%
group_by(cod_station, year_station) %>%
mutate(variable_name= ifelse(variable >…

Chris
- 2,019
- 5
- 22
- 67
0
votes
3 answers
How does filter() in dplyr evaluate what's inside the () in a customized function?
I am trying to write a function that takes two column names and upper and/or lower boundary for each column names, so that that way I can subset the data with column names and boundary of my choice.
Using mtcars as an example, if I want to subset…

xiahfyj
- 101
- 1
- 5
0
votes
1 answer
NSE challenge: break out of deparse(substitute(...))
Let's define:
f <- function(x) deparse(substitute(x))
The challenge: find so that f() returns "abc". Excluding, of course, f(abc).
With "tidy NSE", i.e. quasiquoting, this is very easy. However, according to the NSE…

asachet
- 6,620
- 2
- 30
- 74
0
votes
1 answer
Escape backslash with eval and parse (non-standard evaluation)
I need to use a regex in combination with non-standard evaluation.
The following works fine:
library(stringr)
> str_replace("2.5", "\\.", ",") # without non-standard evaluation
[1] "2,5"
> eval(parse(text = 'str_replace("2.5", ".", ",")')) # with…

Annerose N
- 477
- 6
- 14
0
votes
1 answer
Change the body of a function without quote marks
I need to change the body of a function depending on input given by users.
modelstring <- function() {
for (h in (l1i1[j]):(l1i2[j])) {
w[h] <- 1/l1n[h]
}
}
userinput <-…

Ben
- 197
- 1
- 9
0
votes
1 answer
R nonstandard evaluation: pass the name of a variable as a parameter to a function and have the function assign a value to it
I am certain this is answered somewhere, but was unable to find it. Programming with dplyr also doesn't give the answer.
I need to pass the name of a variable as a parameter to a function and have the function assign a value to it.
assign_x <-…

Harlan Nelson
- 1,394
- 1
- 10
- 22