Techno Blender
Digitally Yours.

4 Quick and Easy Steps to Beautify R Markdown | by Jenna Eagleson | Jan, 2023

0 35


Photo by Greyson Joralemon on Unsplash

First of all, I love R Markdown. It is a wonderful, robust, incredibly amazing tool. It can also look bad, so bad that your beautiful hard work can be lost in ugly formatting, especially when sharing with less technical audiences. This is not meant as an introduction to R Markdown itself (some good places to start are here and here) but rather for those who have created work in R Markdown and want to take an extra few minutes to make it polished and presentable. You’ve already done the hard work, time to make it shine.

Here is a preview of where we are going, you can find the full code at the bottom of this article:

Preview of where the 4 steps will get you.
Image by author

In the style of practically every other R tutorial out there, I’ll be using the built-in iris dataset.

I don’t know what kind of cruel joke it was for package messages and the like to be shown by default but we put a stop to that today.

Showing difference when code chunk options are changed
Image by author.

You have the option of setting chunk options to be applied to the whole document, and you can specify options for a specific chunk.

To set the global options, create a code chunk like this:

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE)

If you want a different setting for a specific code chunk, you can do that in the first line of any chunk like this:

```{r message = TRUE}
# code that you want to run and then the message to display
```

There are tons of options and specifications you can experiment with, but just those two can make a big difference.

Yes, I know this is related to #2, and yes, I still think it is worth mentioning separately.

A thing that bugs me, is that how a table appears in .Rmd files is usually exactly how I would like them to appear in my html file. For example, I L-O-V-E love the way that long tables are cropped at 10 rows and then you are able to arrow forward to see additional rows. BUT when you knit your file, the whole shebang is unfolded and your neat report suddenly turns into a thousand pages of endless rows.

I want my reader to have the option to see additional rows of data but I don’t want to force that drudgery upon them.

Showing the difference when scrollbar is added to tables
Image by author.

And guess who is coming to the rescue, yet again? kableExtra (I promise you I’m not sponsored*). Enter scroll_box.

iris %>%
kableExtra::kable() %>%
kableExtra::kable_styling("striped") %>%
kableExtra::scroll_box(width = "100%", height = "400px")

As you can see, you can specify the width and height in pixels or in percentages. I like the width to go across the whole screen so I usually stick with 100% for width and 400 pixels shows around 9 rows along with the column titles which feels right to me.

Finally, the pièce de résistance. If you ignore everything else I say, this one tweak can take you from zero to hero in no seconds flat. Gracious geniuses have created CSS thematic wonders that immediately make reports look immensely more professional. You can find examples of canned themes here.

Showing the difference when using the cerulean theme
Image by author.

My personal favorite is cerulean. A slight tweak to your YAML adds a theme to your report:

title: Beautified RMarkdown
author: Jenna Eagleson
output:
html_document:
theme: cerulean

The last line of code makes all the difference in the world.

We are just scratching the surface here, people. As with all things R, the possibilities really are endless. Are there any R Markdown formatting tricks you love? Is there an example out there that pleases all of your aesthetic senses?

Jenna Eagleson
My background is in Industrial-Organizational Psychology and I have found my home in People Analytics. Data viz is what makes my work come to life. I have fun learning and developing with Power BI, R, Tableau, and other tools I come across. I would love to hear more about your journey! Reach me on
Linkedin or Twitter.

If you want more content like this, you can use my link to sign up for Medium for 5 buckaroos a month (I’ll get a small commission at no additional cost to you).

Full code:

---
title: "Beautified RMarkdown"
author: Jenna Eagleson
output:
html_document:
theme: cerulean
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE)
```

# Data from Iris

Look how much nicer I look!

```{r}
library(tidyverse)

iris <- iris

