12 Sharing Your R Projects

After you have spent hours working on your R project it becomes time to emerge from your dark chamber and present your work to the world, i.e. boss/coworker/friend/people on the internet. At this point in time, it then becomes time to figure out in what form you want to present your work. Common forms include

  • Reports: These are formatted texts documenting maybe a data analysis possibly including intermediate steps, plots, tables and code.
  • Slides: As the name implies, you may put pieces of a data analysis into slides so that you can give a presentation to demonstrate your main findings.
  • Dashboards: Nowadays these are quite ubiquitous and can help to communicate a lot of data. For instance, the John Hopkins Covid-19 dashboard aggregates vast amounts of world-wide data on Covid-19 so that anyone who wishes to do so can explore the data interactively.
  • Blog entry: Possibly, you are into blogging and like to share your R project with everyone on the internet. Then, you may want to communicate your findings via a new blog entry. For example, Julia Silge’s blog regularly analyses data sets using tidymodels which can help you to learn more about this framework.
  • Books/lecture notes/thesis: Sometimes your R project comes down to writing a full-sized book, a thesis or in my particular case lecture notes.

Obviously, all of this can be done outside of R. Even if you have done your data analysis (or whatever it is you want to present) in R, this does not mean that you need to present your project using R. For example, I could have written these lecture notes in LaTeX and generated a PDF as is usual in mathematics. However, this would have been torture as it would involve having to somehow transfer the R code to be displayed to LaTeX and save all plots manually to include them in my LaTeX file.

Thankfully, we can let R handle the conversion to different formats and focus on the core of our project instead. Basically, for a lot of formats there is some R package that can handle the conversion and all we need to do is learn some new commands. One major advantage of this is that a simple R package can enable us to do stuff we would not be able to do otherwise.

This is particularly true when it comes down to publishing something on the internet like these lecture notes here. Basically, the internet is run by a few core programming languages such as HTML86 and CSS87 which I know embarrassingly little about. Luckily, the bookdown package does all the heavy-lifting for me and I only need to worry about the R code as the HTML and CSS files will be generated for me. These files I can then simply push to some server of my choice and access the generated web pages through the web.

So, you see that we do not have to leave our R universe to communicate our projects to the world. Therefore, we will spend this chapter to get an overview of how we can create reports, slides and simple dashboards including a few tweaking options. Afterwards, we will close this chapter by demonstrating how to get your documents to GitHub Pages88 so that you can share your stuff with the world.

12.1 Reports

The main package to generate reports is knitr which - among a lot of other useful features - handles the transition from our R Markdown (.Rmd) files to Markdown files (.md). These files and its correspoding markdown language are widely used, e.g, on GitHub and stackoverflow, and allow formatting code and text via a simple syntax.

Once we have a markdown file, it is possible to convert it into a myriad of formats via pandoc. In the end, we do not really need to worry about how these languages interplay. In RStudio, we can simply hit the Knit button to start the conversion. But what we need to know is how to use Markdown’s syntax to format our texts.

12.1.1 Markdown Highlighting

The Markdown syntax is extremely simple and it is just a matter of getting used to the abbreviations, so here is only a list of conventions Markdown uses.

Headlines of a chapter/section are indicated by # followed by a headline. Depending on how many # you use, your headline will get larger/smaller.

# headline    level 1 header - chapter
## headline   level 2 header - section
### headline  level 3 header - subsection

