使用saveWidget
时遇到问题在 VSCode 中,这可能是由 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
的路径, 因为我输入了 find_pandoc
在VScode中找到版本和目录,显示> 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中提取路径: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
系统上的环境变量。
关于r - 如何解决 pandoc 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62409482/