0

I'm trying to create a function that can evaluate multiple independent expressions. My goal is to input many expressions at once like myfunction(x = 2, y = c(5,10,11) , z = 10, ...), and use each expression's name and value to feed other functions inside of it. The transform() function works kind of like that: transform(someData, x = x*2, y = y + 1).

I know I can get the name and the value of an expression using:

> names(expression(x=2)) [1] "x" > eval(expression(x=2)) [1] 2 

However, I don't know how to pass those expressions through a function. Here is some of my work so far.

With unquoted expression (x=2) I could not pass it using the dots (...).

> myfunction <- function(...) { names(expression(...)) } > myfunction(x=2) expression(...) 

Now, using quotes. It gets the value but not the name. Parse structure is different from the tradicional expression. See class(expression(x=2)) and class(parse(text="x=2")), then str(expression(x=2)) and str(parse(text="x=2")).

> myfunction <- function(...) { assign("temp",...) results <- parse(text=temp) cat(names(results)) cat(eval(results)) } > myfunction("x=2") > 2 

So, any ideas?

2
  • 1
    If you ever have a function that does what you want, you can view the inside of it by looking at first 'methods' then 'getAnywhere'. For example: methods(transform); getAnywhere(transform.zoo). Here you can see the line e<-eval... where I believe it does what you want. Commented Nov 27, 2014 at 19:28
  • Thank you! I was trying to use fix(transform) instead of fix(transform.data.frame) to look inside the function. Commented Nov 27, 2014 at 19:46

1 Answer 1

2

It's unclear exactly what you want the return of your function to be. You can get the names and expressions passed to a function using

myfunction <- function(...) { x<-substitute(...()) #names(x) x } myfunction(x = 2, y = c(5,10,11) , z = 10) 

Here you get a named list and each of the items is an unevaluated expression or language object that you can evaluate later if you like.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.