目标 我一直想创建一个pdf报告功能,但是我无法按照自己的意愿重新设置pdf的样式。我要解决的问题之一是无法将表格向左对齐。现在它居中。
系统和其他
template.Rmd
---
title: "Reporting"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, results='asis', echo=FALSE}
knitr::kable(head(cars), format = "markdown") %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
```
build_report.R
# Libraries
require(knitr)
require(markdown)
library(RMySQL)
library(png)
library(kableExtra)
# create .md file
knit("template.Rmd", "template.md")
# create .html file
markdownToHTML("template.md", "template.html", options=c("use_xhml"))
# create .pdf file
command <- paste0("pandoc -V geometry:'left=0.5in,bottom=1in,top=1.5in' -s ", "template.html", " -o ", "output.pdf")
system(command)
output.pdf 输出示例(忽略了无关信息)1
答案 0 :(得分:0)
将format = "markdown"
中的kable
移到library(kableExtra)
上
答案 1 :(得分:0)
可以在@Hao的帮助下解决它
1)从.Rmd到.md直接转换为PDF,省略markdownToHTML()
2)将库(kableExtra)包含在 template.Rmd 中,而不包含在 build_report.R
中3)使用格式=“ markdown”
template.Rmd
---
title: "Reporting"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, results='asis', echo=FALSE}
knitr::kable(head(cars), format = "markdown") %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
```
build_report.R
# Libraries
require(knitr)
require(markdown)
library(RMySQL)
library(png)
library(kableExtra)
# create .md file
knit("template.Rmd", "template.md")
# create .pdf file
command <- paste0("pandoc -V geometry:'left=0.5in,bottom=1in,top=1.5in' -s ", "template.md", " -o ", "output.pdf")
system(command)