chuxinyuan 感谢您的帮助,现在已经可以导出pdf文档了。但是如果插入的r代码如果含有中文字符,在pdf文档中似乎会出现报错信息。请问这该如何解决呢?
代码如下
---
title: "测试中文 PDF "
output:
pdf_document:
latex_engine: xelatex
# latex_engine: lualatex
# header-includes:
# - \usepackage{ctex}
documentclass: ctexart
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
先观察Titanic的数据信息.
```{r}
head(Titanic)
```
可以看出, Titanic数据集有Class, Sex, Age, Survived, Freq五个变量, 并且, 前四个是类别变量, Freq是数值变量.
绘制各个类别变量的条形图.
```{r}
t = as.data.frame(Titanic)
freq = t$Freq
df1 = cbind(t$Class, t$Freq)
vec = c(sum(df1[df1[,1]==1,2]),
sum(df1[df1[,1]==2,2]),
sum(df1[df1[,1]==3,2]),
sum(df1[df1[,1]==4,2]))
names(vec) = c('1st', "2nd", "3rd", "Crew")
x = sort(vec, decreasing = TRUE)
bar = barplot(x, xlab = "船客类别", ylab = '人数')
```
导出的部分pdf如下
我把插入的r代码中的中文字符改成英文后,可以生成正常的pdf。