Answer a question

I encountered a problem when using saveWidget in VSCode, which might be caused by pandoc.

When I run the following line in Rstudio, it works well and mtcars.html can be generated

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

However, when I move the same code to VSCode, it gives me an error, saying that

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

I suspect VSCode didn't recognize the path to pandoc, since I typed find_pandoc in VScode to find the version and directory, showing that

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

$dir
NULL

However, in Rstudio it shows

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

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

Answers

In order to understand what is going on, let's take a look at the source code of rmarkdown:::find_pandoc(). We can find the following lines there:

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

sources are then used to fetch the pandoc path. I suspect that in your case RSTUDIO_PANDOC is not set, so rmarkdown:::find_pandoc() relies on find_program("pandoc") to find the path. If you in turn look at its source code, you will find out that the path is determined by running Sys.which, which is equivalent to extracting the path from the 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...

That being said, you need to add a path to pandoc to the PATH environment variable on your system.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