MyNixOS website logo
Description

Formal Parser and Related Tools for R Markdown Documents.

An implementation of a formal grammar and parser for R Markdown documents using the Boost Spirit X3 library. It also includes a collection of high level functions for working with the resulting abstract syntax tree.

parsermd parsermd logo

CRANstatus R-CMD-check

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("examples/minimal.Rmd", package = "parsermd")))
#> ├── YAML [4 fields]
#> ├── Heading [h1] - Setup
#> │   └── Chunk [r, 1 line] - setup
#> └── Heading [h1] - Content
#>     ├── Heading [h2] - R Markdown
#>     │   ├── Markdown [5 lines]
#>     │   ├── Chunk [r, 1 line] - cars
#>     │   └── Chunk [r, 1 line] - unnamed-chunk-1
#>     └── Heading [h2] - Including Plots
#>         ├── Markdown [1 line]
#>         ├── Chunk [r, 1 line] - pressure
#>         └── Markdown [2 lines]

as_tibble(rmd)
#> # A tibble: 12 × 5
#>    sec_h1  sec_h2          type         label           ast           
#>    <chr>   <chr>           <chr>        <chr>           <list>        
#>  1 <NA>    <NA>            rmd_yaml     <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>            <markdown>    
#>  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>            <markdown>    
#> 11 Content Including Plots rmd_chunk    pressure        <chunk [r]>   
#> 12 Content Including Plots rmd_markdown <NA>            <markdown>

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

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

rmd_select(rmd, "pressure")
#> ├── YAML [4 fields]
#> └── Chunk [r, 1 line] - pressure

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

Version

0.2.0

License

Unknown

Platforms (76)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows