[R] Parameters/data that live globally

Charles C. Berry cberry at tajo.ucsd.edu
Wed Jan 19 03:41:56 CET 2011


On Tue, 18 Jan 2011, analyst41 at hotmail.com wrote:

> I am coming to R from Fortran and I used to use fixed size arrays in
> named common. common /name1/array(100)
>
> The contents of array can be accessed/modified if and only if this
> line occurs in the function.  Very helpful if different functions need
> different global data (can have name2, name3 etc. for common data
> blocks).
>
> Is there a way to do this in R?

Sure.

But probably better to work in R as the developers intended, rather than 
trying to simulate COMMON blocks per se.

See

 	?list
 	?environment
 	?search
 	10.7 Scope in Intro to R
 	?get
 	?assign

You can collect objects in an environment and pass it to a function or put 
it in the search path. Then you can get/assign objects.

Probably it is easier to work with a list. Here is a toy example

> foo <- function(object){ object$x <- object$y + 1; object}
> my.list <- list( y = 4:5 )
> my.list <- foo(my.list)
> my.list
$y
[1] 4 5

$x
[1] 5 6

>

HTH,

Chuck


>
> Thanks for any help.
>
> ______________________________________________
> 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.
>

Charles C. Berry                            Dept of Family/Preventive Medicine
cberry at tajo.ucsd.edu			    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list