[R] print only a few lines of an object

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Thu Mar 24 01:05:15 CET 2022


Variations on this topic come up regularly and of course variations on possible answers 
to often an unclear phrasing of the question.

Was the question how to change setting in R? Maybe but I consider that to be a stupider 
question than asking HOW to display specific objects the way you want.

As noted, variations on head() and tail() are commonly used. For objects like vectors 
and data.frames that can be indexed, you certainly can use techniques like 
name[1:7, 1:4] to show the first few rows and columns. Lots of ideas like that
make it not necessary to change the way things are printed.

But it is a common need and some have offered other packages that have dealt with
it with functions like glimpse() but one of the more common ones is the tidyverse
packages that use an augmented data.frame called a tibble which actually does
alter how it prints automagically to show just a few lines and only as many columns 
as fit your current output area and then add data mentioning how many rows and what 
columns are being ignored.

Arguably a better way to view your data is not to print it but to View it and if you do a 
View(data) or perhaps edit(data) or click on the variable in a pane in RSTUDIO,
you may be able to see it in a more spreadsheet format. Certainly you can also
save your data into formats like a CSV file and then view them externally, such as 
in EXCEL.

So, yes, you can create your own ways to automatically print objects but it is very 
rarely necessary. R has lots of built in ways and you can always write your own
functions that use a bunch of functions to format your data however you wish including
rounding it to some number of decimal places or all kinds of printf() functionality.

I also know that for many purposes, you may not want to look at your data but some kind 
of summary of your data and there are oodles of tools like psych::describe().

So is there anything you want besides just showing the first N lines and perhaps the
last N too? The more specific and clear the question, the more likely someone
will post a useful answer. I held back to see what others would say and I hope 
somewhere in all the responses you can pick what seems to work for you.

Final note is if you fiddle with a way everything prints, you may need to
work hard when you want something to print in another way or completely.
You often see someone writing code like print(as.data.frame(some_tibble))
to make it act on a tibble more fully.


-----Original Message-----
From: David Carlson via R-help <r-help using r-project.org>
To: John Fox <jfox using mcmaster.ca>
Cc: r-help mailing list <r-help using r-project.org>
Sent: Wed, Mar 23, 2022 5:26 pm
Subject: Re: [R] print only a few lines of an object


You can set the "max.print" option to something other than the default
value of 99999, e.g. options(max.print=50). The number is not lines,
or characters as far as I can tell. For example setting max.print=50
causes print(iris) to display the first 10 lines followed by a
warning. On a list object it seems to apply to each part of the list,
(e.g. split(iris, iris$Species) prints 10 lines of each species. This
is a crude tool that is mostly useful if you are working with large
objects and have a bad tendency to type the object name without
thinking.

David L Carlson


On Wed, Mar 23, 2022 at 3:32 PM John Fox <jfox using mcmaster.ca> wrote:
>
> Dear Jeff, On 2022-03-23 3:36 p.m., Jeff Newmiller wrote: > After-thought... > > Why not just use head() and tail() like normal R users do? head() and tail() are reasonable choices if there are many rows, but not if there are many ZjQcmQRYFpfptBannerStart
> This Message Is From an External Sender
> This message came from outside your organization.
> ZjQcmQRYFpfptBannerEnd
>
> Dear Jeff,
>
> On 2022-03-23 3:36 p.m., Jeff Newmiller wrote:
> > After-thought...
> >
> > Why not just use head() and tail() like normal R users do?
>
> head() and tail() are reasonable choices if there are many rows, but not
> if there are many columns.
>
> My first thought was your previous suggestion to redefine print()
> methods (although I agree with you that this isn't a good idea), but
> though I could get that to work for data frames, I couldn't for matrices.
>
> Adapting my preceding examples using car::brief():
>
>  > X <- matrix(rnorm(20000*200), nrow = 20000)
>
>  > library("car")
> Loading required package: carData
>
>  > print.data.frame <- function(x, ...){ # not recommended!
> +   brief(x, ...)
> +   invisible(x)
> + }
>
>  > as.data.frame(X)
> 20000 x 200 data.frame (19995 rows and 195 columns omitted)
>                V1         V2         V3 . . .        V199       V200
>               [n]        [n]        [n]               [n]        [n]
> 1     -1.1810658 -0.6090037  1.0057908        1.23860428  0.6265465
> 2     -1.6395909 -0.2828005 -0.6418150        1.12875894 -0.7594760
> 3      0.2751099  0.2268473  0.2267713        0.64305445  1.1951732
> . . .
> 19999  1.2744054  1.0170934 -1.0172511       -0.02997537  0.7645707
> 20000 -0.4798590 -1.8248293 -1.4664622       -0.06359483  0.7671203
>
>
>  > print.matrix <- function(x, ...){ # not recommended (and doesn't work)!
> +   brief(x, ...)
> +   invisible(x)
> + }
>
>  > X
>                    [,1]          [,2]          [,3]          [,4]
>    [,5]
>      [1,] -1.181066e+00 -6.090037e-01  1.005791e+00  3.738742e+00
> -6.986169e-01
>      [2,] -1.639591e+00 -2.828005e-01 -6.418150e-01 -7.424275e-01
> -1.415092e-01
>      [3,]  2.751099e-01  2.268473e-01  2.267713e-01 -6.308073e-01
> 7.042624e-01
>      [4,] -9.210181e-01 -4.617637e-01  1.523291e+00  4.003071e-01
> -2.792705e-01
>      [5,] -6.047414e-01  1.976075e-01  6.065795e-01 -8.074581e-01
> -4.089352e-01
>
> . . . [many lines elided]
>
>                  [,196]        [,197]        [,198]        [,199]
> [,200]
>      [1,] -1.453015e+00  1.347678e+00  1.189217e+00  1.238604e+00
> 0.6265465033
>      [2,] -1.693822e+00  2.689917e-01 -1.703176e-01  1.128759e+00
> -0.7594760299
>      [3,]  1.260585e-01  6.589839e-01 -7.928987e-01  6.430545e-01
> 1.1951731814
>      [4,] -1.890582e+00  7.614779e-01 -5.726204e-01  1.090881e+00
> 0.9570510645
>      [5,] -8.667687e-01  5.365750e-01 -2.079445e+00  1.209543e+00
> -0.2697400234
>   [ reached getOption("max.print") -- omitted 19995 rows ]
>
> So, something more complicated that I don't understand is going on with
> matrices.
>
> Best,
>   John
>
> >
> > On March 23, 2022 12:31:46 PM PDT, Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
> >> Sure. Re-define the print method for those objects. Can't say I recommend this, but it can be done.
> >>
> >> On March 23, 2022 11:44:01 AM PDT, Naresh Gurbuxani <naresh_gurbuxani using hotmail.com> wrote:
> >>> In an R session, when I type the name of an object, R prints the entire object (for example, a 20000 x 5 data.frame).  Is it possible to change the default behavior so that only the first five and last five rows are printed?
> >>>
> >>> Similarly, if the object is a 20000 x 200 matrix, the default behavior will be to print first five and last five columns, combined with first five and last five rows.
> >>>
> >>> Thanks,
> >>> Naresh
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fxu1u5Xo$
> >>> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fdt4UHeg$
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fxu1u5Xo$
> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fdt4UHeg$

> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help using 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.



More information about the R-help mailing list