使用 kable 打印大 data.frame 的有效方法

Efficient way to print large data.frame with kable

我想用 kable 打印一个大 data.frame。我将 data.frame 分成几部分。有没有更高效代码更少的方法?

mt1 <-
  matrix(
    data = runif(n = 200, min = 101, max = 999)
  , nrow = 10
  , ncol = 20
  , byrow = TRUE
  , dimnames = list(LETTERS[1:10], paste0("V", 1:20))
  )

df1 <- data.frame(Name = row.names(mt1), mt1) 

library(kableExtra)

kable(df1[,c(1, 2:6)], format = "rst", row.names = FALSE)
kable(df1[,c(1, 7:11)], format = "rst", row.names = FALSE)
kable(df1[,c(1, 12:16)], format = "rst", row.names = FALSE)
kable(df1[,c(1, 17:21)], format = "rst", row.names = FALSE)

已编辑

library(tidyverse)
kable(df1[,c(1, 2:6)], format = "latex", row.names = FALSE) %>% 
  row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")

kable(df1[,c(1, 7:11)], format = "latex", row.names = FALSE) %>% 
  row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")

kable(df1[,c(1, 12:16)], format = "latex", row.names = FALSE) %>% 
  row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")

kable(df1[,c(1, 17:21)], format = "latex", row.names = FALSE) %>% 
  row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")

我们可以创建索引并将其传递给Map

Map(function(i, j) kable(df1[c(1, i:j)], format = 'rst', 
        row.names = FALSE), seq(2, 17, by = 5), seq(6, 21, by = 5))

或将 split 与使用 gl

创建的分组索引一起使用
lapply(split.default(df1[, -1], as.integer(gl(ncol(df1)-1,  5,
  ncol(df1) - 1))), function(x) 
         kable(cbind(df1[1], x), format = 'rst', row.names = FALSE))
#$`1`


#====  ========  ========  ========  ========  ========
#Name        V1        V2        V3        V4        V5
#====  ========  ========  ========  ========  ========
#A     359.0833  809.0627  150.5985  651.3763  200.7702
#B     338.8996  388.5825  612.0037  695.4628  709.0798
#C     760.0153  423.7709  956.7838  799.0494  663.9588
#D     319.5298  683.4278  144.7564  757.8725  694.7529
#E     294.5364  954.1390  223.0555  227.9699  347.4535
#F     117.1561  281.8261  205.4815  265.4050  227.5070
#G     944.5761  312.9263  741.1121  533.0935  833.5107
#H     145.8520  223.1062  802.7357  570.6363  871.7380
#I     591.0673  490.9409  722.8031  307.7193  226.9439
#J     152.7567  756.3408  970.0148  941.1165  125.0778
#====  ========  ========  ========  ========  ========

#$`2`
#...

经过编辑,它将是

lapply(split.default(df1[, -1], as.integer(gl(ncol(df1)-1,  5,
  ncol(df1) - 1))), function(x) 
         kable(cbind(df1[1], x), format = 'latex', row.names = FALSE) %>% 
             row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l"))

如果我们要添加分页符

f1 <- function(dat, i1, i2) {
     kable(dat[, c(1, i1:i2)], format = "latex", row.names = FALSE) %>% 
         row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")}

v1 <- seq(2, 17, by = 5)
v2 <- seq(6, 21, by = 5)
for(i in seq_along(v1) {
    print(f1(df1, v1[i], v2[i]))
    if(i < length(v1)) {
     cat("\n\n\pagebreak\n")
      }
    }