[R] Again: Variable names in functions

Heinz Tuechler tuechler at gmx.at
Fri Feb 18 03:05:38 CET 2005


At 08:32 17.02.2005 -0800, Berton Gunter wrote:
>
>I thought Thomas L. was clear, but apparently not...
>
>** Do not pass character string names as arguments to functions. ** Pass the
>objects (or expressions) which can consist of lists of vectors, dataframes,
>etc. instead. 
>
>If you need the names (e.g. as labels) you can use the deparse(substitute())
>construction. I strongly recommend that you study pp. 44-46 and section 3.5
>("Computing on the Language") of V&R's S PROGRAMMING. The point is that one
>can, of course, do things the way you want, but it makes life unnecessarily
>difficult and complex because R is set up to pass arguments by value and can
>keep better track of proper evaluation environments when this is done (which
>means you don't have to).
>
>-- Bert Gunter
>
Thank you for your advice. I will try to get the book as soon as possible
and meanwhiles study again other publications regarding these questions.
At the moment I am uncertain which way to prefer, since I am making my
first steps in R. Somehow I got the impression that there is a little
ambiguity concerning the use of object (variable) names in functions. My
little experience with R gave me the imression that much output
automatically has a sensible format (title, naming of estimates for
factors, ...) when you pass the object (=variable) name. If you don't do
it, you have more work to arrive at the same result. A title with the
variable's name is usually more informative than one with "get(x)". So for
a beginner like me it's temting to try to program functions in a way that
they work as similar as possible to the simple call from the R prompt.
Maybe objects like columns of data frames could have something like a name
attribute equal to their (column)name which is passed automatically to
functions.
Again, many thanks,
Heinz Tüchler

>> >
>> 
>> Thank you, this method works well. One step further I am again using
>> parse(), but maybe there is a better solution for that situation too.
>> The example would be a function, where I pass the variable 
>> name as string
>> instead of the name. The motivation for this is that it seems 
>> easier to
>> handle if I want to pass several variables (i.e. a vector of variable
>> names) to the function (as I learned recently from this help-list). 
>> In this case I have to use get(). In the case of calling table() the
>> variable name disappeares.
>> 
>> > alpha<-c(rep(1:5,10))
>> > name.alpha<-"alpha"
>> > mytable1<-function(x){print(table(get(x)))}
>> > mytable1(name.alpha)
>> 
>>  1  2  3  4  5 
>> 10 10 10 10 10 
>> 
>> If I use eval(parse()) instead, it works as expected. I tried several
>> combinations of eval() and substitute() but I did not find a solution.
>> Is there a similar "trick"?
>> 
>> > mytable2<-function(x){
>> +   string<-paste("print(table(",as.symbol(x),"))")
>> +   eval(parse(text=string))}
>> > mytable2(name.alpha)
>> alpha
>>  1  2  3  4  5 
>> 10 10 10 10 10 
>> 
>> 
>> Thanks,
>> Heinz Tüchler
>> 
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! 
>> http://www.R-project.org/posting-guide.html
>> 
>
>
>




More information about the R-help mailing list