7

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

James Crumpler
  • 192
  • 1
  • 8
  • 2 more potential helpful points of reference... ` quarto_path() = "C:\\PROGRA~1\\RStudio\\bin\\quarto\\bin\\quarto.cmd" ; quarto_version() = ‘0.9.649’ ` – James Crumpler Aug 29 '22 at 17:19
  • 2
    @JamesCrumpler, your error is not `cmd` related, therefore I am not seeing the issue here with `cmd` (`batch-file`) are you able to run the command outside of a `batch-file` successfully? – Gerhard Aug 29 '22 at 17:56
  • Can you demonstrate to me how you run the code outside of the batch file? Sorry for the step by step approach, but I have good reason for it. – Gerhard Aug 29 '22 at 18:00
  • 1
    here is what I would do (seeing as the batch is in the same path as the files) `cd /d %~dp0` then `if exist .\test_document.qmd "C:\Program Files\R\R-4.2.1\bin\x64\Rscript.exe" ".\test_script.R"` Also, for the sake of my sanity, rename the file with a `.cmd` extension instead of `.bat` – Gerhard Aug 29 '22 at 18:02
  • @Gerhard - Corrected the .bat to .cmd (thank you), and working through the rest of the proposed solution – James Crumpler Aug 29 '22 at 18:11
  • @Gerhard - updated the now .cmd file with the recommended code, and output is the same as before, which makes me think that the error is with `quarto` in some way...this is the closest discussion I could find that seemed similar to my current problem https://community.rstudio.com/t/render-with-quarto-error/134403/40?page=2 – James Crumpler Aug 29 '22 at 18:29

3 Answers3

3

To save having two files, you can do:

library(quarto)
library(here)

# quarto_render(here("test_document.qmd"))
system(paste0('quarto.cmd render ', here("test_document.qmd")))

If quarto.cmd isn't in your path, you may need

system(paste0('"D:/Program Files/RStudio/bin/quarto/bin/quarto.cmd" render ', here::here("test_document.qmd")))
  • This almost worked for me, however my .qmd file was in a path that contained spaces. For the command to run I needed to add quotation marks around that file location. I struggled to do this initially as the paste command was adding extra \ characters to the output. In the end I solved this by using capture.output(cat(x)). The full command I used was: system(capture.output(cat('quarto.cmd render ', "\"",here("Scripts/Report.qmd"),"\"",sep=""))) – mob Jun 26 '23 at 00:36
0

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!

James Crumpler
  • 192
  • 1
  • 8
0

With RStudio Update to 2022.12.0+353

The render of quarto is moved to a different location...

D:
cd "D:\Program Files\Rstudio\resources\app\bin\quarto\bin"
quarto render "C:\Temp\Test\test_script.R"

This is a separate answer to accommodate the new workflow version.

James Crumpler
  • 192
  • 1
  • 8