dbcoffee
dbcoffee

Reputation: 317

How do I change the default output location of the quarto document in Rstudio?

For example, my quarto file is in D:\myr\index.qmd, how can I output the generated PDF file to E:\myoutput?

Upvotes: 4

Views: 2113

Answers (1)

Shafee
Shafee

Reputation: 19857

The output file location can be specified with the output_file argument of quarto_render() function from {quarto} package.

So Ideally we can try something like this,

quarto::quarto_render("index.qmd", output_file = "E:/index.pdf")

index.qmd

---
title: "Untitled"
format: pdf
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

But this leads me to an error,

ERROR: The system cannot move the file to a different disk drive. (os error 17), 
rename 'index.pdf' -> 'E:/index.pdf'

Version Info

> xfun::session_info()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044), RStudio 2022.7.1.554

> quarto::quarto_version()
[1] ‘1.0.38’

Solution

We can use this function that will move the rendered output file to the desired directory.


render_qmd <- function(input_file, output_path, file_ext, ...) {
    # Extract just the input file name (without the file-extension)
    file_name <- xfun::sans_ext(input_file)

    # render the input document and output file will be in the
    # current working directory.
    quarto::quarto_render(input = input_file, output_format = file_ext, ...)

    # name of the rendered output file
    output_name <- paste0(file_name, ".", file_ext)

    # move the file to the output path
    fs::file_move(paste0(output_name), output_path)

    msg <- paste0(paste0(output_name, collapse = " and "), " moved to ", output_path)
    message(msg)
}

render_qmd("index.qmd", "E:/myoutput", file_ext = "pdf")

Note that, the packages {quarto}, {fs}, {xfun} are used in this function. So make sure that you have these pkgs installed.

Upvotes: 4

Related Questions