[R] Changing global variables from functions

jtouyz jtouyz at uwaterloo.ca
Tue Mar 16 16:04:08 CET 2010


Hey all,
I'm relatively new to the R-environment. I'm having a bit of trouble with
encapsulation.
I have a globally declared variable that doesn't update it when I change it
in a function. For example when I run the following function 

>deckn<-NULL
>deck1<-1  #52 card deck
>deck<-function()
{
#Creating a standard deck
deck1<-c(1:52)
deckn<-deck1
    #Creating n decks
    for (i in 2:num_decks)
    {
    deckn<-c(deckn,52*i+deck1-1)
    }
}
>deckn
>NULL

it returns NULL for deckn instead of a vector of values. Is there an easy
fix to update deckn in the function so that it outputs a vector of values (
I don't wish the function to return a value just update the current one)? 

Thanks in advance,
Josh Elliott
-- 
View this message in context: http://n4.nabble.com/Changing-global-variables-from-functions-tp1595002p1595002.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list