Skip to content

Instantly share code, notes, and snippets.

@grantmcdermott
Created February 20, 2026 20:49
Show Gist options
  • Select an option

  • Save grantmcdermott/08902a3645fb8585b7d38d9596d4dde7 to your computer and use it in GitHub Desktop.

Select an option

Save grantmcdermott/08902a3645fb8585b7d38d9596d4dde7 to your computer and use it in GitHub Desktop.
Nice(r) base R data.frame print method
print.data.frame = function(x, ..., topn = 5, nrows = 20) {
nr = nrow(x)
if (nr <= nrows) {
out = x
splitprint = FALSE
} else {
out = rbind(head(x, topn), tail(x, topn))
splitprint = TRUE
}
# Type abbreviations
type_map = c(
numeric = "<num>", integer = "<int>", character = "<chr>",
factor = "<fct>", logical = "<lgl>", Date = "<Date>",
POSIXct = "<dttm>", complex = "<cpl>", ordered = "<ord>"
)
classes = sapply(x, \(col) class(col)[1])
abbs = unname(type_map[classes])
abbs[is.na(abbs)] = paste0("<", classes[is.na(abbs)], ">")
names(abbs) = colnames(x)
# Format each column right-justified, build character matrix
cols = lapply(out, \(col) format(col, ..., justify = "right"))
toprint = do.call(cbind, cols)
dimnames(toprint) = list(rownames(out), colnames(x))
# Set rownames for default integer indices
if (identical(rownames(x), as.character(seq_len(nr)))) {
rn = if (splitprint) {
c(seq_len(topn), seq.int(to = nr, length.out = topn))
} else {
seq_len(nr)
}
rownames(toprint) = format(rn, right = TRUE)
}
# Prepend class row
toprint = rbind(abbs, toprint)
rownames(toprint)[1] = " "
# Insert separator
if (splitprint) {
sep_row = matrix("", 1, ncol(toprint))
toprint = rbind(
head(toprint, topn + 1),
sep_row,
tail(toprint, topn)
)
rownames(toprint)[topn + 2] = "---"
rownames(toprint) = format(rownames(toprint), justify = "right")
}
print(toprint, right = TRUE, quote = FALSE)
invisible(x)
}
@grantmcdermott
Copy link
Author

Use the above function interactively, or add it to your .Rprofile to make it your default data.frame method.

Examples:

R 4.5.2> [I] iris
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
           <num>       <num>        <num>       <num>     <fct>
  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
---
146          6.7         3.0          5.2         2.3 virginica
147          6.3         2.5          5.0         1.9 virginica
148          6.5         3.0          5.2         2.0 virginica
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica
R 4.5.2> [I] mtcars
                    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
                  <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
        Mazda RX4  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
    Mazda RX4 Wag  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
       Datsun 710  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
   Hornet 4 Drive  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
Hornet Sportabout  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
              ---
     Lotus Europa  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
   Ford Pantera L  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
     Ferrari Dino  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
    Maserati Bora  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8
       Volvo 142E  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
R 4.5.2> [I] head(mtcars)
                    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
                  <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
Mazda RX4          21.0     6   160   110  3.90 2.620 16.46     0     1     4     4
Mazda RX4 Wag      21.0     6   160   110  3.90 2.875 17.02     0     1     4     4
Datsun 710         22.8     4   108    93  3.85 2.320 18.61     1     1     4     1
Hornet 4 Drive     21.4     6   258   110  3.08 3.215 19.44     1     0     3     1
Hornet Sportabout  18.7     8   360   175  3.15 3.440 17.02     0     0     3     2
Valiant            18.1     6   225   105  2.76 3.460 20.22     1     0     3     1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment