GitHub - ThinkR-open/lightparser: From Rmd And Qmd To Tibble And Back (original) (raw)
lightparser
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 released CRAN version:
install.packages("lightparser")
You can install the development version of lightparser fromGitHub 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
#>
#> 1 yaml NA
#> 2 inline <lgl [1]> NA
#> 3 block development NA
#> 4 inline <lgl [1]> NA
#> 5 heading <lgl [1]> Descri… 1 Descri…
#> 6 inline <lgl [1]> NA Descri…
#> 7 block description NA Descri…
#> 8 inline <lgl [1]> NA Descri…
#> 9 heading <lgl [1]> Read d… 1 Read d…
#> 10 inline <lgl [1]> 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) #>
#>
#>
#>
#> {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 = "john@email.me", role = c("aut", "cre"), comment = c(ORCID = "0000-0000-0000-0000")) #> ) #> ) #> ) #> # Define License with use_*_license() #> usethis::use_mit_license("John Doe") #>
#>
#>
#> # Read data
#>
#>
#>
#> {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.