Chapter 21 Translating R code

Learning objectives:

  • Build DSL (domain specific languages) to aid interoperability between R, HTML and LaTeX
  • Reinforce metaprogramming concepts (expressions, quasiquotation, evaluation)
expressions
R
quasiquotation
evaluation
HTML
LaTeX
Mermaid code
DiagrammeR::mermaid("
graph LR

expressions --> R
quasiquotation --> R
evaluation --> R

R --> HTML
R --> LaTeX
")
Session Info
library(DiagrammeR) #for Mermaid flowchart
library(lobstr)     #abstract syntax trees
library(purrr)      #functional programming
library(rlang)      #tidy evaluation

# from section 18.5
expr_type <- function(x) {
  if (rlang::is_syntactic_literal(x)) {
    "constant"
  } else if (is.symbol(x)) {
    "symbol"
  } else if (is.call(x)) {
    "call"
  } else if (is.pairlist(x)) {
    "pairlist"
  } else {
    typeof(x)
  }
}
flat_map_chr <- function(.x, .f, ...) {
  purrr::flatten_chr(purrr::map(.x, .f, ...))
}
switch_expr <- function(x, ...) {
  switch(expr_type(x),
    ...,
    stop("Don't know how to handle type ", typeof(x), call. = FALSE)
  )
}
utils::sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] R6_2.5.1             ids_1.0.1            memoise_2.0.1       
#>  [4] patchwork_1.3.0      scales_1.3.0         rlang_1.1.4         
#>  [7] reshape2_1.4.4       deSolve_1.40         lubridate_1.9.3     
#> [10] forcats_1.0.0        stringr_1.5.1        purrr_1.0.2         
#> [13] readr_2.1.5          tidyr_1.3.1          tibble_3.2.1        
#> [16] tidyverse_2.0.0      ggplot2_3.5.1        palmerpenguins_0.1.1
#> [19] gt_0.11.1            dplyr_1.1.4          lobstr_1.1.2        
#> [22] DiagrammeR_1.0.11   
#> 
#> loaded via a namespace (and not attached):
#>  [1] emoji_16.0.0       gtable_0.3.6       xfun_0.49          bslib_0.8.0       
#>  [5] htmlwidgets_1.6.4  visNetwork_2.1.2   bench_1.1.3        tzdb_0.4.0        
#>  [9] vctrs_0.6.5        tools_4.4.2        generics_0.1.3     stats4_4.4.2      
#> [13] curl_6.0.0         parallel_4.4.2     fansi_1.0.6        pkgconfig_2.0.3   
#> [17] RColorBrewer_1.1-3 uuid_1.2-1         lifecycle_1.0.4    compiler_4.4.2    
#> [21] farver_2.1.2       textshaping_0.4.0  munsell_0.5.1      codetools_0.2-20  
#> [25] htmltools_0.5.8.1  sass_0.4.9         yaml_2.3.10        profmem_0.6.0     
#> [29] pillar_1.9.0       crayon_1.5.3       jquerylib_0.1.4    cachem_1.1.0      
#> [33] commonmark_1.9.2   tidyselect_1.2.1   digest_0.6.37      stringi_1.8.4     
#> [37] bookdown_0.41      labeling_0.4.3     fastmap_1.2.0      grid_4.4.2        
#> [41] colorspace_2.1-1   cli_3.6.3          magrittr_2.0.3     utf8_1.2.4        
#> [45] withr_3.0.2        prettyunits_1.2.0  sloop_1.0.1        bit64_4.5.2       
#> [49] timechange_0.3.0   rmarkdown_2.29     bit_4.5.0          ragg_1.3.3        
#> [53] hms_1.1.3          evaluate_1.0.1     knitr_1.49         viridisLite_0.4.2 
#> [57] markdown_1.13      Rcpp_1.0.13-1      glue_1.8.0         xml2_1.3.6        
#> [61] rstudioapi_0.17.1  vroom_1.6.5        jsonlite_1.8.9     plyr_1.8.9        
#> [65] systemfonts_1.1.0