Adding session info to blog posts

meta
quarto

A snapshot of your code state

Author

Thomas Mock

Published

April 18, 2022

Adding session info

Session info in R has lots of useful information on the state of your working environment at the moment you were coding. While tools such as renv or packrat can help you create project-specific libraries, sessionInfo() at least lets you capture the basic environment details. I’m not wanting to create project-specific libraries, but I’m rather wanting to share a nicely formatted output of my environment metadata.

Put another way, renv can be useful for someone (including yourself) to revert back to the specific package environment, whereas the session info simply returns the basic environment info. For a blogpost, I don’t imagine others trying to match to exact package environments, but rather confirm that their package environment is “similar enough”.

We can create and capture the session details like so:

base_sesh <- sessionInfo()

base_sesh
R version 4.2.0 (2022-04-22)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] emo_0.0.0.9000    lubridate_1.8.0   here_1.0.1        digest_0.6.29    
 [5] crayon_1.5.1      rprojroot_2.0.3   assertthat_0.2.1  jsonlite_1.8.0   
 [9] magrittr_2.0.2    evaluate_0.15     rlang_1.0.2       stringi_1.7.6    
[13] cli_3.3.0         rstudioapi_0.13   generics_0.1.2    rmarkdown_2.14   
[17] rsthemes_0.3.1    tools_4.2.0       stringr_1.4.0     htmlwidgets_1.5.4
[21] glue_1.6.2        purrr_0.3.4       yaml_2.3.5        xfun_0.30        
[25] fastmap_1.1.0     compiler_4.2.0    htmltools_0.5.2   knitr_1.38.3     

Since sessionInfo() generates a list, we can explore it like a list or even inject new objects into it.

base_sesh |> 
  str(max.level = 1)
List of 12
 $ R.version      :List of 14
 $ platform       : chr "aarch64-apple-darwin20 (64-bit)"
 $ locale         : chr "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
 $ running        : chr "macOS Monterey 12.2.1"
 $ RNGkind        : chr [1:3] "Mersenne-Twister" "Inversion" "Rejection"
 $ basePkgs       : chr [1:7] "stats" "graphics" "grDevices" "utils" ...
 $ loadedOnly     :List of 28
 $ matprod        : chr "default"
 $ BLAS           : chr "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib"
 $ LAPACK         : chr "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib"
 $ system.codepage: chr "NULL"
 $ codepage       : chr "NULL"
 - attr(*, "class")= chr "sessionInfo"

For example, I can inject my current version of the quarto CLI into the session info.

base_sesh$quarto <- system("quarto -V", intern = TRUE)

base_sesh |>
  str(max.level = 1)
List of 13
 $ R.version      :List of 14
 $ platform       : chr "aarch64-apple-darwin20 (64-bit)"
 $ locale         : chr "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
 $ running        : chr "macOS Monterey 12.2.1"
 $ RNGkind        : chr [1:3] "Mersenne-Twister" "Inversion" "Rejection"
 $ basePkgs       : chr [1:7] "stats" "graphics" "grDevices" "utils" ...
 $ loadedOnly     :List of 28
 $ matprod        : chr "default"
 $ BLAS           : chr "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib"
 $ LAPACK         : chr "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib"
 $ system.codepage: chr "NULL"
 $ codepage       : chr "NULL"
 $ quarto         : chr "0.9.387"
 - attr(*, "class")= chr "sessionInfo"

We’ve now captured a lot of useful session info and even injected a new item, so that we can remember what version of the Quarto CLI I was using.

sessioninfo package

An alternative to using utils::sessionInfo() would be to use the sessioninfo package. Per the package docs:

Query and print information about the current R session. It is similar to utils::sessionInfo(), but includes more information about packages, and where they were installed from.

Differences from utils::sessionInfo()

  • Additional platform details: time zone, pandoc version, RStudio version, etc.
  • Information about package sources, e.g. GitHub repo and hash for packages installed from GitHub.
  • Highlight package installation problems, e.g. if the loaded and on-disk versions are different, if the MD5 checksum of the package DLL is wrong, etc.
  • Highlight packages from unusual sources.
  • Information about external software via external_info().
  • Information about the Python configuration is the reticulate package is loaded and configured.
  • Information about package libraries.
  • Compare two session info outputs with the session_diff() function.
  • Option to show loaded (default), attached or installed packages, or the recursive dependencies of the specified packages.
Note

Note that I’m really only interested in the session info and which packages were attached (ie via library(pkgname)), so I’ll use the pkgs = "attached" argument. This flexibility is nice!

library(sessioninfo)
session_info(pkgs = "attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.2.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-05-11
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────

Now while information such as this is very useful for attaching to things like a reprex, it can also be useful to include for testing purposes or for “posterity” when rendering a document or writing a blogpost.

Adding to sessioninfo

Let’s also note one more thing - again I’d like to record the Quarto version, since I’m relying on quarto for rendering my blog. We can “inject” the quarto version and path to the sessioninfo list object.

I did go ahead and submit a feature request on the sessioninfo repo - maybe quarto info will be incorporated into sessioninfo in the near future!
# save the session info as an object
pkg_sesh <- session_info(pkgs = "attached")

# get the quarto version
quarto_version <- system("quarto --version", intern = TRUE)

# inject the quarto info
pkg_sesh$platform$quarto <- paste(
  system("quarto --version", intern = TRUE), 
  "@", 
  quarto::quarto_path()
  )

# print it out
pkg_sesh
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.2.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-05-11
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
 quarto   0.9.387 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────

That’s looking good!

Include in docs

Now to add it to the end of our document, we can use a quick HTML <details> tag. This creates a small expandable section. We can also change the title of with the use of a <summary> tag. We can add this to all of our documents/blogposts and capture

<details><summary>Session Info</summary>

```{r, echo = FALSE}
library(sessioninfo)
# save the session info as an object
pkg_sesh <- session_info(pkgs = "attached")

# get the quarto version
quarto_version <- system("quarto --version", intern = TRUE)

# inject the quarto info
pkg_sesh$platform$quarto <- paste(
  system("quarto --version", intern = TRUE), 
  "@", 
  quarto::quarto_path()
  )

# print it out
pkg_sesh
```

</details>

Which generates something like the below (again note that you need to expand this <details> tag by clicking on it):


Session Info
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.2.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-05-11
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
 quarto   0.9.387 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────

Quarto-native callouts

Quarto itself adds the ability to create boostrap-style callouts and collapsible callouts, so we can simplify our code a bit:

:::{.callout-tip collapse="true"}
## Expand for Session Info
```{r, echo = FALSE}
library(sessioninfo)
# save the session info as an object
pkg_sesh <- session_info(pkgs = "attached")

# get the quarto version
quarto_version <- system("quarto --version", intern = TRUE)

# inject the quarto info
pkg_sesh$platform$quarto <- paste(
  system("quarto --version", intern = TRUE), 
  "@", 
  quarto::quarto_path()
  )

# print it out
pkg_sesh
```

:::

Which will return a colored callout like below:

─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.2.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-05-11
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
 quarto   0.9.387 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────

Closing Notes

If you want to see some examples of similar concepts in action on other blogs, check out:

TJ also uses sessioninfo::session_info() while Danielle takes it a step further and generates a full renv lockfile.

─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.2.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-05-11
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
 quarto   0.9.387 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────