Simple nomnoml in R examples

1 Load packages

library(tidyverse)  # data wrangling
library(nomnoml)  # graphs
library(magick)  # render SVG image

2 Motivation

Sketching diagrams such as flow charts is a useful thing. There exist a number of well-known (command line) engine for that purpose, such as

  • Mermaid
  • GraphViz, aka DOT
  • TikZ
  • nomnoml

3 Introducing Nomnoml

Nomnoml is quite new to the show.

I like it, because it is simple, modern, and I find it visually quite appealing.

4 R API

There’s a nice API for nomnoml in R.

Let’s define a valid nomnoml spec for a simple graph:

p1 <- "[x]->[y]"

And now plot it:

nomnoml(p1)

5 Adjust the size

Adjust the size of the plot

nomnoml(p1, width = 100, size = 100)

6 Change the direction

p2 <- 
"
#direction: right
[x]->[Y]
"
nomnoml(p2)
p2 <- 
"
#direction: down
[x]->[Y]
"
nomnoml(p2)

7 Size of the HTML container

Use the knitr/rmarkdown options to control the size of the output chunk.

out.width="50%" or similar options should do the trick.

8 Save to disk

As png:

nomnoml(p1, png = "p1.png")

As svg:

nomnoml(p1, png = "p1.svg")

9 Load from SVG

A SVG file can be downloaded from the nomnoml site with the included nicety that the source code is embedded.

For that purpose, we need some funcionality to render the SVG, for which purpose the R package magick is handy.

library(magick)

image_read(path = "nomnoml.svg")

10 Caveats

Some of the language appears not to be available via the R API.

In addition, the rendering quality of larger graphs detiorates apparently.

d <- 
"
[Workflow|
  [preprocessing|
    [Vorverarbeitung]
    [Imputation]
    [Transformation]
    [Prädiktorwahl]
    [Feature Engineering]
    [AV-Wahl]
    [...]
  ]
  [fitting |
    [Modell berechnen]
    [...]
  ]
  [postprocessing|
    [Grenzwerte für Klass. festlegen]
    [...]
  ]
]
"
nomnoml(d, height = 500)

However, some more clever reshaping of the graph may mitigate the problem.

d2 <- 
"
Workflow|
  [preprocessing|
   Vorverarbeitung;
   Imputation;
   Transformation;
   Prädiktorwahl
   AV-Wahl
   ...
  
  ]
  [fitting |
    Modell berechnen
    ...
  ]
  [postprocessing|
    Grenzwerte für Klass. festlegen
    ...
  ]
]

"
nomnoml(d2, height = 500)

11 Reproducibility

