Well, R does not do references like some other languages do (I am not familiar 
with Igor).  Partly because of the danger that references pose (you don't know 
if a function has just changed you data or not).  If you use environments, then 
you can work with objects in a reference like manner (but that is often more 
complicated then you need).  The main argument that I have heard for references 
is to save your function from making a copy of a large data object.  But with 
R's copy-on-modify behavior, you already have that benefit without needing the 
references.

The typical R idiom for having your function have access to an object and its 
original name is to use deparse(substitute(...)) on the object rather than 
using get with the name, e.g.:

> testfunc <- function(x) {
+ cat('the value of x is',x,'\n and the name of x is',
+ deparse(substitute(x)),'\n\n')
+ }
>
> myvar <- c(1,4,89)
> testfunc(myvar)
the value of x is 1 4 89
 and the name of x is myvar

hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111

From: Payam Minoofar [mailto:payam.minoo...@meissner.com]
Sent: Monday, June 15, 2009 10:39 AM
To: Greg Snow; r-help@r-project.org
Subject: Re: Referencing data frames

You are, indeed, correct. I'm still used to the IGOR Pro programming language 
(for which I even wrote an introductory manual, http://payam.minoofar.com/igor/)

The feedback I've gotten has been fantastic, and I know how I'm going to 
proceed from this point forward.

Nevertheless, I'll solicit more feedback while I have willing "ears".

I would like to have a function acquire an object by reference, and within the 
function create new objects based on the original object and then use the name 
of the original object as the base for the names of the newly created objects.

It seems to me that the optimal way of doing this is to have the function 
acquire the name of the object as a string, and then use get() to access the 
object, and then to use the same string to do the name formation of the new 
objects.

IGOR Pro has a built-in function that returns the name of an object as a 
string, and I was wondering if R has a similar facility.

Yes, I'm definitely getting used to the R language, but I've made tremendous 
progress thanks to the list , and the power of R is abundantly clear to me, as 
is the abstruseness of the language. :)

Payam


On 6/15/09 9:29 AM, "Greg Snow" <greg.s...@imail.org> wrote:
Generally when someone asks a question like this, they are trying to program in 
a different language using R rather than taking advantage of the power of R 
itself.  If you give us more information on what you are trying to accomplish, 
then we may be able to give better advice on how to accomplish your final goal 
using the power of R.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111

> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
> project.org] On Behalf Of Payam Minoofar
> Sent: Friday, June 12, 2009 4:00 PM
> To: r-help@r-project.org
> Subject: [R] Referencing data frames
>
> Hi,
>
> How do I use the string content of a string variable to reference a
> data frame of the same name? I want to do the typical tasks of 1)
> building a name with a string variable and using the string variable to
> create a data frame (or any object) whose name is the string value of
> the variable and 2) pass on a string to a function as a parameter, and
> then use that string to refer to an existing data frame.
>
> Thanks in advance.
>
> Payam
> --
> Payam Minoofar, Ph.D.
> Scientist
> Meissner Filtration Products
> 4181 Calle Tesoro
> Camarillo, CA 93012
> USA
> +1 805 388 9911
> +1 805 388 5948 fax
> payam.minoo...@meissner.com
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to