extract argument names

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

extract argument names

RKoenker
I have a vector of character strings that look like R expressions:

 > a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep =  
"")
 > a
[1] "qss(x1, lambda =100)" "qss(x2, lambda =100)" "qss(x3, lambda =100)"
[4] "qss(x4, lambda =100)" "qss(x5, lambda =100)" "qss(x6, lambda =100)"

That I would like to operate on to obtain the names of the first  
argument, i.e.

 > foo(a)
[1] "x1" "x2" "x3" "x4" "x5" "x6"

I thought there was some simple idiom involving deparse, but it is  
eluding
my searches.


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820

______________________________________________
[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: extract argument names

Patrick Burns
unlist(lapply(parse(text=a), function(x) deparse(x[[2]])))

seems to do the job.


Patrick Burns
[hidden email]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")

roger koenker wrote:

> I have a vector of character strings that look like R expressions:
>
> > a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep = "")
> > a
> [1] "qss(x1, lambda =100)" "qss(x2, lambda =100)" "qss(x3, lambda =100)"
> [4] "qss(x4, lambda =100)" "qss(x5, lambda =100)" "qss(x6, lambda =100)"
>
> That I would like to operate on to obtain the names of the first
> argument, i.e.
>
> > foo(a)
> [1] "x1" "x2" "x3" "x4" "x5" "x6"
>
> I thought there was some simple idiom involving deparse, but it is
> eluding
> my searches.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    [hidden email]            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: extract argument names

D. Rizopoulos
In reply to this post by RKoenker
one way seems to be the following:

a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep = "")
all.vars(parse(text = a))


I hope it helps.

Best,
Dimitris


roger koenker wrote:

> I have a vector of character strings that look like R expressions:
>
>  > a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep = "")
>  > a
> [1] "qss(x1, lambda =100)" "qss(x2, lambda =100)" "qss(x3, lambda =100)"
> [4] "qss(x4, lambda =100)" "qss(x5, lambda =100)" "qss(x6, lambda =100)"
>
> That I would like to operate on to obtain the names of the first
> argument, i.e.
>
>  > foo(a)
> [1] "x1" "x2" "x3" "x4" "x5" "x6"
>
> I thought there was some simple idiom involving deparse, but it is eluding
> my searches.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    [hidden email]            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
> ______________________________________________
> [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.
>

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
[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: extract argument names

macrakis
In reply to this post by RKoenker
a1 <- "qss(x1,lambda=100)"
> parse(text=a1)[[1]][[2]]
x1

This will not work for length(a) != 1, so you have to explicitly map
over your list, e.g.

a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep = "")
> dput( lapply(a,function(x)parse(text=x)[[1]][[2]]) )
list(x1, x2, x3, x4, x5, x6)

paste(lapply(a,function(x)parse(text=x)[[1]][[2]]),collapse=", ")
[1] "x1, x2, x3, x4, x5, x6"

          -s

On Tue, Apr 7, 2009 at 3:26 PM, roger koenker <[hidden email]> wrote:

> I have a vector of character strings that look like R expressions:
>
>> a <- paste("qss(",paste("x",1:6,sep = "") ,", lambda =100)", sep = "")
>> a
> [1] "qss(x1, lambda =100)" "qss(x2, lambda =100)" "qss(x3, lambda =100)"
> [4] "qss(x4, lambda =100)" "qss(x5, lambda =100)" "qss(x6, lambda =100)"
>
> That I would like to operate on to obtain the names of the first argument,
> i.e.
>
>> foo(a)
> [1] "x1" "x2" "x3" "x4" "x5" "x6"
>
> I thought there was some simple idiom involving deparse, but it is eluding
> my searches.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    [hidden email]            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
> ______________________________________________
> [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.