1

Let's say I have the following function:

foo = function(x, y){
    plot(x, y)
}

When I run foo(1:10, 11:20), how do I get the axis labels to say 1:10 and 11:20 rather than x and y?

0

1 Answer 1

4

If that's what you want, you can do

foo = function(x, y, xlab=deparse(substitute(x)), ylab=deparse(substitute(y))){
  plot(x, y, xlab=xlab, ylab=ylab)
}
foo(1:10, 11:20)

This will default to the exact values you pass, but you can override by setting explicitly values.

Alternativly, if you just want to essentially change the call from your function to the plot function and pass everything though, you could do something like

foo = function(x, y){
  call <- match.call()
  call[[1]] <- quote(plot)
  eval.parent(call)
}
Sign up to request clarification or add additional context in comments.

3 Comments

The top answer is very helpful. I'm trying to figure out how to modify it so that the xlab and ylab parameters are not in the parameter list but can optionally be accessed by "...". I've been trying to figure out exists() or missing() but am stuck in figuring out how to search the right environment only. Any suggestion of how to tuck these new parameters internally within the function?
I’m not sure I understand what you want to change. You want to hard code the clause do they can never be overridden? You can just move them out of the argument list into the body of the function, though that makes the function far less flexible. It’s very difficult to mix default clause with ... of the values aren’t in the formal argument list.
Okay, that's helpful to know. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.