Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture function arguments as strings in R?

Tags:

r

tidyeval

I want to capture the incoming arguments that I'm passing to a function. How can I do this? The function below isn't quite what I want. The output I want is "Using mtcars and am". I was under the impression rlang could help with this, but I haven't been able to find a function to get the job done.

fx_capture<- function(fx_data, fx_var) {
  name_data <- quote(fx_data)
  name_var  <- quote(fx_var)
  paste("Using", name_data, "and", name_var)
}

fx_capture(mtcars, am)
> "Using fx_data and fx_var"
like image 568
kputschko Avatar asked Oct 29 '25 01:10

kputschko


1 Answers

We can use deparse(substitute(arg))

fx_capture<- function(fx_data, fx_var) {

     name_data <- deparse(substitute(fx_data))
     name_var <- deparse(substitute(fx_var))

     paste("Using", name_data, "and", name_var)
}

fx_capture(mtcars, am)

Or with match.call

fx_capture<- function(fx_data, fx_var) {

   paste0("Using ", do.call(paste, c(lapply(match.call()[-1], 
              as.character), sep = ' and ')))
  
  
}

fx_capture(mtcars, am)
like image 131
akrun Avatar answered Oct 30 '25 17:10

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!