我创建了一个 R 项目,它使用 rmarkdown::render 和 .Rmd 文件将一些图表输出为 pdf。
当 R 项目和所有相关文件都保存在 Google Drive 时,我将该文件夹共享给了一位同事。我在 Mac 上创建了该项目,而同事使用的是 Windows。
当我在 Mac 上将 .Rmd 文件编织为 pdf 时,没有遇到任何问题。
当我在他的 Windows 上运行代码时出现以下错误:
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS Plot_per_DPUO.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output "Grafici Professioni/Periodo apr-14 ott-15 test/DH ALLERGOLOGIA - SAN PAOLO.pdf" --template "C:\Users\lucilla.rava\Documents\R\win-library\3.1\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"
pandoc.exe: Could not find image `Grafici%20Professioni\Periodo%20apr-14%20ott-15%20test\DH%20ALLERGOLOGIA%20-%20SAN%20PAOLO_files/figure-latex/set-options-1.pdf', skipping...
pandoc.exe: Error producing PDF from TeX source.
! I can't find file `C:/Users/LUCILL'.
<to be read again>
\protect
<*> C:/Users/LUCILL~
1.RAV/AppData/Local/Temp/tex2pdf.3172/input.tex
Please type another input file name
! Emergency stop.
<to be read again>
\protect
<*> C:/Users/LUCILL~
1.RAV/AppData/Local/Temp/tex2pdf.3172/input.tex
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on C:/Users/LUCILL~1.RAV/AppData/Local/Temp/tex2pdf.3172/tex
put.log.
Show Traceback
Rerun with Debug
Error: pandoc document conversion failed with error 43
似乎找不到绘图的临时文件。我猜测路径中的哈希分隔可能存在问题。在错误中,我看到 Unix 和 Windows 的 \ / 混合存在。
另外,路径中的用户名也很奇怪:
<*> C:/Users/LUCILL~
1.RAV/AppData/Local/Temp/tex2pdf.3172/input.tex
而它应该是 lucilla.rava。
下面是我调用 render() 的代码:
render.report <- function(type = 1, test = F) {
test <- if (test) ' test' else ''
months.vec <- get.months(unique(Data$Periodo))
months.vec <- format(months.vec, '%b-%y')
if (type == 1) folder.name <- 'Grafici Generali'
else if (type == 2) folder.name <- 'Grafici Professioni'
else if (type == 3) folder.name <- 'Grafici Indicazioni'
folder.path <- file.path(folder.name, sprintf('Periodo %s %s%s', months.vec[1], tail(months.vec, 1), test))
dir.create(folder.path, recursive = T, showWarnings = F)
for (UO in unique(Data$UO)) {
render('Report template.Rmd', clean = T, output_file = file.path(folder.path, sprintf('%s.pdf', sub(pattern = "/", replacement = ' ', UO, fixed = T))))
}
}
这是 .Rmd:
---
output: pdf_document
header-includes:
- \usepackage{tikz}
- \usepackage{booktabs}
- \usetikzlibrary{calc}
- \usepackage[utf8]{inputenc}
- \renewcommand*{\familydefault}{\sfdefault}
- \usepackage[none]{hyphenat}
- \pagenumbering{gobble}
---
\newcommand{\header}[1]{
\thispagestyle{empty}
\begin{tikzpicture}[overlay, remember picture]
\definecolor{head.col}{HTML}{5B9BD5}
\draw let \p1 = (current page.west), \p2 = (current page.east) in
node[minimum width=\x2-\x1, minimum height=2cm, draw, rectangle, fill=head.col, anchor=north west, align=center, text width=\x2-\x1] at ($(current page.north west)$) {\Large\bfseries #1};
\end{tikzpicture}
}
```{r set-options, echo=FALSE, cache=FALSE, fig.width=9, fig.height=7, results='asis'}
# if (type == 1) create.dpuo.graph(UO, T)
# if (type == 2) create.dpuo.prof.graph(UO, T)
# if (type == 3) create.dpuo.indc.graph(UO, T)
create.graph(UO, type, T)
create.table(UO, type)
我们需要该项目能够同时在我的电脑和他的电脑上运行。
谢谢