On 07/09/11 08:47, Khanvilkar, Shashank wrote:
> Hello All,
> Thanks in advance for all help.
>
> In my prog, I have a global list object that is used as a container for 
> storing some data frames.
> Here is an example code.
> --SNIP--
> temp1<- function(X){
>      statName = c("Mean", "stdDev", "NumSamples")
>      statVal = c("0.5", "0.51", "5")
>
>      r = data.frame(statName=statName, statVal = statVal)
>      X[["temp1"]] = r
> }
>
> reportList = list()
> temp1(reportList)
> print(reportList)
> --SNIP--
>
> I was expecting that the new data frame in temp1 would get added to the list 
> (reportList), but it doesn't, probably because, arguments to functions are 
> passed "Call-by-value" and loose all locally made changes.
>
> Any ideas how I can do this.

Don't.  This is not the R-ish way of doing things.  You can do it using 
"<<-"
or "assign()" but this is ***BAD***.  See:

     fortune("<<-")

The R-ish way to do things is have your function return the value you want
and assign that value to the object you want to up-date:

>     temp1 <- function(X){
>         statName <- c("Mean", "stdDev", "NumSamples")
>         statVal <-  c("0.5", "0.51", "5")
>         X[["temp1"]] <- data.frame(statName=statName, statVal = statVal)
>         X
>     }
>
>     reportList <- list()
>     reportList <- temp1(reportList)
>     print(reportList)
>

     cheers,

         Rolf Turner


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to