The goal this week is to introduce R and RStudio which will be used throughout the course both to review the statistical concepts discussed in the course and to analyze real data and come to informed conclusions. To clarify which is which: R is the name of the programming language itself, and RStudio is a convenient interface.

Today, we begin with the fundamental building blocks of R and RStudio: the interface, creating and saving files, and basic commands.

1 Opening RStudio Server

Open Appalachian’s RStudio Server and sign in: RStudio Server

Your credentials are the same as for your email.

Please DO NOT choose Stay signed in.

2 The RStudio Interface

In RStudio Server, you should see a window that looks like the image in Figure 2.1.

The RStudio interface

Figure 2.1: The RStudio interface

The panel on the left is where the action happens. It’s called the console. Every time RStudio is launched, it will have the same text at the top of the console describing the version of R that is running.

The panel in the upper right contains the workspace. This shows the variables and objects defined during an R session and a history of the commands that are entered.

Any plots that are generated will show up in the panel in the lower right corner. This is also where you can browse your files, access help files, and upload and download files.

3 Using R Markdown Files

3.1 Opening a New File

In R, we use a document type called an R Markdown document. R Markdown documents are useful for both running code and annotating the code with comments. The document can be saved, so you can refer back to your code later and can be used to create other document types (html, word, pdf, or slides) for presenting the results of your analyses. R Markdown provides a way to generate clear and reproducible statistical analyses.

To open a new file, click on the little green plus on the upper left hand and select R Markdown; see Figure 3.1. You can leave it untitled.

Starting a new R Markdown file

Figure 3.1: Starting a new R Markdown file

When you open a new R Markdown file, there is some example code in it that you can delete. We will take care of this next.

3.2 Make Changes to a File

Let’s make some changes to the R Markdown file you just opened. Using the image below as a guide,

  • First, change the title of the lab at the top to “Getting to Know RStudio.” Be sure to keep the quotation marks.
  • Second, add an author line, following the example below. You need quotation marks!
  • Third, delete everything in the document after line 6.
  • Fourth, add headers and text, exactly following the example below.
  • Finally, insert what is called a “code chunk.” To do this, you click on the insert button near the top center of the screen, then choose R. The greyed out box that shows up is where you type code.

Your final result should look like this:

3.3 Saving a File

Lab work is committed and pushed as an R Markdown file like this each week, so it is important to learn how to save these files.

  • Click File > Save As…
  • Name the file: PS01_lastname_firstname (fill in your firstname and lastname)
  • Click save

This is now saved in the MD-PS01-SC folder on the server.

3.4 Knitting a File

Click the Knit button at the top left side of the screen to “knit” the file, or in other words, produce an output document. An .html file will be generated. It is automatically saved in the same folder as your R Markdown file.

Note that there is now a R Markdown file (.Rmd) and an html file (.html) in the MD-PS01-SC folder.

Inspect the .html file to see how what you typed was formatted. There are lots of tricks for controlling the formatting of the knitted html file. For instance:

  • putting ## and a space in front of text makes it into a large header. For example, see how ## This is a header in your R Markdown .Rmd file translates in the resulting .html output.
  • putting ### and a space in front of text makes it a smaller header.

4 Entering and Running Commands

The code chunks are where you put R code in an R Markdown file. So far, your “knitted” file (your output document file) doesn’t show anything, because we did not put any content in the code chunks yet.

Using your first code chunk, type the following command to create a new variable called x with the value of 6.

x <- 6

The arrow <- is called an ASSIGNMENT OPERATOR and tells R to save an object called x that has the value of 6. This is similar to saving a value in a graphing calculator.

Note that whatever you want to save must always be to the left of the assignment operator.

To actually RUN this command in your console, you have a few options:

  • click on the green triangle in the code chunk
  • highlight the code and hit Control-Enter on a PC or Command-Return on a Mac

Think of “running” code in your console as telling R “do this.”

Note that you now have a new object in your workspace, called x.

5 Data Types\(-\)a Brief Intro

So far, you have made a numeric variable x. There many other types of data objects you can make in R.

First, copy, paste, and run the following command in a new code chunk to make a character called favorite_movie. Think of characters as text as opposed to numerical values. Note that R knows this is a character because there are quotation marks around Star_Wars.

favorite_movie <- "Star_Wars"

Next, copy, paste, and run the following command in a new code chunk.

v <- c(2, 4, 6)

This makes what is called a vector, which we have named v. It is a data object that has multiple elements of the same type. This vector contains three numbers, 2, 4, and 6. The c() function says tells R to concatenate the values 2, 4, 6, into a single vector. Note in the Environment pane that your vector v contains numbers (listed as num).

You can do math on a vector that contains numbers. For instance, copy, paste, and run the following command in a new code chunk. This tells R to multiply each element of the vector v by 3.

v * 3

6 Practice on Your Own

Type complete sentences to answer all questions inside the answer tags provided in the R Markdown document. Round all numeric answers you report inside the answer tags to four decimal places. Use inline R code to report numeric answers inside the answer tags (i.e. do not hard code your numeric answers).

