views:

949

answers:

3

Hello all,

I am a newbie in R programming. Though I am poking into the manuals, I also wanted to ask the community "How can we set global variables inside a function?"

Any pointers will help.

Question-2: Regarding plotting,

I am using plotting multiple graphs in a single sheet, and to differentiate each one of them, I want to add title for each one of them. Can anyone tell me how I can achieve this?

+1  A: 

Dan Goldstein provides a search engine for R that gives you fast answers to questions like setting global variables. For global variables there a solution in a mailinglist posting:

a <- "old" test <- function () {
   assign("a", "new", envir = .GlobalEnv)
} test() a
Christian
+2  A: 

Use one post per main question.

1) As the first answer with assign() showed you, there is a way to assign in the global environment. A simpler, shorter (but not better ... stick with assign) way is to use the <<- operator, ie

a <<- "new"

inside the function.

2) For your plots, use main="My title here" for each plot. Use something like par(mar=c(3,3,3,1)) to give sufficient spacing.

Dirk Eddelbuettel
+2  A: 

Why are you trying to create global variables from inside a function? It is very unlikely that this is a good idea and you should post more details about what you are trying to achieve.

hadley
I am very new to R programming, and hence using globals to pass as parameters ;-) ... I am also learning parameter passing and things like that ... slowly though !!!BTW, I am trying to set a global variable inside my function, and **not** create it.
Alphaneo