For code highlighting you may use the following:

  • *italic* appears as italic
  • **bold** appears as bold
  • `code` appears as code
  • exponent^2^ appears as exponent2
  • index~2~ appears as index2
  • footnote^[text here] appears as footnote89
  • [Link](https://www.youtube.com/watch?v=dQw4w9WgXcQ) appears as Link

Lists can be easily formatted via * or numbers 1., i.e.

* List entry 1

* List entry 2

  * Two spaces for indentation of bullets

1. List entry 1

1. List entry 2 (numbering works automatically)

will appear as

  • List entry 1

  • List entry 2

    • Two spaces for indentation of bullets
  1. List entry 1

  2. List entry 2 (numbering works automatically)

Finally, you can evaluate R expressions inline via `r variable` which will evaluate variable’s value and display that.

12.1.2 Tables and Images

For completeness’ sake let me mention that there are ways to include images and tables via Markdown syntax which is given by

![optional caption text](path/to/img.png)

Col1  | Col2
----- | -----
1     | 2
3     | 4

but personally I prefer knitr commands to do that for me.

knitr::include_graphics("Images/taylor-tree.png")

tbl <- tibble::tribble(
  ~col1, ~col2,
  1, 2,
  3, 4
) 

tbl %>% knitr::kable()

What is neat about kable() is that it allows us to do a lot of customization of our tables. For instance with the help of the kableExtra package we can apply different themes to our table.

library(kableExtra)
tbl %>% 
  knitr::kable() %>% 
  kable_classic(full_width = F)
col1 col2
1 2
3 4
tbl %>% 
  knitr::kable() %>% 
  kable_minimal(full_width = F)
col1 col2
1 2
3 4

Further, kable_styling() offers a lot more customization.

tbl %>% 
  knitr::kable() %>% 
  kable_styling(
    # See documentation for more options
    full_width = F, 
    position = "left",
    row_label_position = "r"
  )
col1 col2
1 2
3 4

We can also target specific columns of a table.

tbl %>% 
  knitr::kable() %>% 
  kable_styling(
    # See documentation for more options
    full_width = F, 
    position = "left",
    row_label_position = "r"
  ) %>% 
  column_spec(column = 2, bold = T) 
col1 col2
1 2
3 4

For even more things to tweak (and there is a lot of cool stuff that is possible to adjust) check out the vignette of kableExtra. Also, let me mention that there more R packages out there that can create tables for you, e.g. gt.

12.1.3 Chunk Options

Another thing that can help you to create your report are chunk options. These are helpful in a lot of scenarios. For instance, you could run some code in order to display the output, e.g. some plot, but not the code as this might not be of interest for the intended audience of your report.

In general, when you create an empty code chunk it may look like this

```{r}
```

but what it actually looks like when some default chunk options are specifically included is

```{r unnamed-chunk-xx, 
  eval = TRUE, 
  include = TRUE, 
  echo = TRUE, 
  message = TRUE, 
  warning = TRUE}
```

So, before you knit the document depending on your use case you can declare a chunk name (for possibly later reference) by changing unnamed-chunk-xx to something else and set any of those variables to FALSE where

  • eval controls whether the code is evaluated.
  • include controls whether the code and its output are displayed but the code is run in any case.
  • echo controls whether the code is displayed.
  • message controls whether messages (like the one displayed when running library(tidyverse)) are displayed.
  • warning controls whether warnings (like the one displayed when library(tidyverse) is run and some functions are masked) are displayed.

Therefore, code, output, messages and warnings are always displayed by default which is, of course, not always intended in a report. Further, there are way more chunk options that you can control like caching code chunks (in order to not run code chunks that take long to complete with each knit) or changing the width of a figure. An extensive list of options can be found online. Finally, you can set options globally if you want to apply something to every chunk via

knitr::opts_chunk$set(<option> = <value>)

12.1.4 Report Themes

Of course, you can set not only chunk options globally but also control the overall appearance of your document. This is usually done in the YAML90 header of your .Rmd-file. When you create a new notebook in RStudio, then your YAML header looks like this:

---
title: "R Notebook"
output: html_notebook
---

As you can see, the YAML header collects information on your output. Here, the output is a html_notebook which is basically the same as an html_document. Because .Rmd-files can potentially be converted into many formats, the YAML header can collect information on multiple output formats, e.g. we could change our YAML header as follows

---
title: "R Notebook"
output:
  prettydoc::html_pretty:
    theme: cayman
  html_document: default
  pdf_document: default
---

This header would now contain information on three outputs I could select from the Knit button’s dropdown menu. In this case, my options are to knit my .Rmd-file to a standard html- or pdf-file or use the prettydoc package to generate an html-file with theme cayman (more options are available in the package).

Similar to how we set the theme option for the prettydoc output, we could set other options for the standard html- or pdf-output. For example, the standard html document can use the code_folding: hide option so that the code chunks are collapsed by default, but can be extended via a click. Sadly, this option is not available for prettydoc or pdf_document.

In general, html is a very versatile format, so a lot of options might be possible that are not available for rigid formats like pdf. This is why it is possible to set options separately for each output.

Other straight-forward options like author name, data or table of content can be easily added to the YAML header. More options for the YAML header can be found here.

---
title: "R Notebook"
author: John Jacob Jingleheimer Schmidt
date: "2021-06-16"
output:
  prettydoc::html_pretty:
    theme: cayman
    toc: true
  html_document: 
    toc: true
  pdf_document: default
---

12.2 Flexdashboards

Flexdashboards from the flexdashboard package are comparatively simple dashboards compared to the complex dashboards you can create with the shiny package (which we will work with in the next chapter). To create a flexdashboard simply install the corresponding package and change the output in the YAML header to flexdashboard::flex_dashboard.

Then, you can easily fill pages, columns and rows of this dashboard with content. All you need to know for that is that the Markdown headers are used to indicate what page (#), column (##) or row (###) you are working on. As an example, simply take the following code and knit it. Also, feel free to tweak with the code to see how your changes affect the output.

#> ---
#> title: "Flexdashboard Example"
#> output: flexdashboard::flex_dashboard
#> ---
#> 
#> ```{r}
#> library(tidyverse)
#> knitr::opts_chunk$set(fig.width = 10)
#> ```
#> 
#> # First Page (Two-columned)
#> 
#> ## Iris data 
#> 
#> ### Data
#> 
#> ```{r, }
#> iris %>% 
#>   DT::datatable() # make it an interactive table with DT package
#> ```
#> 
#> ## Iris plot
#> 
#> ### Plot1
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Width, Petal.Length, col = Species))
#> ```
#> 
#> ### Plot2
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Length, Petal.Length, col = Species))
#> ```
#> 
#> ### Plot3
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Width, Sepal.Length, col = Species))
#> ```
#> 
#> # Second Page

