mshaffer February 2016

Using R, how to reference variable variables (or variables variable) a la PHP [revisited]

In a previous Using R, how to reference variable variables (or variables variable) a la PHP[post]

I asked a question about something in R analagous to PHP $$ function:

Using R stats, I want to access a variable variable scenario similar to PHP double-dollar-sign technique: http://php.net/manual/en/language.variables.variable.php

Specifically, I am looking for a function in R that is equivalent to $$ in PHP.

The get( response works for strings (characters).

lapply is a way to loop over lists

Or I can loop over and get the values ...

for(name in names(vars))
            {
            val = vars[[name]];

I still haven't had the $$ function in R answered, although the lapply solved what I needed in the moment.

`$$` <- function

that allows any variable type to be evaluated. That is still the question.


UPDATES


> mlist = list('four'="score", 'seven'="years");
> str = 'mlist$four'
> mlist
$four
[1] "score"

$seven
[1] "years"

> str
[1] "mlist$four"
> get(str)
Error in get(str) : object 'mlist$four' not found
> mlist$four
[1] "score"

Or how about attributes for an object such as mobj@index


UPDATES #2

So let's put specific context on the need. I was hacking the texreg package to build a custom latex output of 24 models of regression for a research paper. I am using plm fixed effects, and the default output of texreg uses dcolumns to center, which I don't like (I prefer r@{}l, so I wanted to wr

Answers


Mekki MacAulay February 2016

There is no equivalent function in R. get() works for all types, not just strings.


mshaffer February 2016

Here is what I came up with, after chatting with the R-bug group, and getting some ideas from them. KUDOS!

`$$` <- function(str)
    {
    E = unlist( strsplit(as.character(str),"[@]") );
        k = length(E);
        if(k==1)
            {
            eval(parse(text=str));
            } else {
                # k = 2
                nstr = paste("attributes(",E[1],")",sep="");
                nstr = paste(nstr,'$',E[2],sep="");

                if(k>2) {
                    for(i in 3:k)
                        {
                        nstr = paste("attributes(",nstr,")",sep="");
                        nstr = paste(nstr,'$',E[i],sep="");
                        }
                    }
                `$$`(nstr);
                }
    }

Below are some example use cases, where I can directly access what the str(obj) is providing... Extending the utility of the '$' operator by also allowing '@' for attributes.

model = list("four" = "score", "seven"="years");
str = 'model$four';
result = `$$`(str);
print(result);

matrix = matrix(rnorm(1000), ncol=25);
str='matrix[1:5,8:10]';
result = `$$`(str);
print(result);



## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14);
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69);
group <- gl(2, 10, 20, labels = c("Ctl","Trt"));
weight <- c(ctl, trt);
lm.D9 <- lm(weight ~ group);
lm.D90 <- lm(weight ~ group - 1); # omitting intercept

myA = anova(lm.D9); myA; str(myA);

str = 'myA@heading';
result = `$$`(str);
print(result);


myS = summary(lm.D90); myS; str(myS);


str = 'myS$terms@factors';
result = `$$`(str);
print(result);

str = 'myS$terms@factors@dimnames';
result = `$$`(str);
print(result);

str = 'myS$terms@dataClasses@names';
result = `$$`(str);
print(result);

After realizing the back-tick can be a bit tedio

Post Status

Asked in February 2016
Viewed 3,467 times
Voted 14
Answered 2 times

Search




Leave an answer