如何解决pandoc问题

How to address the pandoc issue

我在VSCode中使用saveWidget时遇到问题,可能是pandoc造成的。

当我在Rstudio中运行下一行时,效果很好,可以生成mtcars.html

htmlwidgets::saveWidget(DT::datatable(mtcars), "mtcars.html", selfcontained = TRUE, title = "mtcars")

然而,当我将相同的代码移动到 VSCode 时,它给我一个错误,说

Error in htmlwidgets::saveWidget(DT::datatable(mtcars), "mtcars.html",  : 
  Saving a widget with selfcontained = TRUE requires pandoc. For details see:
https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md

我怀疑 VSCode 没有识别到​​ pandoc 的路径,因为我在 VScode 中输入 find_pandoc 来查找版本和目录,显示

> rmarkdown::find_pandoc()
$version
[1] '0'

$dir
NULL

然而,在 Rstudio 中显示

> find_pandoc()
$version
[1] ‘2.7.2’

$dir
[1] "C:/Program Files/RStudio/bin/pandoc"

为了弄明白是怎么回事,我们先来看看rmarkdown:::find_pandoc()的源码。我们可以在那里找到以下几行:

sys_pandoc <- find_program("pandoc")
sources <- c(Sys.getenv("RSTUDIO_PANDOC"), if (nzchar(sys_pandoc)) dirname(sys_pandoc))

sources 然后用于获取 pandoc 路径。我怀疑在你的情况下 RSTUDIO_PANDOC 没有设置,所以 rmarkdown:::find_pandoc() 依赖 find_program("pandoc") 来找到路径。如果反过来看它的源码,会发现路径是由运行Sys.which决定的,相当于从shell:[=20=中提取路径]

The system command which reports on the full path names of an executable (including an executable script) as would be executed by a shell...

也就是说,您需要将 pandoc 的路径添加到系统上的 PATH 环境变量中。