Before you start, be sure to load the packages you need. I always do this in an initial ‘set up’ code chunk at the top of a page. You could also use the function library()
instead of the function require()
:
require(mosaic)
require(tidyverse)
At the end of today’s lab you should be able to do the following:
Demonstrate knowledge of how to interact with R using RStudio
Identify and employ the basic tools of R Markdown to understand R Notebooks
The goal of this lab is to introduce you to R and RStudio. Let us clarify which is which:
R is the name of the programming language itself, and
RStudio is a convenient interface allowing you to use R easily.
I encourage you to explore beyond what the labs dictate. A willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface, R markdown, and basic commands.
Figure 1: R interface
The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered. Any plots that you generate will show up in the panel in the lower right corner.
The panel on the left is where the magic happens.
It’s called the console. Every time you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running.
Below that information is the prompt.
As its name suggests, this prompt is really a request, a request for a command. Initially, interacting with R is all about typing commands and interpreting the output. These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.
To get you started, try the following commands at the R prompt (i.e. right after >
on the console).
556 + 232
72/4
= \(72 \div 4 = \frac{72}{4}\)sqrt(144)
= \(\sqrt{144}\)log(10)
= \(log_e 10 \neq log_{10}10\)You can either type the command in manually or copy and paste it from this document. So, as you can see, R is a good calculator. Later, we shall also see that R can be used to graph functions and create a variety of interesting graphics.
Later, I’ll talk to you about participation levels. To get a sense of what you can do with data and interactivity in R and R Markdown with HTML, take a look at this graph at FRED: research.stlouisfed.org/fred2/graph/?id=LNS11300001,LNS11300002,#
Here you can see a lovely dygraph
that can be generated using htmlwidgets (go to www.htmlwidgets.org to see a variety of other advanced options for what you can do when you become and R Champion). You saw this graph in my presentation, but I wanted also to show you that it can be produced in an html documen that would be online.
This document is based on an R Notebook or R Markdown document. R notebooks are meant to be like Lab Notebooks in the sciences where you “do (data) science’’ by:
R Notebooks are interactive documents. The notebooks can check the code while you write it, suggesting corrections where errors appear and displaying output when you run a code chunk. We shall see this in action several times during the workshop.
Practice what you’ve learned by creating a brief CV of your fictional alter ego in an R Markdown document.
First, open an R Markdown document:
---
lines (you’ll only change the title)Using the R Markdown quick reference (In R Studio navigate to Help -> Cheatsheets -> R Markdown Reference Guide), figure out how to ensure the following:
I’ll come around and check what people have done.
See an example here: simonhalliday.