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:
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.
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!
─ 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-06-13
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 objectpkg_sesh<-session_info(pkgs ="attached")# get the quarto versionquarto_version<-system("quarto --version", intern =TRUE)# inject the quarto infopkg_sesh$platform$quarto<-paste(system("quarto --version", intern =TRUE),
"@",
quarto::quarto_path())# print it outpkg_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-06-13
pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
quarto 0.9.577 @ /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 objectpkg_sesh <-session_info(pkgs ="attached")# get the quarto versionquarto_version <-system("quarto --version", intern =TRUE)# inject the quarto infopkg_sesh$platform$quarto <-paste(system("quarto --version", intern =TRUE), "@", quarto::quarto_path() )# print it outpkg_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-06-13
pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
quarto 0.9.577 @ /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 objectpkg_sesh <-session_info(pkgs ="attached")# get the quarto versionquarto_version <-system("quarto --version", intern =TRUE)# inject the quarto infopkg_sesh$platform$quarto <-paste(system("quarto --version", intern =TRUE), "@", quarto::quarto_path() )# print it outpkg_sesh```:::
Which will return a colored callout like below:
Expand for 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-06-13
pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
quarto 0.9.577 @ /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: