r - 指示 RMarkdown 使用相对文件路径或停止缩短文件夹名称
问题描述
我有一个参数化的 RMarkdown PDF 报告,我正在从闪亮的仪表板运行(在将其复制到临时目录之后)。
示例代码:
闪亮的仪表板:
---
title: "Company Report Frontend"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}
library(flexdashboard)
library(shiny)
```
### Select Company
```{r}
textInput('name', label ='Firm')
```
### Report
```{r}
uiOutput("downloadUI")
# Create the actual downloadButton
output$downloadUI <- renderUI( {
downloadButton("downBtn", "Download Report")
})
# Add download handling
output$downBtn <- downloadHandler(
filename = "full_report.pdf",
content = function(file) {
tempReport <- file.path(tempdir())
file.copy("test_report.Rmd", paste0(tempReport, "/test_report.Rmd"), overwrite = TRUE)
rmarkdown::render(paste0(tempReport, "/test_report.Rmd"), output_file = file,
params = list(input=input),
envir = new.env(parent = globalenv()), clean = FALSE,
knit_root_dir = tempReport,
)
}
)
```
test_report.RMD
:
---
title: "Report"
header-includes:
\usepackage{graphicx}
params:
input: NA
output:
pdf_document:
latex_engine: xelatex
---
```{r}
input <- params$input
data(mtcars)
plot(mtcars$hp, mtcars$mpg)
title(input$name)
```
当我运行它时,它成功生成了.tex
文件,但随后无法编译。当我尝试.tex
直接在 LaTeX 编辑器中编译文件时,我在任何一行都出现错误,undefined control sequence
如下所示:Missing endcsname defined
\includegraphics
\includegraphics{C:/User/LONGUS~1/Temp/file5b381fa967c8_files/figure-latex/unnamed-chunk-1-1.pdf}
其中LONGUS~1
是实际 Windows 用户名 LONGUSERNAME 的缩短文件夹名称。
错误消失,并且 PDF 编译,如果我替换LONGUS~1
为LONGUSERNAME
,或者只是将它指向相对文件路径。LaTeX 有时确实倾向于对文件路径挑剔。
如何指示 RMarkdown 避免缩短文件夹名称,或跳过绝对文件路径而只使用相对文件?test_report.RMD
如果我自己运行它(并指定一个 default input
)编译得很好,所以我猜这与函数的使用tempdir()
或至少是render()
函数中的某些东西有关。但我真的应该tempdir()
在闪亮应用程序的多个同时用户的情况下保留这些东西。我确实尝试删除该knit_root_dir
选项,但这并没有解决它。
欢迎任何建议。谢谢!
解决方案
推荐阅读
- excel - 如何计数然后查找?
- npm - 在本地和全局安装 npm 依赖项在执行命令行时会产生错误
- outlook-redemption - 是否可以在不处理项目的情况下复制文件夹结构?
- java - 生成契约契约:如何创建匹配任何东西的 PactDslJsonBody
- python - Python 正则表达式搜索返回正非整数 <1 作为空字符串
- tensorflow - 如何在每个时期打印一个张量
- go - 对于看似有效的 png,输入字节处的非法 base64 数据
- node.js - 在 Sequelize 中有一个关联
- powershell - 未保存在 PowerShell 中的变量
- javascript - 使用 Div 作为表格,如何按特定列排序?