Home > Net >  quarto_render broken from .bat / .cmd file
quarto_render broken from .bat / .cmd file

Time:09-01

This is a bit complicated, but I think others may be having this issue as well.

Quarto is fantastic, but have been confronting an issue where one function (quarto_render) is not able to render a document in a script that is accessed through a .bat / .cmd file. If I run the R script from the RStudio IDE, no problems, but accessed through a .bat, yes. Have been able to recreate the problem, which I will try to outline and hope someone can provide a method to resolve.

Unfortunately, to recreate it takes some time since three files are required (the actual project I'm working on uses five separate files). The source project folder I'm using is called Test, which does not really play a role till File 3.

File 1 - test_document.qmd

Create a new .qmd document (I called mine test_document.qmd). Set to an output of html, and kept default YAML and other text. No modifications needed.

File 2 - test_script.R

Create a new Script (test_script.R). In the following way...

library(quarto)
library(here)

quarto_render(here("test_document.qmd"))

When run, this should render the Test_document.qmd as a separate html file within the file location.

File 3 - test.cmd - (This is where problems start to occur)

A text file (.txt), when the extension is changed to .bat should render in the Command Prompt. The code used in the .cmd file is attached below, though have to redact sections since it is work based.

Folder start is "C:\Temp\Test"

{.cmd file}
cd /d %~dp0 
if exist .\test_document.qmd "C:\Program Files\R\R-4.2.1\bin\x64\Rscript.exe" "C:\Temp\Test\test_script.R"
PAUSE

Save the file to the same folder as the other two files for convenience. Now, when the test.cmd file is run (run by double clicking) the following occurs in the command prompt interface...

Output

{command prompt}

C:\Temp\Test>cd /d C:\Temp\Test\

C:\Temp\Test>if exist .\test_document.qmd "C:\Program Files\R\R-4.2.1\bin\x64\Rscript.exe" "C:\Temp\Test\test_script.R"
here() starts at C:/Temp/Test
Error in find_quarto() : Unable to find quarto command line tools.
Calls: quarto_render -> find_quarto
Execution halted

C:\Temp\Test>PAUSE

find_quarto is not defined in quarto package, though it does appear here...

https://rdrr.io/github/quarto-dev/quarto-r/src/R/quarto.R

This is as far as I can get. I would like use the .bat file, since the plan is to tie that file to the Task Scheduler in order to schedule actual project I'm working on to run once per week or so.

Session_info in case this is helpful...

> devtools::session_info()
─ Session info ──────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23 ucrt)
 os       Windows 10 x64 (build 19042)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/New_York
 date     2022-08-29
 rstudio  2022.07.0 548 Spotted Wakerobin (desktop)
 pandoc   2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)

To run script outside of Batch

Open RStudio IDE >

open test_script.R file >

with cursor in script select cntrl alt R to run

CodePudding user response:

After some experimentation, was able to come up with a two step process to work-around the quarto_render problem in running an Rscript from a .cmd file.

Step 1

Comment out quarto_render(), since this function will be moved to a .cmd file.

To render a quarto document from command line, found I needed to access quarto.cmd directly.

which looks like this in my .cmd file test...

cd /d %~dp0 
"D:\Program Files\R\R-4.2.1\bin\Rscript.exe" "C:\Temp\Test\test_script.R"


C:
cd "D:\Program Files\RStudio\bin\quarto\bin" 
quarto.cmd render "C:\Temp\Test\test_document.qmd"

I found this to work for my workflow to render a quarto doc from the command line, and thought it may be useful for other.

Two sources were extremely helpful to piecing this solution together...

Command prompt won't change directory to another drive

https://community.rstudio.com/t/bash-quarto-command-not-found/144187

Step 2

Finally, after rendering the quarto file, then run the remainder of the Rscript.

cd /d %~dp0 
"D:\Program Files\R\R-4.2.1\bin\Rscript.exe" "C:\Temp\Test\test_script.R"

Have since scheduled these actions in the Task Scheduler application, with the quarto render operation first, and the remaining script as second.

Hope this helps others!

  • Related