Questions tagged [non-standard-evaluation]

156 questions
0
votes
0 answers

Custom function does not work properly unless the object is stored in the global environment in R

Context I have a custom function myfun1 that fits the cox model. Before fitting the model, I need to do a bit of processing on the data used to fit the model. Specifically, run two lines of code, dd = datadist(data) and options(datadist = 'dd'). If…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
2 answers

inject() function from rlang package cannot work with Predict() function from rms package in R

Context I am learning use tidy eval to write my own function. If I calculate hr1 without using tidy eval, the code works fine, such as: hr1 = Predict(fit, meal.cal = q, fun = exp, ref.zero = TRUE) When I use rlang::inject() and rms::Predict()…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
1 answer

can not use non-standard evaluation in rms pacakge in R

Context I would like to customize a function that uses non-standard evaluation (NSE) to fit the cox model. It works fine when I fit the cox model using the code. But when I wrap the code into a custom function using NSE it reports an…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
1 answer

Custom function: update old variables and create new variables/ non standard evaluation

I would use some help with a tidyverse solution for a function that I've custom written. I have a dataset with a binary phenotype and an associated diagnosis date, as well as 2 other dates I hope to use to update and create new variables. I want…
0
votes
1 answer

subvert external function's `deparse(substitute())` without `eval`

I'd like to wrap around the checkmate library's qassert to check multiple variable's specification at a time. Importantly, I'd like assertion errors to still report the variable name that's out of spec. So I create checkargs to loop through input…
Will
  • 1,206
  • 9
  • 22
0
votes
1 answer

can not understand Curly Curly in tidyeavl in r

I used {{}} to bulid a self-function in r. I don't understand why this happend. test.data = data.frame(pm10 = 1:5) test.data new.col.name = 'lag_pm10' col = 'pm10' test.data %>% # error mutate({{new.col.name}} := lag({{col}}, 2)) test.data %>%…
zhiwei li
  • 1,635
  • 8
  • 26
0
votes
0 answers

Get string name of function piped into another function

While passing in a function into another function, I want the string name of this function. I also want this to work with the {magrittr} pipe operator. This is very similar to this question. But when I try it with the pipe, it…
Eric Leung
  • 2,354
  • 12
  • 25
0
votes
1 answer

substitute within a function with list as environment

I am trying to understand the behavior of substitute here, here are my examples, If you run both the examples, f1 and f2 gives you "b" and "k" as answers. The only difference in both the codes is that, I have added list(y = 'z') which doesn't…
DeepR
  • 17
  • 5
0
votes
2 answers

How to convert a column to a different type using NSE?

I'm writing a function that takes a data frame and a column names as arguments, and returns the data frame with the column indicated being transformed to character type. However, I'm stuck at the non-standard evaluation part of dplyr. My current…
A. Stam
  • 2,148
  • 14
  • 29
0
votes
2 answers

Pass vector of symbols as function argument and convert to character vector

I have a function that I want to pass as an argument a vector of symbols and then internally I want to convert that vector to a character vector. Minimal example: fun <- function(symbols = c(a, b, c)) { # code to convert to character vector…
Will Hipson
  • 366
  • 2
  • 9
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
1 answer

Using non-standard evaluation to call an argument in a nested function

I am trying to take an argument from a simple function "adder" and then use a loop to look at the effect of incrementing that argument. I know there must be better approaches, such as building a single function that makes a longer data frame or…
0
votes
1 answer

in R, eval is not evaluating in the correct environment

z <- 1 a <- function() { print(parent.frame()) eval(quote({print(z)})) } b <- function() { z <- 2 print(environment()) a() } b() # output: [1] 1 According to…
0
votes
1 answer

using = inside bquote inside dplyr function calls

a <- "A" bquote(tibble::tibble(a = 1:10) %>% dplyr::mutate(`=`(.(as.name(a)), a*2))) %>% eval() # A tibble: 10 x 2 a `A = a * 2` 1 1 2 2 2 4 3 3 6 4 4 8 5 …
0
votes
1 answer

Identifying whether a named function was supplied as function argument

As part of a project I've been working on I would like to allow the user to return a summary of various inputs of a nested object structure. A part of the objects are used as containers or formatters, while the remainders are used to parse other…
Oliver
  • 8,169
  • 3
  • 15
  • 37