Две проблемы с рендерингом файла qmd с помощью quarto_render из R

У меня есть файл my_file_to_render.qmd, написанный в RStudio с помощью R. Я пытаюсь отобразить этот файл с выходным форматом html из файла wrapper.R. В wrapper.RI я использую:

      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')

Я столкнулся с двумя проблемами, которые я не могу решить.

  1. Я обнаружил, что файл html действительно появляется в подкаталоге «HTML», как я и хотел, однако папка, сопровождающая my_file_to_render_20220523.html, по-прежнему отображается в рабочем каталоге. Следовательно, когда я открываю my_file_to_render_20220523.html, изображения и форматирование отсутствуют. Какие-либо предложения?
  2. Мне нужно, чтобы папка, в которой хранится форматирование html-файла, имела такое же имя. Мне нужен файл my_file_to_render_20220523, чтобы найти папку my_file_to_render_20220523, и my_file_to_render_20220522, чтобы найти папку my_file_to_render_20220522. В настоящее время это не работает.

Кто-нибудь может помочь?

Фил,

1 ответ

У меня была такая же проблема, которая также обсуждается на дискуссионных форумах quarto-cli здесь: https://github.com/quarto-dev/quarto-cli/discussions/2171

Мое решение состояло в том, чтобы написать свою собственную функцию, которая является оболочкой вокругquarto::quarto_render()который просто перемещает визуализированный вывод в желаемоеoutput_dir. Я назвал функциюquarto_render_move()и поместить его в мой личный пакет R {jph} . Вы можете увидеть функцию на https://github.com/jhelvy/jph/blob/master/R/quarto_render_move.R.

Вот код функции:

      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)
    }
}
Другие вопросы по тегам