Part 0 of the coRps R Building Blocks Series (RBBS). All content can be found on this blog under the rbbs category as well as on the USAID-OHA-SI/coRps GitHub repo.

RBBS 0 - Software and Account Setup

In order to make the most of this training series, participants should have a number of account setup and software pre-configured so you can actively participate during the sessions.

RStudio Cloud

If you are newer to R or don’t have RStudio installed currently, we recommend using an RStudio Cloud Account. To do so, you can access RStudio Cloud here to get setup. You can set this up with your email (work or personal) or link this with your GitHub account (see below following slide for details on GitHub). This is a great option if you’re getting started. It is free so the catch is there are a limited number of processing hours (25) you can use it for in a month.

R and RStudio

If you don’t use RStudio Cloud, above, you will need to install R and RStudio.

a. You can install R on your USAID GFE, by going to Start > Microsoft Endpoint manager > Software Center. Once there, you can select the Application called “R for Windows” and click “Install”. If working on a personal machine, you can install R from CRAN. Select “Download R for Windows” and then “base” and follow the instructions for installing that pop up when you launch the .exe file from your downloads. b. To install RStudio on your GFE, you should submit a M/CIO Help Desk ticket and they will install it for you in person. If submitting, please (1) ask M/CIO to add RStudio back to Software Center and (2) also submit a ticket for GitHub Desktop, below. If working on a personal machine, you can install RStudio from this site. Select “Download” for the free version and follow the instructions for installing that pop up when you launch the .exe file from your downloads.

RStudio/RStudio Cloud Global Options

It’s best practice to start with a clean R session each time you load up RStudio, so you will want to adjust some default option in your RStudio. To access these, in the menu bar at the top, navigate to Tools > Global Options. Here are the places you will want to make changes to the default options:

  • Uncheck “Result most recent opened project at startup”
  • Unckeck “Restore .RData into Workspace at startup”
  • Change dropdown to “Never” for “Save workspace to .RData on exit”
  • Uncheck “Always save history (even when not saving .RData)

RStudio Global Options

GitHub

GitHub is a version control system that is free and used to host code. All of our code are stored there (just not the data) under our organization, USAID-OHA-SI. You can use your work or personal email to setup an account. When you have done so, you can send us an email to get added to the organization. We will spend a session on how to use GitHub. We also recommend submitting a Help Desk Ticket with M/CIO to install GitHub Desktop (approved software).

Starter Packages from CRAN

With proprietary programs like Excel, Tableau, or Stata, all of your functions are built into the software. With open-source packages on the other hand, we need to load different libraries or packages that are written by other organization or individual users. To install a package from CRAN, we use the command install.packages(). Copy and paste the code chunk into your console in R to install all the required packages. We will discuss what packages/libraries and functions are in our next session.

install.packages("tidyverse")
install.packages("gt")
install.packages("scales")
install.packages("patchwork")
install.packages("remotes")

Starter Packages from GitHub

We also need to install packages from other locations, like GitHub, and will use a slightly different command, remotes::install_github(). You will need to enter the following command into your console.

remotes::install_github("USAID-OHA-SI/gophr", build_vignettes = TRUE)
remotes::install_github("USAID-OHA-SI/glitr", build_vignettes = TRUE)
remotes::install_github("USAID-OHA-SI/glamr", build_vignettes = TRUE)
remotes::install_github("USAID-OHA-SI/gisr", build_vignettes = TRUE)

Setting up Rtools

Some packages require some additional “building” and need something called RTools installed. You can set this up yourself by following the steps laid out here. You will need to download R tools from CRAN and override the default to install it to your Documents folder. Modify the code chunk below replacing USER_NAME with your local username, then run the code chunk below from within an R-studio session.

# Find path for RTools
Sys.setenv(PATH = paste("C:\\Users\\USER_NAME\\Documents\\Rtools\\bin", Sys.getenv("PATH"), sep = ";"))
Sys.setenv(BINPREF = "C:\\Users\\USER_NAME\\Documents\\Rtools\\mingw_$(WIN)/bin/")
options(buildtools.check = NULL)
# Check
install.packages("pkgbuild") # pkgbuild is not available (for R version 3.5.0)
install.packages("remotes") # make sure you have the latest version from CRAN
remotes::install_github("r-lib/pkgbuild") # install updated version of pkgbuild from GitHub
pkgbuild::find_rtools()

Custom Fonts

To create standard visualizations across our team, we rely on one of USAID’s alternate fonts, Sans Source Pro. This font is not only not native to R, nor is it a standard to Windows. Sans Source Pro is an open source font available from Google Fonts. To install it on your computer, naviagate to this link and click the “Download family”. After the folder finishes downloading, unzip it. To use non-naitive fonts in R, you must run a program called extrafonts. You will need to run the following code below to install all the fonts on your computer (if desired) and the ones you just downloaded. You will only need to import fonts only once. However, to use these fonts with any plotting in R, you will need to load the extrafont as with any other package.

# Required library
library(extrafont) ## install.packages("extrafont") if not installed

#import fonts for the first time
#import all Windows fonts
  font_import()
  
#restart your R session - CTRL + SHIFT + F10
library(extrafont)

#import Source Sans Pro from download
  folderpath <- "~/Downloads/Source_Sans_Pro"
  font_import(folderpath)
  
#restart your R session - CTRL + SHIFT + F10

library(extrafont)