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

Try the papaja package in your browser

Any scripts or data that you put into this service are public.

papaja documentation built on Oct. 30, 2024, 9:09 a.m.