Hello,
I believe but do not have references that str was meant for interactive
use, not for use in a script or package. If this is the case, then it
should be rare to have to output to an object such as a character vector.
As for my solution, it is far from perfect, I try to avoid
capture.output and, once again, limit its use to interactive R. It is
overkill but I use it so few times that performance issues probably do
not matter. It is sometimes a convenient way of solving an immediate
problem and once done, move on.
As for the OP, Enrico's solution seems better, even with the 2nd printed
line. Unless the 1st line is to be processed (?).
Hope this helps,
Rui Barradas
Às 17:32 de 02/09/21, Avi Gross via R-help escreveu:
Thanks for the interesting method Rui. So that is a way to do a redirect of
output not to a sinkfile but to an in-memory variable as a textConnection.
Of course, one has to wonder why the makers of str thought it would be too
inefficient to have an option that returns the output in a form that can be
captured directly, not just to the screen.
I have in the past done odd things such as using sink() to capture the output
of a program that wrote another program dynamically in a loop. The saved file
could then be used with source(). So a similar technique can capture the output
from str() or cat() or whatever normally only writes to the screen and then the
file can be read in to get the first line or whatever you need. I have had to
play games to get the right output from some statistical programs too as it was
assumed the user would read it, and sometimes had to cherry pick what I needed
directly from withing the underlying object.
I suspect one reason R has so many packages including the tidyverse I like to use, is because the
original R was designed in another time and in many places is not very consistent. I wonder how
hard it would be to change some programs to simply accept an additional argument like sink() has
where you can say split=TRUE and get a copy of what is being diverted to also come to the screen. I
find cat() to be a very useful way to put more complicated output together than say print() but
since it does not allow capture of the text into variables, I end up having to use other methods
such as the glue() function or something like print(sprint("Hello %s, I have %d left.\n",
"Brian", 5))
But you work with what you have. Your solution works albeit having read the
function definition, is quite a bit of overkill when I read the code as it does
things not needed. But as noted, if efficiency matters and you are only looking
at data.frame style objects, there are cheaper solutions.
-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Rui Barradas
Sent: Thursday, September 2, 2021 7:31 AM
To: Luigi Marongiu <marongiu.lu...@gmail.com>; r-help <r-help@r-project.org>
Subject: Re: [R] Show only header of str() function
Hello,
Not perfect but works for data.frames:
header_str <- function(x){
capture.output(str(x))[[1]]
}
header_str(iris)
header_str(AirPassengers)
header_str(1:10)
Hope this helps,
Rui Barradas
Às 12:02 de 02/09/21, Luigi Marongiu escreveu:
Hello, is it possible to show only the header (that is: `'data.frame':
x obs. of y variables:` part) of the str function?
Thank you
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.