Learn R Programming

parsermd

The goal of parsermd is to extract the content of an R Markdown file to allow for programmatic interactions with the document’s contents (i.e. code chunks and markdown text). The goal is to capture the fundamental structure of the document and as such we do not attempt to parse every detail of the Rmd. Specifically, the yaml front matter, markdown text, and R code are read as text lines allowing them to be processed using other tools.

Installation

parsermd can be installed from CRAN with:

install.packages("parsermd")

You can install the latest development version of parsermd from GitHub with:

remotes::install_github("rundel/parsermd")
library(parsermd)

Basic Usage

For more details see the getting started vignette or any of the topic specific articles.

(rmd = parsermd::parse_rmd(system.file("minimal.Rmd", package = "parsermd")))
#> ├── YAML [4 lines]
#> ├── Heading [h1] - Setup
#> │   └── Chunk [r, 1 opt, 1 lines] - setup
#> └── Heading [h1] - Content
#>     ├── Heading [h2] - R Markdown
#>     │   ├── Markdown [6 lines]
#>     │   ├── Chunk [r, 1 lines] - cars
#>     │   └── Chunk [r, 1 lines] - unnamed-chunk-1
#>     └── Heading [h2] - Including Plots
#>         ├── Markdown [2 lines]
#>         ├── Chunk [r, 1 opt, 1 lines] - pressure
#>         └── Markdown [2 lines]

as_tibble(rmd)
#> # A tibble: 12 × 5
#>    sec_h1  sec_h2          type          label           ast           
#>    <chr>   <chr>           <chr>         <chr>           <rmd_ast>     
#>  1 <NA>    <NA>            rmd_yaml_list <NA>            <yaml>        
#>  2 Setup   <NA>            rmd_heading   <NA>            <heading [h1]>
#>  3 Setup   <NA>            rmd_chunk     setup           <chunk [r]>   
#>  4 Content <NA>            rmd_heading   <NA>            <heading [h1]>
#>  5 Content R Markdown      rmd_heading   <NA>            <heading [h2]>
#>  6 Content R Markdown      rmd_markdown  <NA>            <rmd_mrkd [6]>
#>  7 Content R Markdown      rmd_chunk     cars            <chunk [r]>   
#>  8 Content R Markdown      rmd_chunk     unnamed-chunk-1 <chunk [r]>   
#>  9 Content Including Plots rmd_heading   <NA>            <heading [h2]>
#> 10 Content Including Plots rmd_markdown  <NA>            <rmd_mrkd [2]>
#> 11 Content Including Plots rmd_chunk     pressure        <chunk [r]>   
#> 12 Content Including Plots rmd_markdown  <NA>            <rmd_mrkd [2]>

rmd_select(rmd, by_section("Content"))
#> └── Heading [h1] - Content
#>     ├── Heading [h2] - R Markdown
#>     │   ├── Markdown [6 lines]
#>     │   ├── Chunk [r, 1 lines] - cars
#>     │   └── Chunk [r, 1 lines] - unnamed-chunk-1
#>     └── Heading [h2] - Including Plots
#>         ├── Markdown [2 lines]
#>         ├── Chunk [r, 1 opt, 1 lines] - pressure
#>         └── Markdown [2 lines]

rmd_select(rmd, by_section(c("Content", "*"))) %>%
  rmd_select(has_type(c("rmd_chunk", "rmd_heading")))
#> └── Heading [h1] - Content
#>     ├── Heading [h2] - R Markdown
#>     │   ├── Chunk [r, 1 lines] - cars
#>     │   └── Chunk [r, 1 lines] - unnamed-chunk-1
#>     └── Heading [h2] - Including Plots
#>         └── Chunk [r, 1 opt, 1 lines] - pressure

rmd_select(rmd, "pressure")
#> └── Chunk [r, 1 opt, 1 lines] - pressure

rmd_select(rmd, 1:3)
#> ├── YAML [4 lines]
#> └── Heading [h1] - Setup
#>     └── Chunk [r, 1 opt, 1 lines] - setup

Copy Link

Version

Install

install.packages('parsermd')

Monthly Downloads

819

Version

0.1.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Colin Rundel

Last Published

January 16th, 2024

Functions in parsermd (0.1.3)

rmd_node

rmd node utility functions
rmd_source

Source the code chunks of an Rmd document
rmd_subset_util

rmd_subset utility functions
render

rmd_select_helpers

Rmd selection helper functions
rmd_select

Select nodes of an Rmd ast
rmd_subset

Subset the nodes of an rmd object
rmd_template

Create a template from an rmd object.
reexports

Objects exported from other packages
rmd_node_sections

Find the sections for each rmd object node
as_ast

Convert an object into an rmd_ast.
chunk_options

Get and set code chunk options
as_document

Convert an rmd_ast, rmd_tibble, or any ast node into text.
parse_rmd

Parse an R Markdown document
rmd_check_template

Check an Rmd against a template
parsermd-package

parsermd: Formal Parser and Related Tools for R Markdown Documents