access objects

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

access objects

Berry Boessenkool


hi,

say I have consecutively numbered objects obj1, obj2, ... in my R workspace.
I want to acces one of them inside a function, with the number given as an argument.

Where can I find help on how to do that? Somebody must have been trying to do this before...
Some keywords to start a search are appreciated as well.

Here's an example, I hope it clarifies what I'm trying to do:

obj1 <- 7:9
obj2 <- 6:2
testf <- function(k) plot(  noquote(paste("obj", k, sep=""))  )
testf(1) # should plot obj1



-------------------------------------
Berry Boessenkool
D-14476 Potsdam (OT Golm)
-------------------------------------
     
______________________________________________
[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: access objects

Michael Weylandt
The get() function should get you where you need.

Michael Weylandt

On Wed, Sep 7, 2011 at 12:53 PM, Berry Boessenkool <
[hidden email]> wrote:

>
>
> hi,
>
> say I have consecutively numbered objects obj1, obj2, ... in my R
> workspace.
> I want to acces one of them inside a function, with the number given as an
> argument.
>
> Where can I find help on how to do that? Somebody must have been trying to
> do this before...
> Some keywords to start a search are appreciated as well.
>
> Here's an example, I hope it clarifies what I'm trying to do:
>
> obj1 <- 7:9
> obj2 <- 6:2
> testf <- function(k) plot(  noquote(paste("obj", k, sep=""))  )
> testf(1) # should plot obj1
>
>
>
> -------------------------------------
> Berry Boessenkool
> D-14476 Potsdam (OT Golm)
> -------------------------------------
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: access objects

Berry Boessenkool


Thanks!

That's exactly it.
get gets it ;-)

Berry


From: [hidden email]
Date: Wed, 7 Sep 2011 14:54:48 -0500
Subject: Re: [R] access objects
To: [hidden email]
CC: [hidden email]

The get() function should get you where you need.

Michael Weylandt

On Wed, Sep 7, 2011 at 12:53 PM, Berry Boessenkool <[hidden email]> wrote:






hi,



say I have consecutively numbered objects obj1, obj2, ... in my R workspace.

I want to acces one of them inside a function, with the number given as an argument.



Where can I find help on how to do that? Somebody must have been trying to do this before...

Some keywords to start a search are appreciated as well.



Here's an example, I hope it clarifies what I'm trying to do:



obj1 <- 7:9

obj2 <- 6:2

testf <- function(k) plot(  noquote(paste("obj", k, sep=""))  )

testf(1) # should plot obj1







-------------------------------------

Berry Boessenkool

D-14476 Potsdam (OT Golm)

-------------------------------------



______________________________________________

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


     
        [[alternative HTML version deleted]]

______________________________________________
[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: access objects

PIKAL Petr
Hi

Beware of shooting in your leg. Instead of poisoning your workspace with
numerous objects called "obj.n" or something like that you help yourself
with creating single object of type list. Parts of a list can be accessed
much more easily than this kind of get construction.

I use R quite a long time but I hardly remember a single event when I used
such construction; I always used lists.

In your example

lll<-vector("list", 2)
lll[[1]] <- 7:9
lll[[2]] <- 6:2
testf <- function(k) plot(lll[[k]])
testf(1)

Regards
Petr


> Thanks!
>
> That's exactly it.
> get gets it ;-)
>
> Berry
>
>
> From: [hidden email]
> Date: Wed, 7 Sep 2011 14:54:48 -0500
> Subject: Re: [R] access objects
> To: [hidden email]
> CC: [hidden email]
>
> The get() function should get you where you need.
>
> Michael Weylandt
>
> On Wed, Sep 7, 2011 at 12:53 PM, Berry Boessenkool
> <[hidden email]> wrote:
>
> hi,
>
> say I have consecutively numbered objects obj1, obj2, ... in my R
workspace.
>
> I want to acces one of them inside a function, with the number given as
anargument.
>
> Where can I find help on how to do that? Somebody must have been trying
to

> do this before...
>
> Some keywords to start a search are appreciated as well.
>
>
>
> Here's an example, I hope it clarifies what I'm trying to do:
>
>
>
> obj1 <- 7:9
>
> obj2 <- 6:2
>
> testf <- function(k) plot(  noquote(paste("obj", k, sep=""))  )
>
> testf(1) # should plot obj1
>
>
>
>
>
>
>
> -------------------------------------
>
> Berry Boessenkool
>
> D-14476 Potsdam (OT Golm)
>
> -------------------------------------
>
>
>
> ______________________________________________
>
> [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.
>
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.