[R] save intermediate result

Joerg van den Hoff j.van_den_hoff at fzd.de
Sat May 5 12:48:41 CEST 2007


On Fri, May 04, 2007 at 04:55:36PM -0400, Weiwei Shi wrote:
> sorry for my English, staying in US over 7 years makes me think I were
> ok, now :(
> 
> anyway, here is an example and why I need that:
> 
> suppose I have a function like the following:
> 
> f1 <- function(){
> line1 <- f2() # assume this line takes very long time, like more than 30 min
> # then I need to save line1 as an object into workspace b/c
> # the following lines after this might have some bugs
> # currently I use
> save(line1, file="line1.robj")
> # but I am thinking if there is anothe way, like enviroment, instead
> of save it into a file
> 
> # codes as followed might have some bugs
> # blabla...
> }

yes, that's what I thought you meant. so use

line1 <<- f2()

i.e. the assignment operator `<<-' instead of `<-'. after f2() is
finished, the object `line1' is available from the R prompt, i.e.
visible in the workspace (for details, cf. `help("<<-")').

> 
> if some codes as followed have bugs, my f1 function cannot return
> anything, which means I have to re-run the time-consuming line again.

you probably should debug it using some dummy data instead of the f2()
call...

> 
> thanks,
> 
> Weiwei
> 
> On 5/4/07, Joerg van den Hoff <j.van_den_hoff at fzd.de> wrote:
> >On Fri, May 04, 2007 at 03:45:10PM -0400, Weiwei Shi wrote:
> >> hi,
> >>
> >> is there a way to save a R object into workspace instead of into a
> >> file during a running of function?
> >>
> >
> >if I understand the question correctly you want the 'super-assignment'
> >operator `<<-' as in
> >
> >-------------cut---------------
> >R> f <- function(x) y <<- 2*x
> >R> f(1)
> >R> y
> >[1] 2
> >R> f(10)
> >R> y
> >[1] 20
> >-------------cut---------------
> >
> >i.e. y is assigned a value in the 'workspace'. be careful, though, with
> >this kind of assignment. why not return the object you are interested
> >in as a list component together with other results when the function
> >call finishes?
> >
> >
> 
> 
> -- 
> Weiwei Shi, Ph.D
> Research Scientist
> GeneGO, Inc.
> 
> "Did you always know?"
> "No, I did not. But I believed..."
> ---Matrix III



More information about the R-help mailing list