On 29/04/2008 9:53 AM, Julien Roux wrote:
Hi list,
I created a function to plot my data:
plot_function(vector)
I want to write the name of the argument vector in the legend/title of the plot.
For example if I call:
 > plot_function(my_vector)
I want "my_vector" to be written in the legend or title and so retrieve the name of this object as a string.

Is it possible to achieve this?

Yes. If the argument name is arg in the function definition, then substitute(arg) gives the expression that was passed, and deparse(substitute(arg)) converts it into a string to use in a title.

e.g.

> plot_function <- function(arg) {
+   deparse(substitute(arg))
+ }
> plot_function(my_vector)
[1] "my_vector"

Duncan Murdoch

Thanks a lot for your help
Julien


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to