Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R language Functions within lists

Tags:

object

list

r

G'Day, I am a newbie at R and I have GOOGLED and read books and had lots of play, but I can't seem to figure out if what I am doing is implemented. It compiles (no interpreter spit) and can be called (again no spit), it just doesn't seem to want to do anything.

OK. SYNOPSIS.

I read that lists in R are the OBJECTS of other languages. So just for a Saturday and Sunday play I have been trying to get this to work.

GLOBAL <- list( counter = 1,
                locked = FALSE,
                important_value = 42,
                copy_of_important_value = 42,
                lock = function() { GLOBAL$locked = TRUE },
                unlock = function() { GLOBAL$locked = FALSE },
                is_locked = function() { return(GLOBAL$locked )},
                visit = function() { GLOBAL$counter <- GLOBAL$counter + 1 })

> GLOBAL$locked
[1] FALSE
> 

This works...

> GLOBAL$locked <- TRUE
> GLOBAL$locked
[1] TRUE
> 

This does not

> GLOBAL$unlock()
> GLOBAL$locked
[1] TRUE
>

Has R got a $this or $self construct? None of this generates any errors. Just doesn't seem to want to do anything! (functions that is). I suppose I could set up a function as a routing access table, but I thought the encapsulation would be nifty.

Second question. It has been mentioned to me several times that R MUST keep all data in memory, and that is a limitation. Does that include swp on *NIX systems? I mean, if you had a humungus matrix could you just add some swap to make it fit?

Sorry for dumb newbie questions

like image 399
Addinall Avatar asked Dec 26 '22 05:12

Addinall


1 Answers

This can be done using proto objects:

library(proto) # home page at http://r-proto.googlecode.com

GLOBAL <- proto( counter = 1,
                 locked = FALSE,
                 important_value = 42,
                 copy_of_important_value = 42,
                 lock = function(.) { .$locked = TRUE },
                 unlock = function(.) { .$locked = FALSE },
                 is_locked = function(.) { return(.$locked )},
                 visit = function(.) { .$counter <- .$counter + 1 })

GLOBAL$locked <- TRUE
GLOBAL$unlock() 
GLOBAL$locked
## FALSE
like image 78
G. Grothendieck Avatar answered Jan 10 '23 09:01

G. Grothendieck