Re: [R] save intermediate result

From: Joerg van den Hoff <j.van_den_hoff_at_fzd.de>
Date: Sat, 05 May 2007 12:48:41 +0200

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



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. Received on Sat 05 May 2007 - 10:54:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 May 2007 - 11:31:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.