[R] Grouping commands so that variablas are removed automatically - like functions

jim holtman jholtman at gmail.com
Mon Jan 20 14:27:03 CET 2014


Check out the use of the 'local' function:


> gc()
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 199420 10.7     407500 21.8   350000 18.7
Vcells 308004  2.4     786432  6.0   786424  6.0
> result <- local({
+     a <- rnorm(1000000)  # big objects
+     b <- rnorm(1000000)
+     mean(a + b)  # return value
+ })
>
> result
[1] 0.0001819203
> gc()
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 199666 10.7     407500 21.8   350000 18.7
Vcells 308780  2.4    2975200 22.7  3710863 28.4
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Mon, Jan 20, 2014 at 8:12 AM, Rainer M Krug <Rainer at krugs.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi
>
> I would like to group commands, so that after a group of commands has
> been executed, the variables defined in that group are automatically
> deleted.
>
> My reasoning: I have a longer sript which is used to load data, do
> analysis and plot graphs, all part of a document (in org-mode /
> emacs).
>
> I have several datasets which are loaded, and each one is quite
> big. So after doing one part of the job (e.g. analysing the data and
> storing the results) I want to delete all variables used to free space
> and to avoid having these variables being used in the next block and
> still having the old (for this block invalid) values.
>
> I can't use rm(list=ls()) as I have some variables as "constants"
> which do not change over the whole document and also some functions
> defined.
>
> I could put each block in a function and then call the function and
> delete it afterwards, but this is as I see it abusing functions.
>
> I don't want to keep track manually of the variables.
>
> Therefore my question:
>
> Can I do something like:
>
> x <- 15
>
> { #here begins the block
> a <- 1:100
> b <- 4:400
> } # here ends the block
>
> # here are and b not defined anymore
> # but x is still defined
>
> {} is great for grouping the commands, but the variables are not
> deleted afterwards.
>
> Am I missing a language feature in R?
>
> Rainer
>
> - --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Stellenbosch University
> South Africa
>
> Tel :       +33 - (0)9 53 10 27 44
> Cell:       +33 - (0)6 85 62 59 98
> Fax :       +33 - (0)9 58 10 27 44
>
> Fax (D):    +49 - (0)3 21 21 25 22 44
>
> email:      Rainer at krugs.de
>
> Skype:      RMkrug
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQEcBAEBAgAGBQJS3SDSAAoJENvXNx4PUvmCEAwH/jBCuQLRpRcPu+PSrUBsck8v
> 49q3f0wAZqhyfjMQvRnLSECAfQN4GHI1WvXcuC9R8Z0eokL7gAqMnJSgWd61Un0F
> I+yClK1qbhpCwR8WV4nDXTuEW5rb5d8a1iHRPxXXSi/vdJZL3imWMsfvGTpgIhVw
> Dbi7+BSh52ZFEZPIyTm2+4qBfQA2ZaY3AEPTjBdB4iL603S+lpgmm1mAInFHFx5g
> 0CzzY3feTWreD+EATXMGofTDaoxR5vuLvIRvv+PA/Ehz/hVnQah2xriL4NR+pIHz
> 7WbqiReJ8H1ruAgtW6o8CmQRMArHmk0oBy1vYQvwB7SZ8/DOyKkArKBy8tGx/J0=
> =dBo5
> -----END PGP SIGNATURE-----
>
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list