Reputation: 3274
Is there a way to print small data.frames
to the console in a more readable manner?
For example, would it be possible to output to the console:
library(MASS)
iris[1:5, ]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
as
iris[1:5, ]
+--------------+-------------+--------------+-------------+---------+
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
+--------------+-------------+--------------+-------------+---------+
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
+--------------+-------------+--------------+-------------+---------+
I realise for large data.frames
it would take up an unnecessary amount of time, but if it's an option, I would like to be able to look at small frames in a more structured manner.
In particular, when I have two text fields next to each other, it would be much easier with a pipe between the two fields to separate them, as the spacing between words is the same size as the spacing between columns.
Thanks
Upvotes: 30
Views: 30439
Reputation: 821
How about a third party cli app for viewing datasets? Visidata outside the console? It's scrollable and does pretty well with larger datasets as well.
You might work up a helper function to write a dataframe to a temp file. Something possibly like:
df2temp <- func(.df) {
write.csv(.df, "temp.csv", row.names=FALSE)
}
You could then do something like:
df2temp(some_dataframe)
and use Visidata to view temp.csv
in another terminal window/tab/pane (check out tmux if you don't use it):
vd temp.csv
Upvotes: 0
Reputation: 56004
tibbles are printed with colour formatting in the console:
library(tidyverse)
x <- as_tibble(mtcars)
x
Upvotes: 2
Reputation: 1411
If you are also open to printing your output into the (RStudio) viewer pane rather than the console, I would recommend using the DT
package.
library(DT)
datatable(iris)
This has several advantages, I think: the output is pretty and well-arranged, the package is able to display large data frames without becoming cumbersome and it is highly customizable to boot.
Upvotes: 2
Reputation: 1938
I had the same problem recently and came across the huxtable
package. It is very flexible and maybe a litte overkill for just nicer console output, but it served me very well.
Here is how you could solve your problem using huxtable
:
library(huxtable)
library(magrittr)
small_iris <- iris[1:5, ]
iris_hux <-
hux(small_iris) %>%
add_colnames() %>%
set_bold(row = 1, col = everywhere, value = TRUE) %>%
set_all_borders(TRUE)
I think all functions speak for themselves. For a thorough introduction, see https://hughjonesd.github.io/huxtable/huxtable.html#adding-row-and-column-names.
print_screen(iris_hux)
yield this output (in the console!):
I have not figured out yet how to suppress the bottom information on the column names. So if someone knows, please comment!
EDIT: In order to suppress the column names at the bottom, use colnames = FALSE
inside print_screen()
.
Upvotes: 13
Reputation: 3274
In case it helps anyone, I just stumbled across the fact that knitr
's kable
achieves a nice pretty print. Combine with some of the .Rprofile
suggestions above, this seems to achieve what I had in mind.
> knitr::kable(head(iris))
| Sepal.Length| Sepal.Width| Petal.Length| Petal.Width|Species |
|------------:|-----------:|------------:|-----------:|:-------|
| 5.1| 3.5| 1.4| 0.2|setosa |
| 4.9| 3.0| 1.4| 0.2|setosa |
| 4.7| 3.2| 1.3| 0.2|setosa |
| 4.6| 3.1| 1.5| 0.2|setosa |
| 5.0| 3.6| 1.4| 0.2|setosa |
| 5.4| 3.9| 1.7| 0.4|setosa |
Upvotes: 35
Reputation: 60452
There are a couple of methods you could try.
Add a couple of helper functions to your .Rprofile
. In my profile, I have
hh = function(d)
if(class(d)=="matrix"|class(d)=="data.frame") d[1:5,1:5]
This function prints the top left hand corner of the data frame. I also have
ht = function(d, n=6) rbind(head(d, n), tail(d,n))
Create your own S3
print function for data frames, e.g.
print.data.frame = function(x, ..., digits = NULL,
quote = FALSE, right = TRUE,
row.names = TRUE)
message("hi")
Use a package, e.g. dplyr
. However, this is a bit overkill if all you want is pretty printing.
Upvotes: 3