Alternatively, you may use the alternative syntax that uses multiple - signs as column indicators and multiple = as page indicator. Thus, using the alternative syntax for the above dashboard the code will look like this.

#> ---
#> title: "Flexdashboard Example"
#> output: flexdashboard::flex_dashboard
#> ---
#> 
#> ```{r}
#> library(tidyverse)
#> knitr::opts_chunk$set(fig.width = 10)
#> ```
#> 
#> First Page (Two-columned)
#> =======================================
#> 
#> Iris data
#> ---------------------------------------
#> 
#> ### Data
#> 
#> ```{r, }
#> iris %>% 
#>   DT::datatable() # make it an interactive table with DT package
#> ```
#> 
#> Iris plot
#> ---------------------------------------
#> 
#> ### Plot1
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Width, Petal.Length, col = Species))
#> ```
#> 
#> ### Plot2
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Length, Petal.Length, col = Species))
#> ```
#> 
#> ### Plot3
#> 
#> ```{r}
#> ggplot(iris) +
#>   geom_point(aes(Sepal.Width, Sepal.Length, col = Species))
#> ```
#> 
#> Second Page 
#> =======================================

12.3 Slides

My package of choice for creating slides in R is the xaringan package which comes from the same creator as the knitr package. Apparently, the creator of this package is a huge Naruto fan which is why the package is names after the Sharingan from Naruto. Basically, the Sharingan enables one to use both the “eye of insight” and the “eye of hypnotism”. So, good luck hypnotizing your audience with your dazzling slides.

Again, using this new format is only a matter of adjusting the YAML header. Then, slides can be created via ---. Also, what is nice about these slides is that they are able to go into presentation mode (by opening the knitted html-file in your browser and pressing P) so that one can hide notes for a particular slide using ???.

An example presentation would look like this. Simply copy and paste the code into an .Rmd-file and knit it.

