如何使用Rmarkdown中的kable
功能为PDF报告创建格式化表格?这是一个MWE:
library(knitr)
df <- data.frame(bucket = 1:11,
value = c(-0.8125594, -0.7590050, -0.7189301, -0.7188391, -0.5047816,
-0.3439579, -0.4376782, -0.1300217, 0.9145718, 2.1844290,
4.8374356))
print(kable(df))
输出:
| bucket| value|
|------:|----------:|
| 1| -0.8125594|
| 2| -0.7590050|
| 3| -0.7189301|
| 4| -0.7188391|
| 5| -0.5047816|
| 6| -0.3439579|
| 7| -0.4376782|
| 8| -0.1300217|
| 9| 0.9145718|
| 10| 2.1844290|
| 11| 4.8374356|
我正在尝试将其格式化为具有良好视觉效果的PDF,如附图所示:
答案 0 :(得分:2)
无需将kable
函数与print
包装在一起。这是一个基本的RMarkdown文档,它演示了它:
---
title: "MWE"
output: pdf_document
---
```{r, message = FALSE}
library(knitr)
df <- data.frame(bucket = 1:11,
value = c(-0.8125594, -0.7590050, -0.7189301, -0.7188391, -0.5047816,
-0.3439579, -0.4376782, -0.1300217, 0.9145718, 2.1844290,
4.8374356))
kable(df)
```
如果您需要进一步自定义表格,我建议您查看
kableExtra
包:https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
答案 1 :(得分:1)