If I want to nicely print my sessionInfo in R for a PDF, I can just use
toLatex(sessionInfo())
It seems like there should be a similar option for rmarkdown to render in HTML, but I can't find it here or on Rdocumentation. Before re-inventing the wheel, thought I'd ask if the equivalent of
toMarkdown(sessionInfo())
already exists.
To create vertical space (Markdown to PDF), I use This command works like \vspace{12pt} for latex. Fantastic! This works beautifully.
To create an R Markdown report, open a plain text file and save it with the extension . Rmd.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
Try pander
, which is a general method to do the R->markdown conversion:
> pander(sessionInfo())
**R version 3.2.1 (2015-06-18)**
**Platform:** x86_64-unknown-linux-gnu (64-bit)
**locale:**
_LC_CTYPE=hu_HU.utf8_, _LC_NUMERIC=C_, _LC_TIME=hu_HU.utf8_, _LC_COLLATE=C_, _LC_MONETARY=hu_HU.utf8_, _LC_MESSAGES=hu_HU.utf8_, _LC_PAPER=hu_HU.utf8_, _LC_NAME=C_, _LC_ADDRESS=C_, _LC_TELEPHONE=C_, _LC_MEASUREMENT=hu_HU.utf8_ and _LC_IDENTIFICATION=C_
**attached base packages:**
_stats_, _graphics_, _grDevices_, _utils_, _datasets_, _methods_ and _base_
**other attached packages:**
pander(v.0.5.3)
**loaded via a namespace (and not attached):**
_tools(v.3.2.1)_, _Rcpp(v.0.12.1)_ and _digest(v.0.6.8)_
Or in the long form:
> pander(sessionInfo(), compact = FALSE)
**R version 3.2.1 (2015-06-18)**
**Platform:** x86_64-unknown-linux-gnu (64-bit)
**locale:**
_LC_CTYPE=hu_HU.utf8_, _LC_NUMERIC=C_, _LC_TIME=hu_HU.utf8_, _LC_COLLATE=C_, _LC_MONETARY=hu_HU.utf8_, _LC_MESSAGES=hu_HU.utf8_, _LC_PAPER=hu_HU.utf8_, _LC_NAME=C_, _LC_ADDRESS=C_, _LC_TELEPHONE=C_, _LC_MEASUREMENT=hu_HU.utf8_ and _LC_IDENTIFICATION=C_
**attached base packages:**
* stats
* graphics
* grDevices
* utils
* datasets
* methods
* base
**other attached packages:**
* pander(v.0.5.3)
**loaded via a namespace (and not attached):**
* tools(v.3.2.1)
* Rcpp(v.0.12.1)
* digest(v.0.6.8)
Resulting in the following HTML:
Another solution making use of devtools::session_info()
which can be copied straight into an R markdown file (Rmd):
<!-- TABLE TITLE: -->
(ref:Reproducibility-SessionInfo-R-environment-title) R environment session info for reproducibility of results
\renewcommand{\arraystretch}{0.8} <!-- decrease line spacing for the table -->
```{r Reproducibility-SessionInfo-R-environment, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
library("devtools")
# library("knitr")
df_session_platform <- devtools::session_info()$platform %>%
unlist(.) %>%
as.data.frame(.) %>%
rownames_to_column(.)
colnames(df_session_platform) <- c("Setting", "Value")
kable(
df_session_platform,
booktabs = T,
align = "l",
caption = "(ref:Reproducibility-SessionInfo-R-environment-title)", # complete caption for main document
caption.short = " " # "(ref:Reproducibility-SessionInfo-R-environment-caption)" # short caption for LoT
) %>%
kable_styling(full_width = F,
latex_options = c(
"hold_position" # stop table floating
)
)
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
<!-- TABLE TITLE: -->
(ref:Reproducibility-SessionInfo-R-packages-title) Package info for reproducibility of results
\renewcommand{\arraystretch}{0.6} <!-- decrease line spacing for the table -->
```{r Reproducibility-SessionInfo-R-packages, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
df_session_packages <- devtools::session_info()$packages %>%
as.data.frame(.) %>%
filter(attached == TRUE) %>%
dplyr::select(loadedversion, date) %>%
rownames_to_column
colnames(df_session_packages) <- c("Package", "Loaded version", "Date")
kable(
df_session_packages,
booktabs = T,
align = "l",
caption = "(ref:Reproducibility-SessionInfo-R-packages-title)", # complete caption for main document
caption.short = " " # "(ref:Reproducibility-SessionInfo-R-packages-caption)" # short caption for LoT
) %>%
kable_styling(full_width = F,
latex_options = c(
"hold_position" # stop table floating
)
)
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
credits go to @user2554330 and @RobertoScotti for their snippets to split tables provided to this SO Q.
```{r prepare multicolumn-table, echo=FALSE, message=FALSE, warning=FALSE}
colnames(df_session_packages) <- c("Package", "Loaded version", "Date")
rows <- seq_len(nrow(df_session_packages) %/% 2)
l_session_packages <- list(df_session_packages[rows,1:3],
matrix(numeric(), nrow=0, ncol=1),
df_session_packages[-rows, 1:3])
```
Generate the table: for few packages vertically crunched on a portrait page
\renewcommand{\arraystretch}{0.8} <!-- decrease line spacing for the table -->
```{r Reproducibility-SessionInfo-R-packages, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
kable(
l_session_packages,
row.names = T,
booktabs = T,
align = "l",
caption = table_caption, # complete caption for main document
caption.short = table_caption_title # short caption for LoT
)
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
Generate the table: for very many packages on a landscape page
\begin{landscape}
```{r Reproducibility-SessionInfo-R-packages-lanscape, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
kable(
l_session_packages,
row.names = T,
booktabs = T,
align = "l",
caption = table_caption, # complete caption for main document
caption.short = table_caption_title # short caption for LoT
)
```
\end{landscape}
Generate the table: for very many packages on a landscape page
\begin{landscape}
```{r Reproducibility-SessionInfo-R-MANY-packages-lanscape, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
# SOURCE: Roberto Scotti (StackOverflow)
# https://stackoverflow.com/questions/56445149/how-can-i-split-a-table-so-that-it-appears-side-by-side-in-r-markdown
split.print <- function(x, cols = 2, sects = 1, spaces = 1, caption = "", label = ""){
if (cols < 1) stop("cols must be GT 1!")
if (sects < 1) stop("sects must be GT 1!")
rims <- nrow(x) %% sects
nris <- (rep(nrow(x) %/% sects, sects) + c(rep(1, rims), rep(0, sects-rims))) %>%
cumsum() %>%
c(0, .)
for(s in 1:sects){
xs <- x[(nris[s]+1):nris[s+1], ]
rimc <- nrow(xs) %% cols
nric <- (rep(nrow(xs) %/% cols, cols) + c(rep(1, rimc), rep(0, cols-rimc))) %>%
cumsum() %>%
c(0, .)
lst <- NULL
spc <- NULL
for(sp in 1:spaces) spc <- c(spc, list(matrix(numeric(), nrow=0, ncol=1)))
for(c in 1:cols){
lst <- c(lst, list(xs[(nric[c]+1):nric[c+1], ]))
if (cols > 1 & c < cols) lst <- c(lst, spc)
}
kable(lst,
row.names = T,
caption = ifelse(sects == 1, caption, paste0(caption, " (", s, "/", sects, ")")),
#label = "tables", format = "latex",
booktabs = TRUE) %>%
kable_styling(latex_options = "hold_position") %>%
print()
}
}
df_session_packages %>%
dplyr::select(1:3) %>%
split.print(cols = 2, sects = 1, caption = "multi page table")
```
\end{landscape}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With