Site icon R-bloggers

Reproducible Reports with RStudio

[This article was first published on Reimagined Invention, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Tired of R code not working on the fly even between your own computers? Think in a reproducible way.

I’ve been thinking about the best way to create a reproducible report with rmarkdown and how to share that with people that don’t even have R installed.

Rmd format allows you to export to html, pdf or docx. That’s cool but there are some details to consider.

Create lightweight html documents

If you decide to export your report to html you can use prettydoc and compose using knit. To do this install the package prettydoc.

install.packages("prettydoc")

and then instead of composing with the option output: html_document you can do, for example, this

output:
  prettydoc::html_pretty:
    theme: hpstr
    highlight: github

Exporting to docx or pdf

If you want to create a pdf or docx just write output: pdf_document or output: word_document.

Think in a reproducible way

Suppose you want to share your report. Sending the html, pdf or docx is ok but probably your colleagues do not have the same installed packages as you. Then you could use a function to automatically download and/or load any package like prettydoc.

This function works ok and makes your rmds working on almost any computer with a recent R version

download_and_or_load <- function(x){
  y <- x[!(x %in% installed.packages()[, "Package"])]
  if (length(y))
    install.packages(y, dependencies = TRUE)
  sapply(x, require, character.only = TRUE)
}

To use it write

my_libraries <- c("ggplot2", "dplyr", "purrr" ... + any additional package ...)
download_and_or_load(my_libraries)

Be sure to include it at the beginning of your document. A good practise would be to include a chunk just after the global options. It may help to use the options echo = TRUE, message = FALSE, eval = FALSE for that chunk.

download_and_or_load <- function(x){
  y <- x[!(x %in% installed.packages()[, "Package"])]
  if (length(y))
    install.packages(y, dependencies = TRUE)
  sapply(x, require, character.only = TRUE)
}

my_libraries <- c("ggplot2", "dplyr", "purrr", ... + any additional package ...)
download_and_or_load(my_libraries)

Additional considerations

If you write, for example, in spanish be sure to replace the tildes to make your file compatible between different systems (e.g. sending a file written in Mac to a Windows user)

Here you can find the proper replacements for many languages: HTML encoding of foreign language characters

To leave a comment for the author, please follow the link and comment on their blog: Reimagined Invention.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.