Skip to content
Prev 391131 / 398506 Next

print only a few lines of an object

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 at r-project.org>
To: John Fox <jfox at mcmaster.ca>
Cc: r-help mailing list <r-help at 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 at mcmaster.ca> wrote:

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