I have based this workshop on examples for you to try yourself, because you won’t be able to learn how to program unless you try it out. I’ve picked example data that I hope will be interesting to Navy and Marine Corp public health researchers and practitioners. You can download the slides from the workshop by clicking here.
To try out these examples, you need some set-up:
This section will walk you through each step.
R is free and open-source software. You can download a copy for just about any operating system at the ]Comprehensive R Archive Network (CRAN)] (https://cran.r-project.org/). Look for the links on this page to “Download for …” and your operating system.1 If your computer has restrictions on what you can download, you may need to take to your system administrator or IT team to discuss whether you can download and install R and, if so, how. In that case, you may also need to talk with them about a strategy for installing R packages, as well.
You do not have to have RStudio2 In this section, I’m referring to RStudio, the software. It is produced by a company that is also called “RStudio”. RStudio, the company, includes a team of some of the best and most prolific R programmers currently contributing packages to extend R. A lot of the packages covered in this booklet were created and are maintained by members of the RStudio team. to run R, but it makes the experience much nicer. I also think it’s much easier to learn R when using RStudio to run it rather than using R by itself.
RStudio is an Integrated Development Environment (IDE)3 Integrated Development Environment (IDE). A program that provides a helpful interface for developing code, often geared to a specific programming language. It typically includes tools and a visual set-up for the programmer’s convenience. You should download it after you have downloaded R. You can download it here. You want the Desktop, open source edition. Download this and install it on your computer as you would any other software package from a website.
When you downloaded the R software from CRAN, you downloaded what’s called base R. This is the “engine” to run the heart of R, plus a few key extension packages. There are now several thousand packages available for R. These packages extend the core functionality of R, and we’ll be using a lot of these extra packages in this booklet.
When you need to use an R package that did not come with base R (most of them), you will first
need to install it to your computer. This can be done from R with the install.packages
function. This installs a package from an online repository to your computer, saving the code
in a special location that R can find. Since this function is downloading code from online,
your computer needs to have an internet connection when you run install.packages
, or you’ll
get an error.
This booklet uses a number of R packages beyond base R. To install all the packages that you’ll need, run the following code in your version of R:
install.packages(c("readr", "ggplot2", "forcats", "magrittr",
"dplyr", "lubridate", "sf", "tigris",
"DT", "plotly", "leaflet", "flexdashboard",
"tidyr", "stringr"))
I’ve created a repository on GitHub4 GitHub. An online platform for directories tracked with
the version control software git
. It has become a very popular place for coders to post code
from their projects, especially for open-source projects like R packages. You may need to get
a GitHub account to be able to clone this example directory. Even if not, it’s not a bad idea to
get an account if you work a lot with data and open-source code. You can find this example
repository by clicking here. On the
page takes you to, click on the “Clone or download” button and then select “Download ZIP”.
This will download a single zipped file to your computer. When you unzip the file, it will be a special type of directory, an R Project directory. To open the R Project and start on the examples, open RStudio, then go to “File” -> “Open Project”. A pop-up window will open to let you navigate through your files and find an R Project to open. Navigate to the directory you downloaded, which should be called “navy_public_health_examples” and doubleclick on the file in this directory called “navy_public_health_examples.Rproj”.
This will open the project. In the “Files” pane of RStudio, you should see some subdirectories for “R” and “data”. These have the example R code and data, respectively, for you to try the examples in this booklet. The code in each of the R files should run independently, including the code to load all required packages. Figure 1.1 shows what this package should look like once you’ve downloaded and opened it, as well as opened the “plot.R” file in the project’s “R” subdirectory.
Click on the Next button (or navigate using the links at the top of the page) to continue.