Back to Intro Slides

Agenda

We will learn about the following topics in this section.

Installation

R (interpreter) can be downloaded from CRAN.

RStudio (IDE) can be downloaded from here.

At the RStudio website, there are many useful cheat sheets. The following are particularly relevant for this bootcamp.

RStudio Panes

If your RStudio has the default configuration, you are seeing 3 or 4 panes.

Without Source

       | Environment etc
Cosole | Files etc

4 panes

Source | Environment etc
Cosole | Files etc

The Source pane is where you write code into documents. Multiple documents can be shown with tabs.

The Console pane is where the R interpreter runs. You can also type here for quick interaction that is not meant to be recorded in the code. Notice the current working directory that is shown at the top.

The Environment pane shows the named objects that are present in the environment. This can include datasets, model results, functions you defined, etc.

The Files pane shows a file viewer, package manager, plots, etc.

In code we trust

Choose RStudio/Preferences… to bring up the configuration screen. The following are recommended.

The philosophy here is that we do not create or use intermediate workspace of half-baked data. We only trust the original dataset, intermediate datasets that we explicitly saved, and most importantly code.

Working directory

Understanding the current working directory is important. This is the folder your R interpreter is directly looking at. You can see it at the top of the Console pane, or inquire it by the get workind directory function.

getwd()
## [1] "/Users/kazuki/Documents/HSPH/HSPH_ScD/_TA/SummerProgram2017/2017Rbootcamp"

You can change the working directory using the set working directory function or the Files panes.

setwd("~/Documents/OtherFolder")

Project management

When working on a specific research or other analysis project, it’s a good idea to keep all related files under a project folder. This bootcamp is no exception. Let’s create a project folder say 2017Rbootcamp. Choose File/New Project… in the menu. You can create a new folder for a project or set up a new project in an existing folder.

Types of documents RStudio can manage

RStudio can manage different types of documents. The most important ones are.

A very long involved code is likely better handled as a plain text R script. If you a creating a documented analysis report, RMarkdown is very powerful. In fact, tis very document you are looking at is written in RMarkdown. Please create several different types of documents to see what they are like. In RMarkdown, check out inserting code chunks and knitting (find above the Source pane) into an html file.

Programming support

Both the script editor (Source pane) and the console (Console pane) have “completion” functionality. You don’t have to type or remember functions in full. Just hit the TAB key. It gives you candidates and some short help.

Pressing F1 key on an object name gives you a help page.

TAB and F1, you should remember these keys!

Installing packages

The base R environment is relatively small. The real power is given by external packages that are available on the CRAN repository. When looking for a package for a specific task xyz Google for “r xyz” or “cran xyz”. If you have a more general idea about what you want to do, the CRAN Task Views may be helpful.

Package installation can be done in the interpreter as follows. Or you can use the Packages tab in the Files pane.

install.packages("tidyverse", dependencies = TRUE)

Loading packages

Loading a package is done with library() command. You can also check the check box in the Packages tab in the Files Pane.

library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats

Once a package is loaded you can examine what’s inside using the Environment Pane’s package:package_name. Another way is to hit TAB after typing the following either in the Source Pane or Console Pane.

For public objects that are meant for user interaction. Notice the two colons.

tibble::

For internal (private) objects that are meant for software development. Notice the three colons.

tibble:::

Learning about packages

Google “cran package_name”. Look for vignettes that provide an overview.

Where to find more resources

Some additional notable resources are: