Skip to contents

swap_value_between_files() swaps the text located between specified begin and end tags within a source file and a target file. The value between the tags in the source file is extracted and swapped into the target file.

Usage

swap_value_between_files(
  from,
  to,
  begin_tag = "%:::% begin %:::%",
  end_tag = "%:::% end %:::%",
  value = NULL,
  quarto_render = FALSE,
  cite_method = "biblatex"
)

Arguments

from

A character string indicating the path to the source file to extract the value from.

to

A character string indicating the path to the target file to swap the value into.

begin_tag

(optional) A character string indicating the begin tag (default: "%:::% begin %:::%").

end_tag

(optional) A character string indicating the end tag (default: "%:::% end %:::%").

value

(optional) A character vector or a function that returns the value to swap into the target file. If NULL, the value is extracted from the source file (default: NULL).

quarto_render

(optional) A logical flag indicating whether to render the value using Quarto before swapping it into the target file. This is useful when the value contains Quarto syntax that needs to be processed (default: FALSE).

cite_method

(optional) A character string indicating the citation method to use when rendering with Quarto. Options are "citeproc", "biblatex", and "natbib" (default: "biblatex").

Value

A invisible NULL. The function is called for its side effect of modifying the target file.

See also

Examples

from_content <- c(
  "Introductory text.",
  "%:::% begin %:::%",
  "This is the value to swap into the target file.",
  "%:::% end %:::%",
  "This is some more text."
)

to_content <- c(
  "Introductory text.",
  "%:::% begin %:::%",
  "%:::% end %:::%",
  "This is some more text."
)

from_file <- tempfile(fileext = ".txt")
to_file <- tempfile(fileext = ".txt")

writeLines(from_content, from_file)
writeLines(to_content, to_file)

swap_value_between_files(
  from = from_file,
  to = to_file
)

readLines(to_file)
#> [1] "Introductory text."                             
#> [2] "%:::% begin %:::%"                              
#> [3] "This is the value to swap into the target file."
#> [4] "%:::% end %:::%"                                
#> [5] "This is some more text."