First R Session


Ways to Use R

Base R

When you install R you will see what is called base R.

R Console

The R console is the window on the left. You can type R code into this and then it runs and returns the output in the same window. You must be mindful because this does not save your work and does not provide any helpful tools.

R Script

The R Script is the window on the right. If you are used to Stata you can think of this as a do file or in SAS this is the code file that you run. In this manner we can save our work and comment it in R.

Like the do file and .sas file, none of the output is included in this and we must run it each time in order to display our results. Then we must copy our results into an external editor in order to use them.

RStudio

RStudio is what we will focus on using in this course. You will not only love it from the very beginning but as you advance in your skills you will not run out of reasons to love RStudio.

You have currently installed RStudio Desktop, however there is a server version available which allows for shared coding projects and a central R installation so that users do not need to manage things by themselves.

We will begin to explore the RStudio windows now.

Source Window

The source window which is highlighted in red is just like the script window in base R. However now instead of just a script you can create R Notebooks which are similar to many other programming and science related notebooks. You can also create R Markdown documents. Markdown is an easy to use language that can create html documents. We will learn how to create these later on in this course. A benefit of both R Notebooks and R Markdown documents is that they not only can display your code but they can have tables, graphs, figures, text and even are able to publish from these.

Environment Window

The environment window is where you can see a list of all available data and functions. It also allows for you to quickly see what the variables in your dataset are. On top of this the environment window also has tabs for:

History: Displays all previous code run in the console.

Spark: You can integrate RStudio with a Apache Spark installation to work with large data.

Git: You can use github for a version tracking of every project.

R Console

This is the exact same console that you see in Base R. This window will also display results from your R Markdown documents when you create html, pdf or other documents from them.

Files and Help Pane

The final pane in the RStudio window is the files pane. This can allow for easy access to your files. On top of this you can see previews of plots and documents that you are creating. This also has a built in help window. In base R when you search for help it takes you out of R and to a web-browser. In RStudio you can have the help right next to the code you are writing.

`swirl`

Getting Started with `swirl`:

1) Click <a href = “https://github.com/sullivanstatistics/swirl_courses/archive/master.zip”here</a> to download the file `swirl_courses-master.zip`.

2) Enter the following from the R console,  substituting the correct file path to your downloaded file and the name of your desired course:

“`
library(swirl)
install_course_zip(“path/to/file/here/swirl_courses-master.zip”, multi=TRUE)
swirl()
“`

For example, if you download the zip file to `~/Downloads/swirl_courses-master.zip`, then the following command will install the R Programming course.

“`
install_course_zip(“~/Downloads/swirl_courses-master.zip”, multi=TRUE)
“`

Please note that course names are case sensitive!

 

On Your Own: Swirl Practice

In order to learn R you must do R. Follow the steps below in your RStudio console:

1. Run this command to pick the course:

swirl()

You will be promted to choose a course. Type whatever number is in front of 01 Getting Started. This will then take you to a menu of lessons. For now we will just use lesson 1. Type 1 to choose Workspace and Files then follow all the instructions until you are finished.

Once you are finished with the lesson come back to this course and continue.