How to address the pandoc issue
Asked Answered
F

1

6

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"
Fango answered 16/6, 2020 at 13:18 Comment(5)
I don't know when I started doing it, but I have Sys.setenv(RSTUDIO_PANDOC="/R/RStudio/bin/pandoc") in my ~/.Rprofile (allegedly to not use a pandoc installed outside of R/RStudio). If you set that within your VSCode env, does find_pandoc() find it, and does the error go away?Fiery
@Fiery well...it seems the error is still thereFango
@Fango What does rmarkdown:::find_program("pandoc") return inside VSCode? Sys.getenv("RSTUDIO_PANDOC")? What about Sys.which("pandoc")? All inside VSCode. You may need to add a path to pandoc executable ("C:/Program Files/RStudio/bin/pandoc") to your system-wide PATH variable.Eileen
The problem is that the RSUDIO_PANDOC variable is not set to the pandoc path inside VSCode. If adding pandoc to PATH helps, I will write a detailed answer.Eileen
@Eileen Wow! Thanks a lot. Adding a PATH variable to system environment variable get it works! Please write down your answer with details and I will accept that!Fango
E
5

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.

Eileen answered 18/6, 2020 at 20:25 Comment(3)
@Fango is my source not reputable enough? Never answered a bountied question before and don't know how this works.Eileen
It seems the system will award the bounty one day after accepting the answer. No worries, I will do that when system allows me to do that.Fango
You say we need to add a path to pandoc to the PATH environment variable on out system but you don't say how to do that. Could you edit your answer to expound on that? I've found where pandoc.exe is but how do I tell R in VSCode where that is? In the VSCode JSON settings?Brittan

© 2022 - 2024 — McMap. All rights reserved.