Remember to save your work as you go along. Click the save button in the upper left hand corner of the R Markdown window.

  1. Answer the following with code in a code chunk (no text necessary). Remember that the code is just instructions for R. You need to run the code chunk to make R execute those instructions.
    • Create a variable called y with the value of 7
    • Multiply x by y, and store the answer in a variable named z like so: z <- x * y
    • You should now see favorite_movie, x, v, y, and z all in your Environment pane
# Type your code and comments inside the code chunk
y <- 7;
z <- x * y;
    • Run the following mathematical operation in a code chunk: 6 + 3
    • Where does the answer appear? (please answer with text)
# Type your code and comments inside the code chunk
6 + 3;
[1] 9

Answer appears in a line labled [1] under the code chunk that ends on line 190 and before the line of code on on line 191.

    • Now add a code chunk, and save the results of 6 + 3 as a variable called a.
    • Does the answer appear? (please answer with text)
    • Where can you see the value of the object a? (please answer with text)
    • Next, type a into the code chunk and re-run the code chunk. What happens? (please answer with text)
# Type your code and comments inside the code chunk

a <- 6 + 3;
a
[1] 9

The answer does not appear anywhere because it a stores the value of 6 + 3. The value of the object a is seen in the environment. After typing a into the code chunk and re-running the code chunk it places the value of a in a line under the code chunk and before the code on line 207.

It is a good idea to try kitting your document from time to time as you go along. Go ahead, and make sure your document is knitting and that your html file includes Exercise headers, text, and code. Note that knitting automatically saves your Rmd file.

    • Run following command in a new code chunk: a^2.
    • What does the ^ operator do? (please answer with text)
# Type your code and comments inside the code chunk
a^2
[1] 81

The ^ operator is used to raise something to a power.

    • Type the following command into a new code chunk. sum(a, x, y)
    • sum is a function. Based on the output, what do you think the sum function does? (please answer with text)
# Type your code and comments inside the code chunk
sum(a, x, y)
[1] 22

The sum function adds things together, in this case variables.

    • Click the little broom icon in the upper right hand corner of the Environment pane. Click yes on the window that opens. What happened? (please answer with text, and don’t freak out)

It removed everything from the environment tab.

    • Go to the Run button at the top right of the R Markdown pane, and choose Run All (the last option)
    • What happened? (please answer with text)

It runs all code chunks in the entire file.

  1. Recall the vector v we created earlier. Copy, paste, and run the following in a code chunk. What does this code accomplish? (please answer with text)
v + 2
# Type your code and comments inside the code chunk
v + 2
[1] 4 6 8

It created a vector v with in this case three entries stored in it. Each time v + 2 is coded and run it will add an entire to the vector. It will take the last entrie and add two to it.

  1. Copy, paste, and run the following code to make a vector called music that contains music genres. Recall a vector is a data object that has multiple elements of the same type. Here the data type is a character. Look in the environment pane. How does R tell us that this vector contains characters not numbers? (please answer with text)
music <- c("bluegrass", "funk", "folk")
# Type your code and comments inside the code chunk
music <- c("bluegrass", "funk", "folk")

The R tells us that the vector contains a vector by using the abbreviation chr to mean character(s).

  1. Now let’s practice some basic formatting. Using https://rmarkdown.rstudio.com/authoring_basics.html, figure out how to put the following into your lab report. These all can get typed into the white section, where text goes. Hint: To put each of these on its own line! hit a hard return between each line of text!

Italicize like this

Bold like this

A superscript: R2

  1. Extra credit: What in the world does Charlie Brown have to do with R?

This is a trick question. Charlie Brown has nothing to do with R.

7 Turning in Your Work

You will need to make sure you commit and push all of your changes to the github education repository where you obtained the lab.

  • Make sure you knit a final copy with all your changes and work
  • Look at your final html file to make sure it contains the work you expect and is formatted properly. Type your text answers inside the </div> tags, and delete the extraneous comments.

8 Logging out of the Server

There are a lot of stats classes and students using the Server. To keep the server running as fast as possible, it is best to sign out when you are done. To do so, follow all the same steps for closing an R Markdown document as above:

  • Save all your work.
  • Click on the orange button in the far right corner of the screen to quit R
  • Choose don’t save for the Workspace image
  • When the browser refreshes, you can click on the icon of your name in the top right.
  • You are signed out.


sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.33

loaded via a namespace (and not attached):
 [1] bookdown_0.23     png_0.1-7         digest_0.6.27     R6_2.5.0         
 [5] jsonlite_1.7.2    magrittr_2.0.1    evaluate_0.14     highr_0.9        
 [9] stringi_1.7.3     rlang_0.4.11      jquerylib_0.1.4   bslib_0.2.5.1    
[13] rmarkdown_2.10    tools_3.6.0       stringr_1.4.0     jpeg_0.1-9       
[17] xfun_0.25         yaml_2.2.1        compiler_3.6.0    htmltools_0.5.1.1
[21] sass_0.4.0