MyNixOS website logo
Description

From 'Rmarkdown' and 'Quarto' Files to Tibble and Back.

Split your 'rmarkdown' or 'quarto' files by sections into a tibble: titles, text, chunks. Rebuild the file from the tibble.

lightparser

R-CMD-check Codecov testcoverage

You need to extract some specific information from your Rmd or Qmd file? {lightparser} is designed to split your Rmd or Qmd file by sections into a tibble: titles, text, chunks. It stores them as a tibble, so you can easily manipulate it with {dplyr} or {purrr}. Later, you can rebuild a Rmd or Qmd from the tibble.

Installation

You can install the development version of lightparser from GitHub with:

# install.packages("devtools")
devtools::install_github("ThinkR-open/lightparser")

Documentation

Full documentation website on: https://thinkr-open.github.io/lightparser/

Example

Split your Rmd or Qmd file into a tibble:

library(lightparser)

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


tbl_rmd <- split_to_tbl(file)
tbl_rmd
#> # A tibble: 35 × 8
#>    type    label       params       text     code  heading heading_level section
#>    <chr>   <chr>       <list>       <named > <lis> <chr>           <dbl> <chr>  
#>  1 yaml    <NA>        <named list> <lgl>    <lgl> <NA>               NA <NA>   
#>  2 inline  <NA>        <lgl [1]>    <chr>    <lgl> <NA>               NA <NA>   
#>  3 block   development <named list> <lgl>    <chr> <NA>               NA <NA>   
#>  4 inline  <NA>        <lgl [1]>    <chr>    <lgl> <NA>               NA <NA>   
#>  5 heading <NA>        <lgl [1]>    <chr>    <lgl> Descri…             1 Descri…
#>  6 inline  <NA>        <lgl [1]>    <chr>    <lgl> <NA>               NA Descri…
#>  7 block   description <named list> <lgl>    <chr> <NA>               NA Descri…
#>  8 inline  <NA>        <lgl [1]>    <chr>    <lgl> <NA>               NA Descri…
#>  9 heading <NA>        <lgl [1]>    <chr>    <lgl> Read d…             1 Read d…
#> 10 inline  <NA>        <lgl [1]>    <chr>    <lgl> <NA>               NA Read d…
#> # ℹ 25 more rows

Combine the tibble into a Rmd or Qmd file:

file_out <- tempfile(fileext = ".Rmd")
out <- combine_tbl_to_file(tbl_rmd, file_out)

Read the file re-created with combine_tbl_to_file() to verify it is a proper Rmd

cat(readLines(file_out), sep = "\n")
#> ---
#> title: dev_history.Rmd for working package
#> output: html_document
#> author: statnmap
#> date: '2023-10-12'
#> editor_options:
#>   chunk_output_type: console
#> ---
#> 
#> 
#> ```{r development}
#> #| include: no
#> 
#> library(testthat)
#> ```
#> 
#> 
#> <!--
#> # Description of your package
#> 
#> This will fill the description of your package.
#> -->
#> ```{r description}
#> # --> for parse tests
#> fusen::fill_description(
#>     pkg = here::here(),
#>     fields = list(
#>         Title = "Build A Package From Rmarkdown file",
#>         Description = "Use Rmarkdown First method to build your package. Start your package with documentation. Everything can be set from a Rmarkdown file in your project.",
#>         `Authors@R` = c(
#>             person("John", "Doe", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0000-0000-0000"))
#>         )
#>     )
#> )
#> # Define License with use_*_license()
#> usethis::use_mit_license("John Doe")
#> ```
#> 
#> 
#> # Read data
#> 
#> <!-- Store your dataset in a directory named "inst/" at the root of your project -->
#> <!-- Use it for your tests in this Rmd thanks to `load_all()` to make it available
#> and `system.file()` to read it in your examples 
#> -->
#> ```{r development-2}
#> # Set error=TRUE for checks() if needed
#> # Run all in the console directly
#> # Create "inst/" directory
#> dir.create(here::here("inst"))
#> # Example dataset
#> file.copy(system.file("nyc_squirrels_sample.csv", package = "fusen"), here::here("inst"))
#> ```
#> 
#> 
#> # Calculate the median of a vector
#> 
#> Here is some inline R code : `r 1+1`
#> ```{r function}
#> #' My median
#> #'
#> #' @param x Vector of Numeric values
#> #' @inheritParams stats::median
#> #'
#> #' @return
#> #' Median of vector x
#> #' @export
#> #'
#> #' @examples
#> #' my_median(2:20)
#> my_median <- function(x, na.rm = TRUE) {
#>     if (!is.numeric(x)) {
#>         stop("x should be numeric")
#>     }
#>     stats::median(x, na.rm = na.rm)
#> }
#> ```
#> 
#> 
#> ```{r examples}
#> my_median(1:12)
#> ```
#> 
#> 
#> ```{r tests}
#> test_that("my_median works properly and show error if needed", {
#>     expect_error(my_median("text"))
#> })
#> ```
#> 
#> 
#> # Calculate the mean of a vector
#> ## Use sub-functions in the same chunk
#> ```{r function-1}
#> #| filename: the_median_file
#> 
#> #' My Other median
#> #'
#> #' @param x Vector of Numeric values
#> #' @inheritParams stats::median
#> #'
#> #' @return
#> #' Median of vector x
#> #' @export
#> #'
#> #' @examples
#> my_other_median <- function(x, na.rm = TRUE) {
#>     if (!is.numeric(x)) {
#>         stop("x should be numeric")
#>     }
#>     sub_median(x, na.rm = na.rm)
#> }
#> 
#> #' Core of the median not exported
#> #' @param x Vector of Numeric values
#> #' @inheritParams stats::median
#> sub_median <- function(x, na.rm = TRUE) {
#>     stats::median(x, na.rm)
#> }
#> ```
#> 
#> 
#> ```{r examples-1}
#> my_other_median(1:12)
#> my_other_median(8:20)
#> my_other_median(20:50)
#> ```
#> 
#> 
#> ```{r tests-1}
#> test_that("my_median works properly and show error if needed", {
#>     expect_true(my_other_median(1:12) == 6.5)
#>     expect_error(my_other_median("text"))
#> })
#> ```
#> 
#> 
#> ```
#> not a R chunk that should be kept in vignette, but not in code
#> ```
#> 
#> ```{r development-1}
#> #| eval: no
#> 
#> # Run but keep eval=FALSE to avoid infinite loop
#> # Execute in the console directly
#> fusen::inflate(flat_file = "dev/dev_history.Rmd")
#> ```
#> 
#> 
#> 
#> # Inflate your package
#> ```{r}
#> # duplicate empty name
#> ```
#> 
#> 
#> ```{r}
#> # duplicate empty name
#> ```
#> 
#> 
#> 
#> You're one inflate from paper to box.
#> Build your package from this very Rmd using `fusen::inflate()`
#> 
#> - Verify your `"DESCRIPTION"` file has been updated
#> - Verify your function is in `"R/"` directory
#> - Verify your test is in `"tests/testthat/"` directory
#> - Verify this Rmd appears in `"vignettes/"` directory

Similar work

{lightparser} is a light version of {parsermd} that has not been updated for a long time, and which is not compatible with the latest versions of C++ compilers. {lightparser} does not rely on C++ compilation.

Code of Conduct

Please note that the lightparser project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • 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-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