53

The data.table has a nice feature that suppresses output to the head and tail of the table.

Is it possible to view / print more than 100 rows at once?

library(data.table)
## Convert the ubiquitous "iris" data to a data.table
dtIris = as.data.table(iris)
## Printing 100 rows is possible
dtIris[1:100, ]
## Printing 101 rows is truncated
dtIris[1:101, ]

I often have data.table results that are somewhat large (e.g. 200 rows) that I just want to view.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
geneorama
  • 3,620
  • 4
  • 30
  • 41
  • 3
    Since I can never find this anywhere else: the option to control column width (nchar / number of characters) is `options(datatable.prettyprint.char=5L)`. This is only documented in the (NEWS)[https://github.com/Rdatatable/data.table/blob/742ca4e2aad8b5672c447f91e0b6d9f21087b710/NEWS.md] – geneorama Jun 21 '16 at 19:38

5 Answers5

52

The print method of data.table has an argument nrows:

args(data.table:::print.data.table)
function (x, nrows = 100L, digits = NULL, ...) 

You can use this to control how many rows get printed:

print(dtIris, nrow=105)
.....
99:          5.1         2.5          3.0         1.1 versicolor
100:          5.7         2.8          4.1         1.3 versicolor
101:          6.3         3.3          6.0         2.5  virginica
102:          5.8         2.7          5.1         1.9  virginica
103:          7.1         3.0          5.9         2.1  virginica
104:          6.3         2.9          5.6         1.8  virginica
105:          6.5         3.0          5.8         2.2  virginica
     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 7
    +1 Which is also FAQ 2.11. Note also that, oddly, typing `print(DT)` at the prompt (with or without `nrows`) is faster than typing just `DT`. It seems to be down to R copying the whole object in the second (more common) case (during dispatch?) before the data.table method comes along to print the head and tail. If anyone knows why R does that I'd love to know. See comments in [FR#1001 REPL print copy](https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1001&group_id=240&atid=978) about `applyClosure`. – Matt Dowle Aug 28 '12 at 15:50
  • 6
    `nrows` does not work in my case, it displays the truncated table only. However, `topn` works. It's weird. I used e.g. `data.table:::print.data.table(dtIris,nrows = 100)` – user3032689 Sep 02 '16 at 09:28
  • 3
    In my case `n` works, but neither `nrows` nor `topn` works (R 3.3.2) – sautedman Feb 07 '17 at 18:03
  • 1
    `print(DT, topn=150)` works for me to display the first 150 rows. R version 3.4.2 (2017-09-28) data.table_1.10.4-3 Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 16.04.3 LTS – Reilstein Dec 08 '17 at 00:56
19

View() (as in View(iris) or View(dtIris[1:120,])) doesn't truncate data.tables, and can often be nicer than printing/spewing out a data.* to the console.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Very nice! Even works in the RStudio server environment, and lets me copy a little report into Excel for posterity. – geneorama Aug 28 '12 at 18:08
  • @geneorama -- Thanks for adding that note. I usually work from vanilla Windows R gui or a Windows emacs installation and wondered how broadly `View()` is implemented. Am especially curious what it produces on a *NIX machine. – Josh O'Brien Aug 28 '12 at 18:24
  • It works in linux, too, here... but I don't see how to copy a report. – nsheff Sep 19 '14 at 12:02
15

To print the top 60 and bottom 60 lines (default is top 5 and bottom 5):

print(dtIris, topn = 60)
user3226167
  • 3,131
  • 2
  • 30
  • 34
  • 2
    Excellent point. You can also set this in options, with `options(datatable.print.topn=60)`. Also, I've learned of a feature that allows you to expand or limit the width of the printed column `options(datatable.prettyprint.char=80L)`. This prettyprint option is not set by default, so you have to know the command in order to use it (whereas you can search options() for other options like `datatable.print.topn` and `datatable.print.nrows` – geneorama Jan 26 '16 at 17:00
0

You could convert it to a data.frame only for printing:

iris_dt = as.data.table(iris)
print(as.data.frame(iris_dt))
andschar
  • 3,504
  • 2
  • 27
  • 35
  • Good workaround, but it doesn't answer the question. If you start looking at workarounds then you could suggest a lot of things, like "just use SAS!" or "print each row one at a time!"... you get the idea. – geneorama Oct 31 '17 at 19:27
-1

A messy option, but you could always export it into excel to view it with excels convenience.

library(xlsReadWrite)
write.xls(mydata, "c:/mydata.xls")
Timothy Alston
  • 1,501
  • 5
  • 18
  • 29
  • 4
    data.tables are used for huge amounts of data. Have fun with Excel. – Roland Aug 28 '12 at 15:45
  • hence I put "a messy option". Depends on the size of your data. – Timothy Alston Aug 28 '12 at 15:51
  • 1
    There is messy and then there is O_O – Roland Aug 28 '12 at 15:56
  • 9
    If you are already working with R and your solution to a specific problem is to export the data to Excel, please ask on Stack Overflow for a better way. – Roland Aug 28 '12 at 16:00
  • I have to say, this is probably the most effective answer. However, it's not a "right" answer in terms of data.table and R. The only reason that I'm not exporting to Excel in this case is that I'm working on a server. It's almost exactly what I do for these summaries that are in the 200-500 row range. – geneorama Aug 28 '12 at 16:41
  • @Roland To be fair, I was asking about printing small data.tables to the console, which is also a size that Excel can handle. – geneorama Aug 28 '12 at 16:46
  • 2
    Timothy, you may find this useful: http://stackoverflow.com/questions/12164897/quickly-view-an-r-data-frame-vector-or-data-table-in-excel/12164899#12164899 – geneorama Aug 28 '12 at 18:03
  • @geneorama Why do you use `data.table` if your data size is so small? `data.table` is optimized for large data sets. For small data sets you should just use base R, i.e., a `data.frame`. – Roland Aug 29 '12 at 07:00
  • @Roland The results from a data table query are also a data table, and query results are not that large. However sometimes they're just large enough that the full result will not print to my screen. – geneorama Aug 30 '12 at 20:35
  • Over the years I've found that at some point in nearly every project I end up exporting a lot of data to Excel to debug, and I find things that I would have otherwise missed. – geneorama Jan 26 '16 at 17:07