#> ---
#> title: "Example Slides"
#> subtitle: "With a super amazing subtitle"
#> output:
#>   xaringan::moon_reader:
#>     lib_dir: libs
#>     nature:
#>       highlightStyle: github
#>       highlightLines: true
#>       countIncrementalSlides: false
#> ---
#> 
#> ```{r, echo = F, message = F, warning = F, fig.align='center'}
#> library(tidyverse)
#> knitr::opts_chunk$set(fig.width = 10, fig.height = 5, fig.align = 'center')
#> ```
#> 
#> # Headline
#> ```{r}
#> iris %>% 
#>   ggplot(aes(Sepal.Length, Sepal.Width, col = Species)) +
#>   geom_point()
#> ```
#> 
#> ---
#> class:middle
#> 
#> ## Centering
#> This slide is now filled from the center due to `class:middle`
#> 
#> ???
#> 
#> * Using `???` I can hide notes.
#> * I can access these from my knitted html document by pressing `P` 
#> (i.e. going into presentation mode.) 
#> * Also I can clone the presentation by pressing `C`
#> 
#> ---
#> class:middle
#> 
#> ### Line highlighting via #<<
#> 
#> ```{r, eval = F}
#> mpg %>% 
#>   ggplot(aes(hwy, cty, col = class)) +
#>   geom_jitter() #<<
#> # Figure is shown on next slide! (due to --)
#> ```
#> 
#> --
#> 
#> ```{r, echo = F}
#> mpg %>% 
#>   ggplot(aes(hwy, cty, col = class)) +
#>   geom_jitter() #<<
#> ```
#> 
#> More information in the [original intro presentation](https://slides.yihui.org/xaringan/#1).

12.4 GitHub Pages

GitHub Pages is a simple but effective way to create websites from a GitHub repository without any knowledge about html, css and all else that is involved in the deployment of a website. Luckily, all that you need to know is how to set up GitHub Pages and then you can simply host your website from a GitHub repository.

Then, every time you want to change something on your website, you simply push changes into your repository and your website is built anew automatically. Also, what is great is that you can also simply add html-files that are created from knitting an Rmd-file to your repository and these automatically become accessible on your website. Thus, this is a simple way to share your reports, slides or whatever it is that you built with R.

Now, setting up a GitHub Pages site is as easy as creating a new repository. On GitHub, simply create a new public repository named <username>.github.io where <username> must correspond to your GitHub account name. Also, this repository should include a blank README file which will initialize the repository.

Once you have done that, a few seconds later, your GitHub Pages site becomes available online at <username>.github.io. If you take a look at this website, you will see that there is not much there yet. Now, you could fill your repository with all kinds of html- and css-files to bring your website to life. Alternatively, you can take the simple route by using an available theme.

In order to go with the latter option, simply go to your GitHub repository and follow Settings > Pages > Choose a theme. There, you can preview and select a theme that you fancy and then hit the Select theme button. As you will see, this also changes your README file whose changes you can commit in the next window that appears. Afterwards, you will see that your Github Pages site now looks the way it did when you were previewing themes.

Of course, now you can fill your repository with files from your R project. Here I have set up a dummy GitHub Page site that includes the dashboard and slide examples from the present chapter. As you can see from the corresponding repository I simply pushed a folder data to the repository that includes the content I want to host online.

All of that content is then accessible online via <username>.github.io/path/to/file. So, to access the flexdashboard from before, simply follow https://albertrapp.github.io/data/flexdashboard_example.html.

Of course, this gives you the opportunity to keep your README file to manage the landing page when someone follows <username>.github.io to your website. From there you can then put hyperlinks to your content in the data directory as I have done on the dummy site.

Finally, notice that a lot of knitted files are self-contained, so it is only a matter of uploading a single html-file. However, some files such as our xaringan example also include css-files for the styles of the file. These need to be uploaded as well which is why there is also a libs directory in the data directory of the dummy repository.

12.5 Exercises

12.5.1 Setting up a GitHub Pages Website

The next exercises are designed such that the created documents are to be hosted online. Thus, set up a GitHub Pages website. If you do not want to use your “real” GitHub account, simply create a dummy account for these exercises.

12.5.2 Create a Report

Reconstruct the report that is given here. Your reconstructed report should match the original as much as possible. Upload your report to a GitHub Pages website and leave a link to the report in the comments of your code in your repository. Note that the Rmd-file which created the report should be available in the exercise repository.

12.5.3 Create a Presentation

Create a fake presentation on whatever topic you fancy using R and the xaringan package. There are some minimal requirements for your slides though:

  • Number of Slides: 4 or more
  • Your slides must include a plot without displaying the code that created the plot
  • Should contain notes that are accessible via the presentation mode
  • One slide should contain multiple parts that are not displayed all at once but one by one
  • One slide should show the output from a tibble where two rows of said output are highlighted via a yellow background

Upload your slides to a GitHub Pages website and leave a link to the slides in the comments of your code in your repository. Note that the Rmd-file which created the presentation should be available in the exercise repository.