Split a Rmd or Qmd file into a tibble and vice-versa"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(lightparser)

Parse and split a Rmd / Qmd file, and transform as tibble

{lightparser} reads your flat file to detect what is a yaml header, what is a code chunk and its options, what is a text part.
Function split_to_tbl() returns a tibble with all these parts.

file <- system.file(
  "dev-template-parsing.Rmd",
  package = "lightparser"
)
split_to_tbl(file)

Combine a parsed tbl Rmd / Qmd file into a new file

You can re-create the Rmd/Qmd file from the tibble returned by split_to_tbl(). As this is a tibble, before combining it as a new file, you can modify its content by removing or adding rows, replacing content, etc.

file <- system.file("dev-template-parsing.Rmd",
  package = "lightparser"
)
# split first
tbl_rmd <- split_to_tbl(file)
# apply your filters
tbl_rmd_filtered <- tbl_rmd[-5, ]
# combine then
combine_tbl_to_file(tbl_rmd_filtered, tempfile(fileext = ".Rmd"))


Try the lightparser package in your browser

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

lightparser documentation built on May 29, 2024, 4:39 a.m.