R/remove_comments.R

Defines functions remove_comments

Documented in remove_comments

#' Remove Comments
#'
#' Removes markdown comments from an R Markdown file.
#'
#' @param x Character. Path to an R Markdown file.
#' @param file Character. Name of the new R Markdown file without comments.
#' @return No return value, called to write text to `file`.
#' @export

remove_comments <- function(x, file) {

  x <- readLines_utf8(x)
  x <- x[!grepl("^<!--((?!-->).)+-->$", x, useBytes = TRUE, perl = TRUE)]
  x <- x[!grepl("^<@~\\{#.+\\}$", x, useBytes = TRUE)]
  x <- x[!grepl("^~@>$", x, useBytes = TRUE)]

  x <- gsub("<!--((?!-->).+)-->", "", x, useBytes = TRUE, perl = TRUE)

  writeLines(x, con = file, useBytes = TRUE)
}
crsh/papaja documentation built on Feb. 21, 2024, 6:11 p.m.