需要 pandoc 版本 1.12.3 或更高版本,但未找到该版本(R Shiny)。

82

我有一个问题,无法从我的托管在服务器上的Shiny应用程序生成PDF报告。

应用程序正常运行,但是当我按下下载报告的按钮时,出现以下错误:

 pandoc version 1.12.3 or higher is required and was not found.

问题是,如果我输入pandoc -v,我会得到:

 pandoc 1.12.3.3
 Compiled with texmath 0.6.6, highlighting-kate 0.5.6.1.
Syntax highlighting is supported for the following languages:
    actionscript, ada, apache, asn1, asp, awk, bash, bibtex, boo, c, changelog,
    clojure, cmake, coffee, coldfusion, commonlisp, cpp, cs, css, curry, d,
    diff, djangotemplate, doxygen, doxygenlua, dtd, eiffel, email, erlang,
    fortran, fsharp, gnuassembler, go, haskell, haxe, html, ini, java, javadoc,
    javascript, json, jsp, julia, latex, lex, literatecurry, literatehaskell,
    lua, makefile, mandoc, markdown, matlab, maxima, metafont, mips, modelines,
    modula2, modula3, monobasic, nasm, noweb, objectivec, objectivecpp, ocaml,
    octave, pascal, perl, php, pike, postscript, prolog, python, r,
    relaxngcompact, restructuredtext, rhtml, roff, ruby, rust, scala, scheme,
    sci, sed, sgml, sql, sqlmysql, sqlpostgresql, tcl, texinfo, verilog, vhdl,
    xml, xorg, xslt, xul, yacc, yaml
 Default user data directory: /home/daniele/.pandoc
 Copyright (C) 2006-2013 John MacFarlane
 Web:  http://johnmacfarlane.net/pandoc
 This is free software; see the source for copying conditions.  There is no
 warranty, not even for merchantability or fitness for a particular purpose.

我想我有正确的版本。TexLive也已安装,路径在$PATH中。

Server.R

library(shiny)
library(drsmooth)
library(shinyBS)
library(knitr)
library(xtable)
library(rmarkdown)

shinyServer(function(input, output,session) { 

 output$downloadReport <- downloadHandler(
filename = function() {
  paste('report', sep = '.','pdf')
},

content = function(file) {
  src <- normalizePath('report.Rmd')

  # temporarily switch to the temp dir, in case you do not have write
  # permission to the current working directory
  owd <- setwd(tempdir())
  on.exit(setwd(owd))
  file.copy(src, 'report.Rmd')

  library(rmarkdown)
  out <- render('report.Rmd')
  file.rename(out, file)
})

output$tb <- renderUI({
             p(h4("Report")),
            "Dowload a the report of your analysis in a pdf format",
            tags$br(),downloadButton('downloadReport',label="Download report"),
            tags$em("This option will be available soon")
     })
})

* report.Rmd* 不包含任何计算,它只是文本。 在我的本地版本(MacOS)上,PDF生成工作正常,但在服务器上不行。

如果需要,我可以提供其他信息。

12个回答

130

进入RStudio并查找RSTUDIO_PANDOC的系统环境变量。

Sys.getenv("RSTUDIO_PANDOC")

然后在调用渲染命令之前,将其放入您的R脚本中。

Sys.setenv(RSTUDIO_PANDOC="--- insert directory here ---")

在我苦苦寻找rmarkdown如何找到pandoc时,这对我很有用。 我不得不检查GitHub以查看源代码。


2
这对我有效,而在bash中使用“export”命令则没有效果(我尝试了将“pandoc”目录附加到$PATH和设置$RSTUDIO_PANDOC)。 - knowah
2
如果您没有安装RStudio,这个还能用吗? - user5359531
8
另一个选项是将以下内容添加到你的项目本地 .Renviron 文件中:RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc。参考链接:https://csgillespie.github.io/efficientR/3-3-r-startup.html#renviron - Interfector
可以在终端中使用 which pandoc 查找 pandoc 的位置,然后使用 usethis::edit_r_environ() 将其放入 .Renviron 中。 - F. Privé

19

为了让这个方法适用于您所有的R脚本,另一个选项是全局定义此变量。

在Debian / Ubuntu上,将以下行添加到您的.bashrc文件中:

export RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc

在 macOS 上,将以下内容添加到您的 .bash_profile 文件中:

export RSTUDIO_PANDOC=/Applications/RStudio.app/Contents/MacOS/pandoc

在Windows上(使用Git Bash),将以下内容添加到您的.bashrc文件中:

