[R] function that uses a variable name as the parameter

Greg Snow Greg.Snow at imail.org
Wed Nov 12 17:54:46 CET 2008


David,

Having a function that changes global variables is very frowned on with few exceptions.  It falls into the category of things that you should never do unless you fully understand why you should never do them (also see fortune(36)).

However, what you want to do may be more of a macro than a function, see Thomas Lumley's article on macros in the September 2001 newsletter for some detail and the defmacro function in the gtools package to see if they fill your need.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of David Croll
> Sent: Wednesday, November 12, 2008 5:59 AM
> To: r-help at r-project.org
> Subject: [R] function that uses a variable name as the parameter
>
>
>
> Hello dear R people!
>
>
> Several times it occurred to me that a function that uses a variable
> name as a parameter would be helpful, but I did not find out how to
> write such a function. I have experience in several programming
> language, but I did not come across a helpful trick...
>
>
> What I want to have is...
>
> a <- 12 # starting value
>
> add <- function(variable,increment) {
>
> variable <- variable + increment
>
> }
>
> # by typing a and 25 for example, you specify that 25 will be added to
> the variable a...
>
> add(a,25)
>
> # the new a equals 12 + 25 = 37
>
> Thanks for all the help – I'll try to give my advice when I get across
> a problem I can solve!
>
>
> David
> --
> GMX Download-Spiele: Preizsturz! Alle Puzzle-Spiele Deluxe über 60%
> billiger.
> http://games.entertainment.gmx.net/de/entertainment/games/download/puzz
> le/index.html
>
> ______________________________________________
> 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.


More information about the R-help mailing list