I have a question about what is considered good style in R.
I have a function which returns several arguments, say [x,y,z]=f(),
where sometimes I only care about the first few
(so in Matlab, I can write e.g., [x,y] = f()).
In R, there seem to be 2 ways to write this, both unsatisfying (to me):
LIST
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
x <- res$x; y <- res$y; z <- res$z
STRUCTURE
f <- function() {
...
structure(x,y=y,z=z)
}
x <- f()
y <- attr(x,"y"); z <- attr(x, "z")
Which is considered better style, or does it just depend on the
problem?
Kevin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
style question: returning multiple arguments - structure or list
10 messages · Kevin Murphy, Ross Ihaka, Brian Ripley +4 more
Kevin Murphy wrote:
I have a question about what is considered good style in R.
I have a function which returns several arguments, say [x,y,z]=f(),
where sometimes I only care about the first few
(so in Matlab, I can write e.g., [x,y] = f()).
In R, there seem to be 2 ways to write this, both unsatisfying (to me):
LIST
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
x <- res$x; y <- res$y; z <- res$z
STRUCTURE
f <- function() {
...
structure(x,y=y,z=z)
}
x <- f()
y <- attr(x,"y"); z <- attr(x, "z")
Which is considered better style, or does it just depend on the
problem?
The second of these looks a little odd to me. In the first x, y, and z are treated symmetrically while the second is asymmetric. You might also consider whether you want to unpackage the x, y and z values from the list at all. If you are just using the returned values you may as well refer to them as res$x, res$y and res$z. Ross -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 27 Jul 2001, Kevin Murphy wrote:
I have a question about what is considered good style in R.
I have a function which returns several arguments, say [x,y,z]=f(),
where sometimes I only care about the first few
(so in Matlab, I can write e.g., [x,y] = f()).
In R, there seem to be 2 ways to write this, both unsatisfying (to me):
LIST
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
x <- res$x; y <- res$y; z <- res$z
STRUCTURE
f <- function() {
...
structure(x,y=y,z=z)
}
x <- f()
y <- attr(x,"y"); z <- attr(x, "z")
Which is considered better style, or does it just depend on the
problem?
The latter. There's a third option, to create a new class and return an object of that class, and write appropriate extractor functions. If you look in the code base you will see many more uses of lists than attributes, and heavy use of classes for returned lists.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear All, I have been trying to build R using Microsoft Visual C++ (Ver 6) for the last three and a half months. This has proved to be UNBELIEVABLY frustrating. Unfortunately, I wasn't helped much by comments along the lines of "RTFM" when I first asked for help from this group. I decided to do it by myself, as the impression I got was that everybody was to busy to help. However, after having wasted the last three and a half months, I'm forced to come back, cup in hand. I would be very grateful to hear from anyone who has managed to build R under VC++ 6.0, and even more grateful, if they could e-mail me a VC project tthat builds R. It would not be neccessary to include R the source files, since I can download these myself. What would be useful though, would be the CORRECT tools (I spent almost a month following links in the very useful Readme files) etc. I happened to stumble on R shortly after my Masters degree in Applied Statistics. I think it's an AMAZING product, and the fact that it is open source is even more amazing. I take off my hat to all those that helped to make it a reality. My only gripe is that of "user-friendliness" when it comes to building R. The impression I get is that this is a closely guarded secret by old grey haired be-spectapled men in white coats with 50 or more years of hacking UNIX, who don't have time to explain anything to any one, and our stuck up in their own little world of academia - Sorry ;-). This is just the impression I get, and many students I've spoken to have the same impression and so don't post to this group if they have any queries. I expect anytime, to have some Professor e-mailing to "tell me off" for being a naughty boy ! (and to kindly inform me that it's all in the FM!). Anyway, I've had my rant (it's been threee and a half month's coming). If you're too busy to bother about helping this "little boy" then DO NOT feel obliged to REPLY to this mail. However, if there are any kind souls out there who would like to give a helping hand to a young student who is willing to learn, I will be most grateful to hear from you. As I mentioned earlier, if you could e-mail a MSVC project that can just get me up and running with building R, I will be forever grateful for your time, patience andd kindness. Sincerely, N. Osborne nc_osborne at hotmail.com -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Neil Osborne" <nc_osborne at hotmail.com> writes:
I decided to do it by myself, as the impression I got was that everybody was to busy to help. However, after having wasted the last three and a half months, I'm forced to come back, cup in hand. I would be very grateful to hear from anyone who has managed to build R under VC++ 6.0, and even more grateful, if they could e-mail me a VC project tthat builds R. It would not be neccessary to include R the source files, since I can download these myself. What would be useful though, would be the CORRECT tools (I spent almost a month following links in the very useful Readme files) etc. I happened to stumble on R shortly after my Masters degree in Applied Statistics. I think it's an AMAZING product, and the fact that it is open source is even more amazing. I take off my hat to all those that helped to make it a reality. My only gripe is that of "user-friendliness" when it comes to building R. The impression I get is that this is a closely guarded secret by old grey haired be-spectapled men in white coats with 50 or more years of hacking UNIX, who don't have time to explain anything to any one, and our stuck up in their own little world of academia - Sorry ;-).
Well, sixteen years to be precise in my case. However, you need to know that the grey-haired men (some more than others, none of us wear white coats though, and some of us can still manage without reading glasses) *also* gave up on Visual C++ many moons ago. The project file structure of VC++ is simply not strong enough to build the auxiliary files needed for R, and we only achieved the cross-platform portability by using the gnuwin tools. This is simply the traditional clash between two kinds of user-friendliness: making simple things easy vs. making difficult things possible (and automatable). (You can read the book or wait for the movie, but in academia the latter option may require a very long wait!)
This is just the impression I get, and many students I've spoken to have the same impression and so don't post to this group if they have any queries. I expect anytime, to have some Professor e-mailing to "tell me off" for being a naughty boy ! (and to kindly inform me that it's all in the FM!).
Well, it is... *provided* you use the suggested tools. Once the tools are installed, you start a shell, go to the relevant directory, type "make" and sit down and watch the magic. This might have taken you three and a half days to get to work and three and a half weeks to begin to understand how it works... If you want to use a different set of tools, in particular a proprietary one, you really can't expect anyone else to jump up and help you out. It might be doable - versions of R for Windows around the 0.50 revision (c.1997) were actually made with VC++ (by Robert), but it usually took him over a month from the Unix releases were made. However, the functionality of R has been expanded considerably since then and I have no clue as to where VC++ stands when it comes to dynamic linking and suchlike. I also think some non-binary items were actually built on Unix and copied across (but my memory is fading).
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, you also have the option of "throwing out" the third result by setting it to NULL (taking your LIST option)?
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
names(jj)
[1] "x" "y" "z"
res$z <- NULL names(res)
[1] "x" "y" and then work with res (res$x and res$y)? Regards, -tom -- Email: vogels at cmu.edu Phone: 412.855.2096
On Fri, 27 Jul 2001, Kevin Murphy wrote:
I have a question about what is considered good style in R.
I have a function which returns several arguments, say [x,y,z]=f(),
where sometimes I only care about the first few
(so in Matlab, I can write e.g., [x,y] = f()).
In R, there seem to be 2 ways to write this, both unsatisfying (to me):
LIST
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
x <- res$x; y <- res$y; z <- res$z
STRUCTURE
f <- function() {
...
structure(x,y=y,z=z)
}
x <- f()
y <- attr(x,"y"); z <- attr(x, "z")
Which is considered better style, or does it just depend on the
problem?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Thomas J Vogels" <tov at ece.cmu.edu> writes:
Hi, you also have the option of "throwing out" the third result by setting it to NULL (taking your LIST option)?
f <- function() {
...
list(x=x,y=y,z=z)
}
res <- f()
names(jj)
[1] "x" "y" "z"
res$z <- NULL names(res)
[1] "x" "y" and then work with res (res$x and res$y)?
A little summer exercise: Can one write a list assignment function, i.e. "list<-" so that list(a,b,c) <- f() would be equivalent to r <- f() a <- r[[1]] b <- r[[2]] c <- r[[3]] Even better, do something useful with named list elements. (And what are the odds of finding that this is really an exercise hidden somewhere in a book by Venables and Ripley?)
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 28 Jul 2001, Peter Dalgaard BSA wrote:
A little summer exercise: Can one write a list assignment function, i.e. "list<-" so that list(a,b,c) <- f() would be equivalent to r <- f() a <- r[[1]] b <- r[[2]] c <- r[[3]] Even better, do something useful with named list elements. (And what are the odds of finding that this is really an exercise hidden somewhere in a book by Venables and Ripley?)
I think you'll find that the first argument of an assignment function has to exist already -- the function is passed the evaluated first argument. This means it isn't quite true that a<-"foo<-"(a,value) is the same as foo(a)<-value eg
"foo<-"<-function(x,value){list(substitute(x),value)}
foo(a)<-4
Error: Object "a" not found
"foo<-"(a,1)
[[1]] a [[2]] [1] 1
a<-1 foo(a)<-4 a
[[1]] *tmp* [[2]] [1] 4 If this is correct, then you can't really write "list<-" -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas Lumley <tlumley at u.washington.edu> writes:
I think you'll find that the first argument of an assignment function has to exist already -- the function is passed the evaluated first argument. This means it isn't quite true that a<-"foo<-"(a,value) is the same as foo(a)<-value eg
"foo<-"<-function(x,value){list(substitute(x),value)}
foo(a)<-4
Error: Object "a" not found
"foo<-"(a,1)
[[1]] a [[2]] [1] 1
a<-1 foo(a)<-4 a
[[1]] *tmp* [[2]] [1] 4 If this is correct, then you can't really write "list<-"
Dang! You're right...
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Re: teaching computer science - I'm surprised nobody has recommended using the classic "Structure and Interpretation of Computer Programs", by Abelson, Sussman, and Sussman (1984/1996) (http://mitpress.mit.edu/sicp/sicp.html), especially since R is so closely modelled on Scheme, the language used in this book. (SICP/Scheme is used in the introductory CS class in Berkeley and many other institutions.) Re: list/structures - I discovered a new reason why using lists is better than structures. Suppose you create res=structure(x,y=y,z=z). How do you just print out x, without the corresponding attributes (which might be large)? If res=list(x=x,y=y,z=z), it's easy. There is a more subtle advantage of the Matlab-style [x,y,z]=f() syntax - by checking how many return arguments are on the LHS, you can decide whether to even bother computing (yet alone returning) the "optional" results y,z. Obviously there are other ways of doing this, but I think this is quite slick (and I have used not infrequently). Re: coming to R from Matlab - I am not trying to write Matlab-like R code (although sometimes I can't help it :), but rather to learn the stylistic conventions of the R community (which, I am pleased to see, seems very concerned with writing elegant code). Kevin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._