export RSTUDIO_PANDOC="/c/Program Files/RStudio/bin/pandoc/"

13

我解决这个问题的最简单方法是在调用RMarkdown::render之前将Sys.setenv(..)命令传递到crontab命令中。您需要使用分号将两个命令分开:

最简单的解决方法是在调用 RMarkdown::render 之前,在 crontab 命令中传递 Sys.setenv(..) 命令。您需要使用分号将两个命令分隔:

R -e "Sys.setenv(RSTUDIO_PANDOC='/usr/lib/rstudio-server/bin/pandoc'); rmarkdown::render('File.Rmd', output_file='output.html')"

(请记住,rstudio-server路径与非服务器版本不同)


4

我使用的是Arch Linux和RStudio。对于我来说唯一有效的方法是:

sudo pacman -S pandoc

:)


1
以下是如何编写一个好的回答?的一些指南。这个提供的答案可能是正确的,但加上解释会更有益。 - Trenton McKinney

3

如果您没有使用RStudio,您可能只需要在系统上安装pandoc。对我来说是这样的

sudo pacman -S pandoc

它在Arch Linux上运行良好。


2

如果有人遇到这个问题并且使用anaconda,那么他们可能会遇到我的问题。当rstudio shell启动时,它不会加载.bashrc文件,这意味着如果您的pandoc版本是在anaconda中安装的,Rstudio将无法找到它。像sudo pacman -S pandoc这样的命令单独安装pandoc对我很有效!


2

对于Windows 10,RStudio 2022.12.0版本,Pandoc已经与RStudio一起安装,因此我更喜欢使用已安装的pandoc.exe。据我所知,它的安装位置会不时发生变化。在过去的几年中,我看到它在以下位置(顶部是我的当前版本RStudio中的位置)。

2023年1月 - “C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools”
2022年8月 - “C:/Program Files/RStudio/bin/quarto/bin/tools”
“C:/Program Files/RStudio/bin/quarto/bin”
“C:/Program Files/RStudio/bin/pandoc”

一旦您知道预安装的pandoc的位置,您可以将以下行包含在.R文件中,如Chris/Yihui的最佳答案所示,并且这适用于我。

Sys.setenv(RSTUDIO_PANDOC = "C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools")

2

我在使用Debian 10构建bookdown文档时,遇到了与pandoc类似的问题。在Makefile中,我所做的是:

# use rstudio pandoc
# this rule sets the PANDOC environment variable from the shell
build_book1:
    export RSTUDIO_PANDOC="/usr/lib/rstudio/bin/pandoc";\
    Rscript -e 'bookdown::render_book("index.Rmd", "bookdown::gitbook")'

# use rstudio pandoc
# this rule sets the environment variable from R using multilines
build_book2:
    Rscript -e "\
    Sys.setenv(RSTUDIO_PANDOC='/usr/lib/rstudio/bin/pandoc');\
    bookdown::render_book('index.Rmd', 'bookdown::gitbook')"

这两个规则是等效的,并成功地编织了这本书。
我只是不喜欢冗长的Rscript命令:
Rscript -e "Sys.setenv(RSTUDIO_PANDOC='/usr/lib/rstudio/bin/pandoc'); bookdown::render_book('index.Rmd', 'bookdown::gitbook')"

1

嘿,我刚刚解决了这个错误。我通过删除 shiny-server 文件夹中的 2 个 pandoc 文件 "pandoc" 和 "pandoc-citeproc" 来解决这个问题。然后,我从 rstudio-server 文件夹中为每个文件创建了一个链接。这很顺利地解决了问题。当我尝试在运行 Linux 机器上的 shiny-server 中嵌入 rmarkdown 文档中的 leaflet 时,这对我是一个问题。我发现很奇怪的是,当我在同一台 Linux 机器上在 rstudio 中运行它时,它正常工作,但是在使用 shiny-server 运行时却不行。因此,shiny-server 安装的 pandoc 版本已经过时了。 干杯


1

如果您正在尝试在Windows命令行中运行脚本,您只需要将目录路径添加到PATH变量中。您还可以创建一个名为RSTUDIO_PANDOC的单独用户变量,并将该变量赋予目录。然后关闭并重新打开任何终端以刷新系统路径。

*如果出现问题,请尝试使用尾随/。 **我无法指向UNC路径。路径开头的//会破坏rmarkdown包pandoc函数。如果您使用UNC路径,则必须将其映射到驱动器并引用驱动器号。有方法可以动态执行此操作。我使用了一个DOS/batch脚本,我是通过Google找到的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接