diff --git a/NEWS.md b/NEWS.md index 82fabf99f3..4604112bfd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ rmarkdown 2.10 - New supported syntax for Shiny prerendered documents: you can now use `server: shiny` or `server: type: shiny`. +- Fix intermediate files being created at the current directory instead of the input directory when the input file name contains shell characters and `render(intermediates_dir = NULL)` (thanks, @atusy, #1982). + + rmarkdown 2.9 ================================================================================ diff --git a/R/render.R b/R/render.R index 18bca61099..1a8494b23c 100644 --- a/R/render.R +++ b/R/render.R @@ -328,6 +328,8 @@ render <- function(input, if (!dir_exists(intermediates_dir)) dir.create(intermediates_dir, recursive = TRUE) intermediates_dir <- normalize_path(intermediates_dir) + } else { + intermediates_dir <- normalize_path(dirname(input)) } intermediates_loc <- function(file) { if (is.null(intermediates_dir)) @@ -336,12 +338,17 @@ render <- function(input, file.path(intermediates_dir, file) } - # resolve output directory before we change the working directory in + # resolve output directory and file before we change the working directory in # preparation for rendering the document if (!is.null(output_dir)) { if (!dir_exists(output_dir)) dir.create(output_dir, recursive = TRUE) output_dir <- normalize_path(output_dir) + } else if (!is.null(output_file)) { + # if output_dir is not null, output file becomes relative to output_dir later + output_file <- structure(file.path(normalize_path(dirname(output_file)), + basename(output_file)), + class = class(output_file)) } # check whether this document requires a knit @@ -367,12 +374,6 @@ render <- function(input, file.copy(input, input_no_shell_chars, overwrite = TRUE) intermediates <- c(intermediates, input_no_shell_chars) input <- input_no_shell_chars - - # if an intermediates directory wasn't explicit before, make it explicit now - if (is.null(intermediates_dir)) { - intermediates_dir <- - dirname(normalize_path(input_no_shell_chars)) - } } # never use the original input directory as the intermediate directory, @@ -483,7 +484,7 @@ render <- function(input, } pandoc_to <- output_format$pandoc$to - # generate outpout file based on input filename + # generate output file based on input filename output_auto <- pandoc_output_file(input, output_format$pandoc) if (is.null(output_file) || is.na(output_file)) output_file <- output_auto else { if (!inherits(output_file, "AsIs") && xfun::file_ext(output_file) == "")