Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two problems rendering a qmd file with quarto_render from R

I have a file my_file_to_render.qmd written in RStudio with R. I am trying to render this file with output html format from a file wrapper.R. In wrapper.R I am using:

a_param = "a_name"
quarto_render(input = "my_file_to_render.qmd",
       output_file = paste0(".\\HTML\\my_file_to_render","_",today(), '.html'),
       execute_params =list(pcn = a_param), 
       output_format='html')

I'm encountering two problem which I can't work out.

  1. I find that the html file does indeed appear in sub-directory 'HTML' as I want however the folder that accompanies my_file_to_render_20220523.html still appears in the working directory. Hence when I open my_file_to_render_20220523.html pictures and formatting are missing. Any suggestions?
  2. I need the folder which stores the formatting to the html file to have the same name. I need file my_file_to_render_20220523 to look in folder my_file_to_render_20220523 and my_file_to_render_20220522 to find the folder my_file_to_render_20220522. Currently that isn't working out.

Could anybody help?

Phil,

like image 449
Philip Avatar asked Nov 01 '25 10:11

Philip


1 Answers

I had this same problem, which is also discussed in the quarto-cli discussion forums here: https://github.com/quarto-dev/quarto-cli/discussions/2171

My solution was to write my own function which is a wrapper around quarto::quarto_render() that simply moves the rendered output to a desired output_dir. I named the function quarto_render_move() and put it in my personal R package {jph}. You can see the function at https://github.com/jhelvy/jph/blob/master/R/quarto_render_move.R

Here is the function code:

quarto_render_move <- function(
    input,
    output_file = NULL,
    output_dir = NULL,
    ...
) {

    # Get all the input / output file names and paths
    x <- quarto::quarto_inspect(input)
    output_format <- names(x$formats)
    output <- x$formats[[output_format]]$pandoc$`output-file`
    if (is.null(output_file)) { output_file <- output }
    input_dir <- dirname(input)
    if (is.null(output_dir)) { output_dir <- input_dir }
    output_path_from <- file.path(input_dir, output)
    output_path_to <- file.path(output_dir, output_file)

    # Render qmd file to input_dir
    quarto::quarto_render(input = input, ... = ...)

    # If output_dir is different from input_dir, copy the rendered output
    # there and delete the original file
    if (input_dir != output_dir) {

        # Try to make the folder if it doesn't yet exist
        if (!dir.exists(output_dir)) { dir.create(output_dir) }

        # Now move the output to the output_dir and remove the original output
        file.copy(
            from = output_path_from,
            to = output_path_to,
            overwrite = TRUE
        )
        file.remove(output_path_from)

    # If the output_dir is the same as input_dir, but the output_file
    # has a different name from the input file, then just rename it
    } else if (output_file != output) {
        file.rename(from = output_path_from, to = output_path_to)
    }
}
like image 189
jhelvy Avatar answered Nov 03 '25 00:11

jhelvy