9  Getting started with R

This chapter helps get you acclimated to the screens of RStudio and the basic structure of R commands. You will always work in RStudio (the car), not R (the engine).

9.1 Getting to know the RStudio screen

Open the application called RStudio rstudio,
NOT R not r.

This is what your screen probably looks like:

console

The Console

The Console is where you can type commands and interact directly with the programming language. Think of it as a very powerful calculator at first. One reason to use it is to install packages.

If you followed the installation demo, you’ve already used the console to install one package using the command install.packages("tidyverse"). Go back and do that part now if you skipped it. It might take several minutes to finish.

Files tab

We won’t be using many of the tabs in the lower right, but the Files tab can help you if you’re having trouble navigating your work. Under the More button, you can choose “Go to working directory”, since that’s where R thinks you’ve parked all of your work. This can be confusing in R, which is why we’ll be working in “projects” that bundle up all of your work in one place. (I don’t find the Help sections in this area very helpful. Instead, I usually rely on written manuals elsehwere, such as tidyverse official site. )

Environment

The upper right screen is the Environment, which is where your active objects live. An object, sometimes called a “variable” in R, is a named thing. It might be a word, a list of words or numbers, or a data frame (spreadsheet). It can even be a little program called a function that you write yourself.

Anything that you want to use has to be loaded or created into that environment first. That might involve importing data from Excel or a text file, or creating data yourself in a program.

Typing into the console

When you type this: 5+5 after the > prompt and press return, you’ll see: [1] 10

When you type this: "Sarah" , you’ll get this back: [1] "Sarah"

To create a new variable, you’ll use the assignment operator <- (two characters : A less than sign and a hyphen). Here is how I would create the variable called my_name (lower case, no spaces). Notice how it appears in the Environment after being created. Then I can print it by typing the name of the variable instead of the letters of my name in quotes:

Pro tip

Use the keyboard shortcut to create the <- assignment operator .

The console remembers your commands, but you have to type them one at a time and you can’t save them for later, or start over without entering them all again. That’s why we’re going to work in programs called Quarto documents most of the time.1

9.2 Take a tour

Watch Andrew Heiss’s tour of RStudio.

We will not be using the R scripts he shows in the end of the video, but many other examples and resources do. Also, as we get into working with Quarto, the “code chunks” work the same way as a script.

9.3 Unlocking packages and the tidyverse

The real power of R comes with packages. Packages are bundles of programs that others have found useful to extend the base R language. R is almost useless to a normal person without them. There are more than 10,000 packages available for R, each doing a special job.

If you followed along with the tutorial in the last chapter, the final step was to install a package called the tidyverse. Almost everything we do from now on depends on that.

The tidyverse is a system of packages, or libraries, that work together with similar grammar and syntax. It’s particularly useful for the kind of work reporters do – importing, cleaning and analyzing data that we get from others. For many reporters (myself included), R was too difficult to learn before the tidyverse came around. We’ll be working almost exclusively within the tidyverse in this course.

Searching for help

When you search for help, be sure to put “tidyverse” somewhere in your query. If you don’t , you’re likely to get inscrutible answers.

perplexity.ai and ChatGPT are two artificial intelligence sites that can help you with your code. These AI sites are particularly good at troubleshooting code – describe your data, copy and paste your code, and ask it what’s wrong! I tend to use the Perplexity site. Some of the methods shown in this book are newer than the ChatGPT cutoff date.2

Either one is usually better than just Googling. If you do use Google, be sure to set a date cutoff – R has changed a LOT in the last five years.

Installing packages for this course

We need to update your packages and install a few more for our work.

If this is the first time you’ve installed it, the tidyverse will take a while to install, and it will look like it’s just stopped. Be patient. It can take 5 minutes or more.

You only have to install packages once on each machine you use. Later on, you’ll learn how to invoke them one by one.

Option 1: Use the console

Copy these commands into your Console. This list of packages contains all of the packages used in this book – I think! Don’t worry about what they all mean – we’ll use them later on.

  • If it asks you if you want to restart, say “Yes”.
  • If it asks you if you want to build from source, say “No”.
update.packages(ask=FALSE)
install.packages(c("tidyverse", "janitor" , 
                "lubridate", "quarto", "swirl", "pacman", 
                "DT", "reactable", "gt", 
                "leaflet", "sf", "tmap", 
                "gapminder", "plotly", 
                "gtsummary", "tigris"
                ))

Option 2: Use RStudio “Packages” tab

Under the Packages tab in the lower right panel:

  • Start by updating all of the packages you might already have installed by pressing the “Update” button.
  • Under the Install tab, search for the packages listed in the install.packages() code above.

packages

Optional hands-on tutorial:

One of the packages you just installed is called swirl, which has some interactive tutorials. The first one might be useful.

Make sure your cursor is in the Console and type swirl::swirl().

Select the first chapter called R Programming: The basics of programming in R, and just the first section, called Basic Building Blocks.

9.4 Relax!

Figure 9.1: Relax by Silwia Bartyzel via Unsplash

You’re all set up and we’re ready to start programming. Congratulate yourself - everything is new, nothing is intuitive and the screen is intimidating. You’ve come a long way.


  1. These used to be “R Markdown”. Quarto is the newer implementaiton of it, but you’ll often see R Markdown referenced in other places.↩︎

  2. If you have a Github account, you might be able to use Github Copilot in RStudio. I’ve found it to be more annoying than helpful.↩︎