简体   繁体   English

knitr 在新页面上打印每个表格的 data.frames 列表

[英]knitr printing list of data.frames with each table on new page

I want to print list of data.frames with each table on new page.我想在新页面上打印每个表格的data.frames list The for-loop works fine but requires long code. for-loop工作正常,但需要很长的代码。 Tried with map function but could not get the required output.尝试使用map function 但无法获得所需的 output。 Not able to use \newpage command with map function.无法将\newpage newpage 命令与map function 一起使用。 It also gives some extra output like [[1]] NULL [[2]] NULL .它还提供了一些额外的 output ,例如[[1]] NULL [[2]] NULL Any hint, please.任何提示,请。

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

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

dfs <- 
  unname(
    split.default(
    df1[, -1], as.integer(gl(ncol(df1)-1,  5, ncol(df1) - 1))
    )
    )


library(kableExtra)
library(tidyverse)


for(i in 1:length(dfs)){
  print(
    kable(
      cbind(df1[1], dfs[i])
      , format = 'latex'
      , row.names = FALSE
    ) %>% 
      row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")
    )
  if(i < length(dfs)) {
  cat("\n\n\\newpage\n")
    }
  }

map(
  dfs
  , function(x) 
    print(
      kable(
       cbind(df1[1], x)
      , format = 'latex'
      , row.names = FALSE
    ) %>% 
    row_spec(row = c(0), bold = TRUE, italic = TRUE, align = "l")
    #cat("\n\n\\newpage\n")
    )
  #if(i < length(x))
    #cat("\n\n\\newpage\n")
  )

You can use purrr::imap to get the index in the .y variable:您可以使用purrr::imap来获取.y变量中的索引:

``` r
library(kableExtra)
library(tidyverse)
library(purrr)

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

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

dfs <-
  unname(split.default(df1[,-1], as.integer(gl(
    ncol(df1) - 1,  5, ncol(df1) - 1
  ))))

f <- function() {
  for (i in 1:length(dfs)) {
    print(
      kable(
        cbind(df1[1], dfs[i])
        ,
        format = 'latex'
        ,
        row.names = FALSE
      ) %>%
        row_spec(
          row = c(0),
          bold = TRUE,
          italic = TRUE,
          align = "l"
        )
    )
    if (i < length(dfs)) {
      cat("\n\n\\newpage\n")
    }
  }
}

f_map <- function() {
  invisible(dfs %>% purrr::imap(~ {
    print(
      kable(cbind(df1[1], .x)
            , format = 'latex'
            , row.names = FALSE) %>%
        row_spec(
          row = c(0),
          bold = TRUE,
          italic = TRUE,
          align = "l"
        )
      #cat("\n\n\\newpage\n")
    )
    if (.y < length(dfs))
      cat("\n\n\\newpage\n")
  }))
}

identical(capture.output(f()), capture.output(f_map()))
#> [1] TRUE

microbenchmark::microbenchmark( f = {res <- capture.output(f())}, f_map = {res <- capture.output(f_map())})
#> Unit: milliseconds
#>   expr      min       lq     mean   median       uq      max neval cld
#>      f 7.908510 8.431997 9.662659 9.012099 10.10318 15.42358   100   a
#>  f_map 7.983586 8.462561 9.797256 9.150356 10.71692 16.20676   100   a
```

<sup>Created on 2020-07-23 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

As stated by @CL, this is not faster nor shorter than a simple loop.正如@CL 所说,这并不比一个简单的循环更快或更短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM