Skip to contents

Reads an R script file, finds a pair of marker tags (an opening tag and a closing tag), and replaces the content between those tags with new content. After replacing the content, the marker tags are removed and the updated file is formatted using styler::style_file().

Usage

fill_placeholders(
  file_path,
  new_content,
  opening_tag = "<<<<",
  closing_tag = ">>>>"
)

Arguments

file_path

Character. Path to the R script file to modify.

new_content

Character vector. Content to insert between the tags.

opening_tag

Character. The opening marker tag. Default is "<<<<".

closing_tag

Character. The closing marker tag. Default is ">>>>".

Value

None. This function updates the specified file in place.

Examples

if (FALSE) { # \dontrun{
# Create a temporary file to simulate an R script.
temp_file <- tempfile(fileext = ".R")

# Define initial content for the file with tags delimiting content to be replaced.
initial_content <- c(
  "# This is a sample R script",
  "print('This is before the tag')",
  "<<<<",                            # Opening tag
  "Old content to be replaced",      # Content to be replaced
  ">>>>",                            # Closing tag
  "print('This is after the tag')"
)

# Write the initial content to the temporary file.
writeLines(initial_content, temp_file)

# Display the file content BEFORE replacement.
cat("File content BEFORE replacement:\n")
cat(readLines(temp_file), sep = "\n")
cat("\n\n")

# Define the new content to insert between the tags.
new_content <- c("print('New content inserted')")

# Call the function to replace the content between the tags.
fill_placeholders(file_path = temp_file, new_content = new_content)

# Display the file content AFTER replacement.
cat("File content AFTER replacement:\n")
cat(readLines(temp_file), sep = "\n")
} # }