#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.3 (2022-03-10)
#>  os       macOS Big Sur/Monterey 10.16
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2022-04-02                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  package     * version date       lib source                            
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)                    
#>  backports     1.4.1   2021-12-13 [1] CRAN (R 4.1.0)                    
#>  blogdown      1.8     2022-02-16 [2] CRAN (R 4.1.2)                    
#>  bookdown      0.24.2  2021-10-15 [1] Github (rstudio/bookdown@ba51c26) 
#>  brio          1.1.3   2021-11-30 [1] CRAN (R 4.1.0)                    
#>  broom         0.7.12  2022-01-28 [1] CRAN (R 4.1.2)                    
#>  bslib         0.3.1   2021-10-06 [1] CRAN (R 4.1.0)                    
#>  cachem        1.0.6   2021-08-19 [1] CRAN (R 4.1.0)                    
#>  callr         3.7.0   2021-04-20 [1] CRAN (R 4.1.0)                    
#>  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.1.0)                    
#>  cli           3.2.0   2022-02-14 [1] CRAN (R 4.1.2)                    
#>  codetools     0.2-18  2020-11-04 [2] CRAN (R 4.1.3)                    
#>  colorout    * 1.2-2   2022-01-04 [1] Github (jalvesaq/colorout@79931fd)
#>  colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.1.2)                    
#>  crayon        1.5.0   2022-02-14 [1] CRAN (R 4.1.2)                    
#>  DBI           1.1.2   2021-12-20 [1] CRAN (R 4.1.0)                    
#>  dbplyr        2.1.1   2021-04-06 [1] CRAN (R 4.1.0)                    
#>  desc          1.4.0   2021-09-28 [1] CRAN (R 4.1.0)                    
#>  devtools      2.4.3   2021-11-30 [1] CRAN (R 4.1.0)                    
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.0)                    
#>  dplyr       * 1.0.8   2022-02-08 [1] CRAN (R 4.1.2)                    
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)                    
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)                    
#>  fansi         1.0.2   2022-01-14 [1] CRAN (R 4.1.2)                    
#>  fastmap       1.1.0   2021-01-25 [2] CRAN (R 4.1.0)                    
#>  forcats     * 0.5.1   2021-01-27 [1] CRAN (R 4.1.0)                    
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.0)                    
#>  generics      0.1.2   2022-01-31 [1] CRAN (R 4.1.2)                    
#>  ggplot2     * 3.3.5   2021-06-25 [2] CRAN (R 4.1.0)                    
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)                    
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.1.0)                    
#>  haven         2.4.3   2021-08-04 [1] CRAN (R 4.1.0)                    
#>  hms           1.1.1   2021-09-26 [1] CRAN (R 4.1.0)                    
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.0)                    
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)                    
#>  jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.1.0)                    
#>  jsonlite      1.7.3   2022-01-17 [1] CRAN (R 4.1.2)                    
#>  knitr         1.37    2021-12-16 [1] CRAN (R 4.1.0)                    
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.0)                    
#>  lubridate     1.8.0   2021-10-07 [1] CRAN (R 4.1.0)                    
#>  magrittr      2.0.2   2022-01-26 [1] CRAN (R 4.1.2)                    
#>  memoise       2.0.0   2021-01-26 [2] CRAN (R 4.1.0)                    
#>  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.1.0)                    
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)                    
#>  pillar        1.7.0   2022-02-01 [1] CRAN (R 4.1.2)                    
#>  pkgbuild      1.2.0   2020-12-15 [2] CRAN (R 4.1.0)                    
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)                    
#>  pkgload       1.2.4   2021-11-30 [1] CRAN (R 4.1.0)                    
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.1.0)                    
#>  processx      3.5.2   2021-04-30 [1] CRAN (R 4.1.0)                    
#>  ps            1.6.0   2021-02-28 [1] CRAN (R 4.1.0)                    
#>  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.1.0)                    
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.0)                    
#>  Rcpp          1.0.8   2022-01-13 [1] CRAN (R 4.1.2)                    
#>  readr       * 2.1.2   2022-01-30 [1] CRAN (R 4.1.2)                    
#>  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.1.0)                    
#>  remotes       2.4.0   2021-06-02 [2] CRAN (R 4.1.0)                    
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)                    
#>  rlang         1.0.2   2022-03-04 [1] CRAN (R 4.1.2)                    
#>  rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.0)                    
#>  rprojroot     2.0.2   2020-11-15 [2] CRAN (R 4.1.0)                    
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)                    
#>  rvest         1.0.2   2021-10-16 [1] CRAN (R 4.1.0)                    
#>  sass          0.4.0   2021-05-12 [1] CRAN (R 4.1.0)                    
#>  scales        1.1.1   2020-05-11 [1] CRAN (R 4.1.0)                    
#>  sessioninfo   1.1.1   2018-11-05 [2] CRAN (R 4.1.0)                    
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.0)                    
#>  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.1.0)                    
#>  testthat      3.1.2   2022-01-20 [1] CRAN (R 4.1.2)                    
#>  tibble      * 3.1.6   2021-11-07 [1] CRAN (R 4.1.0)                    
#>  tidyr       * 1.2.0   2022-02-01 [1] CRAN (R 4.1.2)                    
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.2)                    
#>  tidyverse   * 1.3.1   2021-04-15 [1] CRAN (R 4.1.0)                    
#>  tzdb          0.1.2   2021-07-20 [2] CRAN (R 4.1.0)                    
#>  usethis       2.0.1   2021-02-10 [2] CRAN (R 4.1.0)                    
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)                    
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)                    
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)                    
#>  xfun          0.29    2021-12-14 [1] CRAN (R 4.1.0)                    
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.1.0)                    
#>  yaml          2.2.2   2022-01-25 [1] CRAN (R 4.1.2)                    
#> 
#> [1] /Users/sebastiansaueruser/Library/R/x86_64/4.1/library
#> [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library