iris %>%
kableExtra::kable() %>%
kableExtra::kable_styling("striped") %>%
kableExtra::scroll_box(width = "100%", height = "400px")
```

*As of the time this article was published. I remain open to sponsorship opportunities (I’m not sure what that entails for an R package, but hey, let’s talk).




Photo by Greyson Joralemon on Unsplash

First of all, I love R Markdown. It is a wonderful, robust, incredibly amazing tool. It can also look bad, so bad that your beautiful hard work can be lost in ugly formatting, especially when sharing with less technical audiences. This is not meant as an introduction to R Markdown itself (some good places to start are here and here) but rather for those who have created work in R Markdown and want to take an extra few minutes to make it polished and presentable. You’ve already done the hard work, time to make it shine.

Here is a preview of where we are going, you can find the full code at the bottom of this article:

Preview of where the 4 steps will get you.
Image by author

In the style of practically every other R tutorial out there, I’ll be using the built-in iris dataset.

I don’t know what kind of cruel joke it was for package messages and the like to be shown by default but we put a stop to that today.

Showing difference when code chunk options are changed
Image by author.

You have the option of setting chunk options to be applied to the whole document, and you can specify options for a specific chunk.

To set the global options, create a code chunk like this:

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE)

If you want a different setting for a specific code chunk, you can do that in the first line of any chunk like this:

```{r message = TRUE}
# code that you want to run and then the message to display
```

There are tons of options and specifications you can experiment with, but just those two can make a big difference.

Yes, I know this is related to #2, and yes, I still think it is worth mentioning separately.

A thing that bugs me, is that how a table appears in .Rmd files is usually exactly how I would like them to appear in my html file. For example, I L-O-V-E love the way that long tables are cropped at 10 rows and then you are able to arrow forward to see additional rows. BUT when you knit your file, the whole shebang is unfolded and your neat report suddenly turns into a thousand pages of endless rows.

I want my reader to have the option to see additional rows of data but I don’t want to force that drudgery upon them.

Showing the difference when scrollbar is added to tables
Image by author.

And guess who is coming to the rescue, yet again? kableExtra (I promise you I’m not sponsored*). Enter scroll_box.

iris %>%
kableExtra::kable() %>%
kableExtra::kable_styling("striped") %>%
kableExtra::scroll_box(width = "100%", height = "400px")

As you can see, you can specify the width and height in pixels or in percentages. I like the width to go across the whole screen so I usually stick with 100% for width and 400 pixels shows around 9 rows along with the column titles which feels right to me.

Finally, the pièce de résistance. If you ignore everything else I say, this one tweak can take you from zero to hero in no seconds flat. Gracious geniuses have created CSS thematic wonders that immediately make reports look immensely more professional. You can find examples of canned themes here.

Showing the difference when using the cerulean theme
Image by author.

My personal favorite is cerulean. A slight tweak to your YAML adds a theme to your report:

title: Beautified RMarkdown
author: Jenna Eagleson
output:
html_document:
theme: cerulean

The last line of code makes all the difference in the world.

We are just scratching the surface here, people. As with all things R, the possibilities really are endless. Are there any R Markdown formatting tricks you love? Is there an example out there that pleases all of your aesthetic senses?

Jenna Eagleson
My background is in Industrial-Organizational Psychology and I have found my home in People Analytics. Data viz is what makes my work come to life. I have fun learning and developing with Power BI, R, Tableau, and other tools I come across. I would love to hear more about your journey! Reach me on
Linkedin or Twitter.

If you want more content like this, you can use my link to sign up for Medium for 5 buckaroos a month (I’ll get a small commission at no additional cost to you).

Full code:

---
title: "Beautified RMarkdown"
author: Jenna Eagleson
output:
html_document:
theme: cerulean
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE)
```

# Data from Iris

Look how much nicer I look!

```{r}
library(tidyverse)

iris <- iris

iris %>%
kableExtra::kable() %>%
kableExtra::kable_styling("striped") %>%
kableExtra::scroll_box(width = "100%", height = "400px")
```

*As of the time this article was published. I remain open to sponsorship opportunities (I’m not sure what that entails for an R package, but hey, let’s talk).

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment