通过 for 循环为 pdf 输出创建 flextables
creating flextables through a for loop for pdf output
我在数据帧列表中创建弹性表时遇到问题。
我已经尝试将 flextable 作为一个函数,然后 运行 通过 for 循环这个函数,但是我的 pdf 中没有得到任何输出。
---
title: "Cut"
author: "Question"
date: "10/9/2019"
output: pdf_document
---
```{r setup, include=FALSE}
library(flextable)
df <- data.frame("Item"=c("A","B","C","A","B","C"), "Qty"=c(5,10,15,20,25,30), "Location" = c("NY", "NJ","NY", "NJ","NY", "NJ"))
chunk <- 2
n <- nrow(df)
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n]
d <- split(df,r)
Table_func <- function(the_table){
Table = flextable(the_table)
Table <- align(Table, align = "center", part = "all")
}
Table_out = c()
for(i in seq_along(d)){
Table_out[[i]] = Table_func(as.data.frame(d[[i]]))
}
Table_out
```
这个怎么样:
```{r echo = FALSE, results = 'asis'}
library(flextable)
library(knitr)
library(dplyr)
df <- data.frame("Item"=c("A","B","C","A","B","C"), "Qty"=c(5,10,15,20,25,30), "Location" = c("NY", "NJ","NY", "NJ","NY", "NJ"))
chunk <- 2
n <- nrow(df)
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n]
d <- split(df,r)
results <- character()
for(i in seq_along(d)){
tb <- d[[i]] %>%
as.data.frame() %>%
flextable() %>%
align(align = "center", part = "all")
results <- c(results, knit_print(tb))
}
asis_output(results)
```
我在数据帧列表中创建弹性表时遇到问题。
我已经尝试将 flextable 作为一个函数,然后 运行 通过 for 循环这个函数,但是我的 pdf 中没有得到任何输出。
---
title: "Cut"
author: "Question"
date: "10/9/2019"
output: pdf_document
---
```{r setup, include=FALSE}
library(flextable)
df <- data.frame("Item"=c("A","B","C","A","B","C"), "Qty"=c(5,10,15,20,25,30), "Location" = c("NY", "NJ","NY", "NJ","NY", "NJ"))
chunk <- 2
n <- nrow(df)
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n]
d <- split(df,r)
Table_func <- function(the_table){
Table = flextable(the_table)
Table <- align(Table, align = "center", part = "all")
}
Table_out = c()
for(i in seq_along(d)){
Table_out[[i]] = Table_func(as.data.frame(d[[i]]))
}
Table_out
```
这个怎么样:
```{r echo = FALSE, results = 'asis'}
library(flextable)
library(knitr)
library(dplyr)
df <- data.frame("Item"=c("A","B","C","A","B","C"), "Qty"=c(5,10,15,20,25,30), "Location" = c("NY", "NJ","NY", "NJ","NY", "NJ"))
chunk <- 2
n <- nrow(df)
r <- rep(1:ceiling(n/chunk),each=chunk)[1:n]
d <- split(df,r)
results <- character()
for(i in seq_along(d)){
tb <- d[[i]] %>%
as.data.frame() %>%
flextable() %>%
align(align = "center", part = "all")
results <- c(results, knit_print(tb))
}
asis_output(results)
```