首页 > 解决方案 > 指示 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~1LONGUSERNAME,或者只是将它指向相对文件路径。LaTeX 有时确实倾向于对文件路径挑剔。

如何指示 RMarkdown 避免缩短文件夹名称,或跳过绝对文件路径而只使用相对文件?test_report.RMD如果我自己运行它(并指定一个 default input)编译得很好,所以我猜这与函数的使用tempdir()或至少是render()函数中的某些东西有关。但我真的应该tempdir()在闪亮应用程序的多个同时用户的情况下保留这些东西。我确实尝试删除该knit_root_dir选项,但这并没有解决它。

欢迎任何建议。谢谢!

标签: rshinylatexr-markdownflexdashboard

解决方案


推荐阅读