Hi Alex,


you could use something like this:

printComponent <- function(component, lis){
   if(component %in% names(lis)) return(lis[component])
   else cat(component, "in not an element of", substitute(lis), "\n")
}
##############
my.list <- list(A=1:3, B=c("Brave", "new", "world"))
printComponent("A", my.list)
printComponent("C", my.list)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
    http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- From: "Alex Sanchez" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, January 26, 2005 10:54 AM
Subject: [R] string evaluation



Hello,

I would like to be able to compose an expression using some of the arguments I give to a function, that is assuming I have a list such as:
my.list<-list(A=1:3,B=c("Brave", "new", "world"))
and a function
printComponent<-function(component){
if (component=="A"||component=="B"){
my.expression<-paste("my.list",component,sep="$")
print(my.expression)}
else{cat("Wrong argument, must be A or B")}
}


I would like that a call "printComponent("A")" to be equivalent to calling
my.list$A
[1] 1 2 3
but I only obtain
printComponent("A")
[1] "my.list$A"

I presume I must evaluate it somehow but I really can't find the instruction in the manuals (I would also appreciate to know where should I have looked at)

Thanks for the help

----------------------------------------------------------
Dr. Alex Sánchez
Departament d'Estadística U.B.
Telf: 34 934021590
Fax: 34 93 4111733
e-mail : [EMAIL PROTECTED]
-----------------------------------------------------------

[[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to