Grouping commands so that variablas are removed automatically - like functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Grouping commands so that variablas are removed automatically - like functions

Rainer Krug-3
-----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:      [hidden email]

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-----

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Grouping commands so that variablas are removed automatically - like functions

jholtman
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 <[hidden email]> 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:      [hidden email]
>
> 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-----
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Grouping commands so that variablas are removed automatically - like functions

Rainer Krug-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



On 01/20/14, 14:27 , jim holtman wrote:
> Check out the use of the 'local' function:

True - have completely forgotten the "local" function.

Thanks,

Rainer

>
>
>> 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 <[hidden email]>
> wrote: 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
>
>>
>> ______________________________________________
>> [hidden email] 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.

- --
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:      [hidden email]

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJS3Sn/AAoJENvXNx4PUvmC7uIIAIkXdCNVCA1sqJ7jqODTWbG9
OrDTkhRD/IyR//39sCj5YC79peLbPkpKtQgnmoj7jMoNg2euxmCn3wIGLigWhy2w
cyGqh/TocfRnYVKyQXz4LC/IqVFAi+W9ymyevVDA0vQ9RcEYILEsXxjxl06VhZhS
wzOHOiXXdHka8xswjChPJRjA/17LQaStLYeEIQbukz3WCj1wTY68b6YixqlSh/BZ
7C91EULBQtTqV5OetvfV9lulicw0XyWp+ZcNvEa72Y3jZw5DX0LloLcRuuGLZf3N
dxmnB7Uj4kBArjupgfGtkwZzT1d3UX0bb3vqPt0TRoeJCT04XnupbpdpwUOhJ8c=
=0zID
-----END PGP SIGNATURE-----

______________________________________________
[hidden email] 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.