[R] what's the best way to save global variables?

Stephen D. Weigand weigand.stephen at charter.net
Fri Sep 16 05:41:20 CEST 2005


Johan,

On Sep 15, 2005, at 11:39 AM, johan Faux wrote:

> I am writing a kind of long program in R and I have some variables 
> which I want to be globals. Where should I save them?  I was thinking 
> to create a function wich initialize all the global variables and then 
> whenever I need them, I call this function.

In most cases, you would write a function that would
return an object of class list, the components of
which would be the values you want to use later.
For example

myfun <- function([stuff]){

  [stuff]

  return(list = (var1 = val1, var2 = val2)
}

and your call would be

glob <- myfun([stuff])

and you would access val1 with

glob$var1


> What if I create a file glob.R with
> var1<-val1
> var2<-val2
> .....
> etc.
>
> How do I include this file in my other files/function . Is there in R 
> some kind of   include("glob.R") or something?
> thank you,
> Johan
>

Use

source("glob.R")

Check out "An introduction to R" 
(http://cran.r-project.org/doc/manuals/R-intro.html).

Stephen




More information about the R-help mailing list