I have a numeric vector of lenth 1. I am trying to use it inside a function just by giving its name, rather than specifying it as an argument to the function. I am aware that there is an attach function which you need to call. The attach function will accept a list. However, I don't seem to be able to create the list properly. (Or should I use a frame instead?) free_driver <- function (){ i <- numeric (1) attach (as.list (i)) i <- 25 free_test () } free_test <- function (){ print ("i =") print (i) return () } Anyway, here is the output, starting with the load operation: ------------------------------------------------------------------> free_driver <- function (){+ i <- numeric (1) + attach (as.list (i)) + i <- 25 + + free_test () + + }> free_test <- function (){+ print ("i =") + print (i) + return () + + }> free_driver ()Error in attach(as.list(i)) : all elements of a list must be named>-------------------------------------------------------------- Is there an easy way to name all elements of a list? Your advice? Tom Jones
On Thu, 2007-11-22 at 03:16 -0500, Thomas L Jones, PhD wrote:> I have a numeric vector of lenth 1. I am trying to use it inside > a function just by giving its name, rather than specifying it as > an argument to the function. I am aware that there is an attach > function which you need to call. The attach function will accept a > list. However, I don't seem to be able to create the list properly. (Or > should I use a frame instead?) > > free_driver <- function (){ > i <- numeric (1) > attach (as.list (i)) > i <- 25 > > free_test () > > } > free_test <- function (){ > print ("i =") > print (i) > return () > > } > > Anyway, here is the output, starting with the load operation: > > ------------------------------------------------------------------ > > > free_driver <- function (){ > + i <- numeric (1) > + attach (as.list (i)) > + i <- 25 > + > + free_test () > + > + } > > free_test <- function (){ > + print ("i =") > + print (i) > + return () > + > + } > > free_driver () > Error in attach(as.list(i)) : all elements of a list must be named > > > -------------------------------------------------------------- > > Is there an easy way to name all elements of a list? > > Your advice?Hi Tom, If I understand your question is possible solve yours problem with this code free_driver <- function (){ i<-as.numeric(25) i <- list(i) free_test (i) } free_test <- function (i){ cat ("i =",i[[1]],"\n") } free_driver() -- Bernardo Rangel Tura, M.D,Ph.D National Institute of Cardiology Brazil
Thomas,> I have a numeric vector of lenth 1. I am trying to use it inside > a function just by giving its name, rather than specifying it as > an argument to the function. I am aware that there is an attach > function which you need to call. The attach function will accept a > list. However, I don't seem to be able to create the list properly. (Or > should I use a frame instead?) > > free_driver <- function (){ > i <- numeric (1) > attach (as.list (i)) > i <- 25 > > free_test () > > } > free_test <- function (){ > print ("i =") > print (i) > return () > > }Your code can be written more simply as: free_test <- function (){cat("i =\n", i, "\n")} free_driver <- function(){i=25;free_test()} free_driver() The scoping rules of R mean that the function free_test can see all the variables declared in free_driver. Your code _may_ be more robust if you pass the variable to the function where it is used though. At the moment, if you called free_test from somewhere other than free_driver (where i has not been declared) you will get an error.> Is there an easy way to name all elements of a list?Yes. Try: mylist <- list(a=1:5, b=letters[1:4]) mylist["a"] mylist$b attach(mylist) a b detach(mylist) See the help page for list for more examples. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
It may be easier for us to help you if you tell us what you are actually trying to accomplish (naming the elements of the list is easy, but doing that won't fix your problems). Note that when you attach a list it attaches a copy of the list, any changes to the original list will not be seen in the attached copy. Also, changing the value of 'i' after using it in the list will not affect the value in the list. You should really read up on lexical scoping, that may be the solution to your question, try rewriting your functions as: free_driver <- function(){ i <- 25 free_test <- function(){ cat("i=",i,"\n\n") } free_test() } To see if that is accomplishing what you want to do, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas L Jones, PhD > Sent: Thursday, November 22, 2007 1:16 AM > To: R-project help > Subject: [R] Naming elements of a list > > I have a numeric vector of lenth 1. I am trying to use it > inside a function just by giving its name, rather than > specifying it as an argument to the function. I am aware that > there is an attach function which you need to call. The > attach function will accept a list. However, I don't seem to > be able to create the list properly. (Or should I use a frame > instead?) > > free_driver <- function (){ > i <- numeric (1) > attach (as.list (i)) > i <- 25 > > free_test () > > } > free_test <- function (){ > print ("i =") > print (i) > return () > > } > > Anyway, here is the output, starting with the load operation: > > ------------------------------------------------------------------ > > > free_driver <- function (){ > + i <- numeric (1) > + attach (as.list (i)) > + i <- 25 > + > + free_test () > + > + } > > free_test <- function (){ > + print ("i =") > + print (i) > + return () > + > + } > > free_driver () > Error in attach(as.list(i)) : all elements of a list must be named > > > -------------------------------------------------------------- > > Is there an easy way to name all elements of a list? > > Your advice? > > Tom Jones > > ______________________________________________ > R-help at 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. >