How can I run a Rmarkdown file in VScode?
Asked Answered
M

3

7

I'm using VSCode for R programming. As I try different ways of plotting my data, I thought it might be a nice idea to use a Rmd file to get track of what I tried but my problem is I can't make this work.

I have rmarkdown and knitr installed but when I knit my .Rmd file, I get this output:

[VSC-R] 4.1_main.Rmd process started
==> rmarkdown::render('c:\\Users\\tl100\\Documents\\R\\thesis_figures\\4.1_main.Rmd')
Error: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available).

Execution halted

[VSC-R] 4.1_main.Rmd process exited with exit code 1
[VSC-R] terminating R process

So I tried to install pandoc: installr::install.pandoc(), but I get another error message:

> installr::install.pandoc()
trying URL 'https://github.com/'
downloaded 201 KB


The file was downloaded successfully into:
 C:\Users\tl100\AppData\Local\Temp\RtmpwbBZMs/github.com

Running the installer now...
Die Version von C:\Users\tl100\AppData\Local\Temp\RtmpwbBZMs\github.com ist mit der ausgeführten Windows-Version nicht kompatibel. Überprüfen Sie die Systeminformationen des Computers, und wenden Sie sich anschließend an den Herausgeber der Software.

Installation status:  FALSE . Removing the file:
 C:\Users\tl100\AppData\Local\Temp\RtmpwbBZMs/github.com
 (In the future, you may keep the file by setting keep_install_file=TRUE) 
Warning message:
In shell(install_cmd, wait = wait, ...) :
  'C:\Users\tl100\AppData\Local\Temp\RtmpwbBZMs/github.com' execution failed with error code 1
> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

However, I could successfully install pandoc via the msi installer from its github page (https://github.com/jgm/pandoc/releases/tag/3.1.9). Then, I check if pandoc is availalbe in my PATH variable:

enter image description here

But still, if I run rmarkdown::pandoc_available(), it says FALSE.

So is there a way of running Rmd files from VSCode, or do I have to use RStudio instead?

I would very much appreciate your help :-)

Moorhead answered 16/11, 2023 at 10:7 Comment(1)
There are some helpful answers right now but in the meantime I used Quarto instead ;-)Moorhead
L
2

You could create a conda environment with R in the terminal of vs code, for example like this:

conda create -n rmarkdown_in_vscode r-essentials r-base
conda activate rmarkdown_in_vscode

After that you simple run the file from the terminal using this:

Rscript -e "rmarkdown::render('rmarkdown_in_vs_code.Rmd')"

Then you can find your output file rmarkdown_in_vs_code.html in your directory.


Alternatively you can use the R extension for Visual Studio Code like described in the link. After that you can again simply run the code in your terminal like this:

Rscript -e "rmarkdown::render('rmarkdown_in_vs_code.Rmd')"
Laudianism answered 14/5 at 9:43 Comment(0)
P
2

Possible Failure Reason

I think you failed to install pandoc due to the wrong directory format in Windows

The file was downloaded successfully into:
 C:\Users\tl100\AppData\Local\Temp\RtmpwbBZMs/github.com

As you can see, the last folder name is /github.com, while .com is not a valid directory name in Windows system, so you indeed failed to download the package to the designated folder as prompted in console.


Workaround

You can refer to the posted answers in a previous question How to install Pandoc for Windows?, and the resources of pandoc can be found here.

In your specific case, since you are using Windows, you can easily download the .msi file and install it. After installation, you can check if the the path to pandoc is added into PATH in Windows' environment variables (I think it is automatically done within the installation process, but better to double check; Otherwise, just manually add it). enter image description here

Then, you can restart your VSC and run rmarkdown::find_pandoc(), and you may see something like below

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

$dir
[1] "C:/PROGRA~1/Pandoc"
Pentaprism answered 14/5 at 10:58 Comment(0)
C
2

I think the problem here is that VSCode doesn't know where to find pandoc because a temporary environmental variable has not been set. Setting the variable (among several others) is automatic when starting an R session using RStudio products. I tried this and it seemed to work:

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

rmarkdown::pandoc_available()
# [1] TRUE

You can make this 'sticky' using suggestions here: Setting environment variables for RStudio

Collett answered 15/5 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.