Using str() in a function. I am in the early phase of learning R, and I find I spend a lot of time trying to figure out what is actually in objects I have created or read in from a file. I'm trying to make a simple little function to display a couple of things about a object, let's say the summary() and the str(), sequentially, preferably without a bunch of surplus lines between them. I have tried a large number of things; none do what I want.> GG<- c(1,2,3)# This one ignores the str().> testX <- function(X) {return(summary(X)); str(X)} > testX(GG)Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 # So does this one.> testX2 <- function(X) {return(summary(X)); return(str(X))} > testX2(GG)Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 # On the other hand, this one ignores the summary()> testX3 <- function(X) {summary(X); return(str(X))} > testX3(GG)num [1:3] 1 2 3 # This one displays both, in reverse order, with a superfluous (to my intentions) [[NULL]].> testX4 <- function(X) {list(summary(X), (str(X)))} > testX4(GG)num [1:3] 1 2 3 [[1]] Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 [[2]] NULL # Now we are back to ignoring the str().> testX5 <- function(X) {list(return(summary(X)), (str(X)))} > testX5(GG)Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 # This does the same as testX4().> testX6 <- function(X) {return(list(summary(X), (str(X))))} > testX6(GG)num [1:3] 1 2 3 [[1]] Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 [[2]] NULL I tried a bunch more, using the print command, etc., but nothng I tried resulted in the output of summary() followed by the output of str(). And is there really no way to assign the output of str() -- that is to say, the output str() normally prints to the console -- to an object? I would be very greatful for any guidance you could offer. Sincerely, Andrew -- View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3655785.html Sent from the R help mailing list archive at Nabble.com.
On Jul 9, 2011, at 4:20 AM, andrewH wrote:> Using str() in a function. > > I am in the early phase of learning R, and I find I spend a lot of > time > trying to figure out what is actually in objects I have created or > read in > from a file. I'm trying to make a simple little function to display a > couple of things about a object, let's say the summary() and the > str(), > sequentially, preferably without a bunch of surplus lines between > them. I > have tried a large number of things; none do what I want. > >> GG<- c(1,2,3) > # This one ignores the str(). >> testX <- function(X) {return(summary(X)); str(X)} >> testX(GG) > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0snipped long list of unsuccessful attempts to return str results> # This one displays both, in reverse order, with a superfluous (to my > intentions) [[NULL]]. >> testX4 <- function(X) {list(summary(X), (str(X)))} >> testX4(GG) > num [1:3] 1 2 3 > [[1]] > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > [[2]] > NULL > > I tried a bunch more, using the print command, etc., but nothng I > tried > resulted in the output of summary() followed by the output of str(). > And is > there really no way to assign the output of str() -- that is to say, > the > output str() normally prints to the console -- to an object??str It says: "Value str does not return anything, for efficiency reasons. The obvious side effect is output to the terminal." (Always a good idea to read the `Value` section of a help page.) What you see at the terminal when you make an assignment is not what happens inside a function. The effects inside a function environment are only visible if they change the returned object (or cause an error). Most (if not all) of the output at the terminal is the result of cat() which also returns NULL invisibly. So str, like most base plotting functions, is useful for its side-effects rather than for its returned value. If you want to get rid of what you are calling "the superfluous NULL", then use c() rather than list: > testX7 <- function(X) {return(c(summary(X), str(X)))} > testX7(GG) int [1:3] 1 2 3 Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0> " > I would be very greatful for any guidance you could offer. > > Sincerely, Andrew > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3655785.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi: Is this what you're after? testX <- function(X) { print(summary(X)) print(str(X)) invisible() # returns nothing } testX(1:10) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 3.25 5.50 5.50 7.75 10.00 int [1:10] 1 2 3 4 5 6 7 8 9 10 NULL See inline.. On Sat, Jul 9, 2011 at 1:20 AM, andrewH <ahoerner at rprogress.org> wrote:> Using str() in a function. > > I am in the early phase of learning R, and I find I spend a lot of time > trying to figure out what is actually in objects I have created or read in > from a file. ?I'm trying to make a simple little function to display a > couple of things about a object, let's say the summary() and the str(), > sequentially, preferably without a bunch of surplus lines between them. ?I > have tried a large number of things; none do what I want. > >> GG<- c(1,2,3) > # This one ignores the str(). >> testX <- function(X) {return(summary(X)); str(X)} >> testX(GG) > ? Min. 1st Qu. ?Median ? ?Mean 3rd Qu. ? ?Max. > ? ?1.0 ? ? 1.5 ? ? 2.0 ? ? 2.0 ? ? 2.5 ? ? 3.0Yes, because you asked it to return summary(X).> > # So does this one. >> testX2 <- function(X) {return(summary(X)); return(str(X))} >> testX2(GG) > ? Min. 1st Qu. ?Median ? ?Mean 3rd Qu. ? ?Max. > ? ?1.0 ? ? 1.5 ? ? 2.0 ? ? 2.0 ? ? 2.5 ? ? 3.0Only the first return() is implemented. (Hint: return() is not always a good way to exit a function.)> > # On the other hand, this one ignores the summary() >> testX3 <- function(X) {summary(X); return(str(X))} >> testX3(GG) > ?num [1:3] 1 2 3You asked it to return str(X) - it did.> > # This one displays both, in reverse order, with a superfluous (to my > intentions) [[NULL]]. >> testX4 <- function(X) {list(summary(X), (str(X)))} >> testX4(GG) > ?num [1:3] 1 2 3 > [[1]] > ? Min. 1st Qu. ?Median ? ?Mean 3rd Qu. ? ?Max. > ? ?1.0 ? ? 1.5 ? ? 2.0 ? ? 2.0 ? ? 2.5 ? ? 3.0 > > [[2]] > NULLstr() doesn't save its output - it only prints. Here's an example:> u <- str(1:10)int [1:10] 1 2 3 4 5 6 7 8 9 10> uNULL This is why it prints in front of the list but doesn't get saved into the list.> > # Now we are back to ignoring the str(). >> testX5 <- function(X) {list(return(summary(X)), (str(X)))} >> testX5(GG) > ? Min. 1st Qu. ?Median ? ?Mean 3rd Qu. ? ?Max. > ? ?1.0 ? ? 1.5 ? ? 2.0 ? ? 2.0 ? ? 2.5 ? ? 3.0return(summary(X)) is superfluous; the list() construct is an implicit return() object.> > # This does the same as testX4(). >> testX6 <- function(X) {return(list(summary(X), (str(X))))} >> testX6(GG) > ?num [1:3] 1 2 3 > [[1]] > ? Min. 1st Qu. ?Median ? ?Mean 3rd Qu. ? ?Max. > ? ?1.0 ? ? 1.5 ? ? 2.0 ? ? 2.0 ? ? 2.5 ? ? 3.0 > > [[2]] > NULLThis is the same as testX4.> > I tried a bunch more, using the print command, etc., but nothng I tried > resulted in the output of summary() followed by the output of str(). And is > there really no way to assign the output of str() -- that is to say, the > output str() normally prints to the console -- to an object? > > I would be very greatful for any guidance you could offer.You could always read An Introduction to R - specifically, Chapter 10, writing your own functions. If you look closely, you'll notice that return() is never used in any of the examples in that chapter. It's not 'wrong' to use return(), but it's locked you into a certain mindset you need to get out of. return() is one way of passing output to the object to which the function call is assigned, but your apparent goal was 'display', for which print() is perfectly adequate. HTH, Dennis> > Sincerely, Andrew > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3655785.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
As long as you just want to display it, use print() GG<- c(1,2,3) print(summary(GG),str(GG)) Output: num [1:3] 1 2 3 Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.5 2.0 2.0 2.5 3.0 HTH, Daniel andrewH wrote:> > Using str() in a function. > > I am in the early phase of learning R, and I find I spend a lot of time > trying to figure out what is actually in objects I have created or read in > from a file. I'm trying to make a simple little function to display a > couple of things about a object, let's say the summary() and the str(), > sequentially, preferably without a bunch of surplus lines between them. I > have tried a large number of things; none do what I want. > >> GG<- c(1,2,3) > # This one ignores the str(). >> testX <- function(X) {return(summary(X)); str(X)} >> testX(GG) > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > # So does this one. >> testX2 <- function(X) {return(summary(X)); return(str(X))} >> testX2(GG) > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > # On the other hand, this one ignores the summary() >> testX3 <- function(X) {summary(X); return(str(X))} >> testX3(GG) > num [1:3] 1 2 3 > > # This one displays both, in reverse order, with a superfluous (to my > intentions) [[NULL]]. >> testX4 <- function(X) {list(summary(X), (str(X)))} >> testX4(GG) > num [1:3] 1 2 3 > [[1]] > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > [[2]] > NULL > > # Now we are back to ignoring the str(). >> testX5 <- function(X) {list(return(summary(X)), (str(X)))} >> testX5(GG) > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > # This does the same as testX4(). >> testX6 <- function(X) {return(list(summary(X), (str(X))))} >> testX6(GG) > num [1:3] 1 2 3 > [[1]] > Min. 1st Qu. Median Mean 3rd Qu. Max. > 1.0 1.5 2.0 2.0 2.5 3.0 > > [[2]] > NULL > > I tried a bunch more, using the print command, etc., but nothng I tried > resulted in the output of summary() followed by the output of str(). And > is there really no way to assign the output of str() -- that is to say, > the output str() normally prints to the console -- to an object? > > I would be very greatful for any guidance you could offer. > > Sincerely, Andrew >-- View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3666684.html Sent from the R help mailing list archive at Nabble.com.