Re: [R] Get inside a function the name of a variable called as argument?

From: Hans Ekbrand <hans.ekbrand_at_sociology.gu.se>
Date: Tue, 29 Apr 2008 16:30:51 +0200

On Tue, Apr 29, 2008 at 03:53:02PM +0200, 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?

While it might be possible, I think it would be better to use an extra argument for this:

plot_function(my_vector, title = my_title)

Functions should be general, and relying on the name of the variable makes your function less general. What if you in the future want to use plot_function with an anynmous vector created dynamically? e.g by combining two other vectors:

plot_function(c(foo, bar))

--
Hans Ekbrand (http://sociologi.cjb.net) <hans@sociologi.cjb.net>
GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E

______________________________________________ R-help_at_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.

Received on Tue 29 Apr 2008 - 16:49:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 17:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive