[R] save intermediate result

jim holtman jholtman at gmail.com
Sat May 5 14:17:42 CEST 2007


Break your long running function into two parts.  The first will be
the long running part and it can return a value that you can pass to
the second part.  If the second part fails, you still have the
original value that was returned.

f1.1 <- function(){
    line1 <- f2()
    return(line1)
}

f1.2 <- function(line1){
    .... rest of the original f1
}

origData <- f1.1()
nextData <- f1.2(origData)


On 5/4/07, Weiwei Shi <helprhelp at gmail.com> 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...
> }
>
> 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.
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list