Reporting with Data in R

Author

Christian McDonald & Jo Lukito

Published

January 21, 2024

About this book

Note

This work is under constant revision. It is written using Quarto and the source is available on Github.

This version has been revised for spring 2024.

Reporting with Data in R is a series of lessons and instructions used for courses in the School of Journalism and Media, Moody College of Communication at the University of Texas at Austin. The first two versions of the book were written by Associate Professor of Practice Christian McDonald. Assistant Professor Jo Lukito began collaborating and teaching sections of the Reporting with Data in spring 2022. Anastasia Goodwin taught a section in fall 2023 and provided some valuable edits to this work.

This means the writing voice or point of view may change or be confusing at times. For instance, Prof. McDonald uses a Mac and Prof. Lukito uses a PC, so keyboard commands and screenshot examples may flip back and forth. We’ll try to note the author and platform at the beginning of each chapter.

Some words from Prof. McDonald

I’m a strong proponent of what I call Scripted Journalism, a method of committing data journalism that is programmatic, repeatable, transparent and annotated. There are a myriad of programming languages that further this, including Python (pandas using Jupyter) and JavaScript (Observable), but we’ll be using R, Quarto and RStudio.

R is a super powerful, open-source programming language for data that is deep with features and an awesome community of users who build upon it. No matter the challenge before you in your data storytelling, there is probably a package available to help you solve that challenge. Probably more than one.

There is always more than one way to do things in R. This book is a Tidyverse-oriented, opinionated collection of lessons intended to teach students new to programming and R for the expressed act of committing journalism. As a beginner course, we strive to make it as simple as possible, which means we may not go into detail about alternative (and possibly better) ways to accomplish tasks in favor of staying in the Tidyverse and reducing options to simplify understanding. We rarely discuss differences from base R; Tidyverse is our default.

Programming languages evolve constantly, and R has seen some significant changes in the past few years, many of them driven by Posit, the company that makes Rstudio and maintains the Tidyverse packages.

  • The introduction of Quarto in mid-2022. This modern implementation of RMarkdown is a major shift for this edition of the book. Every lesson and video will need to be updated, and we may not get to all of them. The good news is RMarkdown still works inside Quarto documents.

  • The introduction of the base R pipe |> in 2021. Posit developers began using the |> in favor of the magrittr pipe %>% in 2022, and this book follows their lead. The two implementations work interchangeably and you’ll see plenty of %>% in the wild.

  • The use of YAML code chunk options. I first noticed this style when I started using Quarto in 2023. It might not matter much for this book as we don’t use that many code chunk options in our assignments, but we’ll see.

Conventions and styles in this book

We will try to be consistent in the way we write documentation and lessons. But we are human, so sometimes we break our own rules, but in general keep the following in mind.

Things to do

Things to DO are in ordered lists:

  1. Do this thing.
  2. Then do this thing.

Explanations are usually in text, like this very paragraph.

Sometimes details will be explained in lists:

  • This is the first thing I want you to know.
  • This is the second. You don’t have to DO these things, just know about them.

Code blocks

This book often runs the code that is shown, so you ’ll see the code and the result of that code below it.

1 + 1
[1] 2

Copying code blocks

When you see R code in the instructions, you can roll your cursor over the right-corner and click on the copy icon to copy the code clock content to your clipboard.

You can then paste the code inside your R chunk.

That said, typing code yourself has many, many benefits. You learn better when you type yourself, make mistakes and have to fix them. We encourage you to always type short code snippets. Leave the copying to long ones.

Hidden code

Sometimes we want to include code in the book but not display it so you can try the to write the code yourself first. When we do this, it will look like this:

Click here to show the code
1 + 1
[1] 2

If you click on the triangle or the words that follow, you’ll reveal the code. Click again to hide it.

Annotated code

Sometimes when we are explaining code it is helpful to match lines of code to the explanation about them, which we do through annotated code.

1mtcars |>
2  head()
1
First we take the Motor Trend Car Road Tests data set AND THEN …
2
We pipe into the head() command, which gives us the “top” of the data.

When there are annotations like this you have to be careful if you are copying code from the book. Either copy it one line at a time or use the copy icon noted above.

Fenced code

Sometimes we need to show code chunk options that are added, like when explaining how to name chunks. In those cases, you may see the code chunk with all the tick marks, etc. like this:

```{r block-named}
1 + 1
```
[1] 2

or

```{r}
#| label: block-named-yaml

1 + 1
```
[1] 2

You can still copy/paste these blocks, but you’ll get the entire code block, not just the contents.

Notes, some important

We will use callouts to set off a less important aside:

Markdown was developed by JOHN GRUBER, as outlined on his Daring Fireball blog.

But sometimes those asides are important. We usually indicate that:

Important

You really should learn how to use Markdown as you will use it the whole semester, and hopefully for the rest of your life.

About the authors

Christian McDonald

I’m a career journalist who most recently served as data and projects editor at the Austin American-Statesman before joining the University of Texas at Austin faculty full-time in fall 2018. I’ve taught data-related course at UT since 2013. I also serve as the innovation director of the Dallas Morning News Journalism Innovation Endowment.

Jo Lukito

I’m an aspiring-journalist-turned-academic who studies journalism and digital media. To make a long story short (tl;dr): I trained to be a journalist as an undergraduate student, but just fell in love with researching and supporting journalism. I completed my Ph.D in 2020, and my dissertation focused on international trade reporting (which relies on plenty o’ data). I also do a ton of social media research (especially in politics and disinformation), so if you’re interested in the social media beat, I’m your gal!

License

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Let’s just say this is free on the internet. We don’t make any money from it and you shouldn’t either.

Other resources

This text stands upon the shoulders of giants and by design does not cover all aspects of using R. Here are some other useful books, tutorials and sites dedicated to R. There are other task-specific tutorials and articles sprinkled throughout the book in the Resources section of select chapters.