Packages are after-market installations. You download them once, then call each time you need to use them.
Let's install and call the tidyverse package, which we will be using later in the seminar. This is a highly-used set of tools for data manipulation, analysis and visualization.
First, install, the packages with install.packages. Do this once. Type this into the R console and hit Enter.
install.packages("tidyverse")
You will be asked which mirror to download from. Select a location near you!
To load the package to your workspace, call it using library. Do this each session you want to use the package.
library(tidyverse)
From time to time, remember to check for updates to the packages you are running with `update.packages()'.
This is really the only time we are going to use R on its own. For the rest of the lesson, we will be working from RStudio.
When you open RStudio for the first time you will see three panes. After writing your first script, you'll see four.
credit: "R for Fledglings," Therese M. Donovan.
We will now walk through the various panes of RStudio which will begin the introduction to R programming.
This is where commands are submitted to R to execute.
Type your commands after the prompt symbol, which looks like >.
You should see a vertical, blinking bar to the right of the prompt. We will start coding here.
We'll be doing the first next.
R can be used as a fancy calculator. Type this directly into the console.
1+1 ## [1] 2 5*4 ## [1] 20 2^3 ## [1] 8
2+3*4/(5+3)*15/2^2+3*4^2 ## [1] 55.625 5e4 ## [1] 50000
R also allows you to test conditions of equality among values: + Equality: == (NOT = - this is important!) + Not equal: != + Greater / Less than: > or < + Greater / Less than or equal to: >= or <=
#does 5 equal 4? 5 == 4 ## [1] FALSE
#is 10 less than or equal to 11? 10<=11 ## [1] TRUE
#is the sqrt of 36 equal to 6 squared? sqrt(36) != 6 ## [1] FALSE
sqrt(36) ## [1] 6
You've just passed the number 36 through the sqrt function, just as you might would on a scientific calculator.
Sqrt(10):Sqrt(10) ## Error in eval(expr, envir, enclos): could not find function "Sqrt"
Remember, it's the sqrt function, NOT the Sqrt function.
Moral of the story: R is case sensitive and all-around finnicky!
Normally you would not interact directly with the Console. Instead, you will write your code into the script file and then send the code to the console for it to execute.
The script file is your long-term record of the code you ran. The console only keeps a short-term record of it.
In RStudio, choose File | New File | R Script or Ctrl+N to open a new R script.
You will see a blank document in the upper left panel of RStudio.
Type the following lines of code into your R Script.
#get the square root of 36 sqrt(36) ## [1] 6
Lines of code that are preceded by the# symbol are comments.
Comments are not executed in R. Rather, they are notes that help the coder understand and remember what the program should do.
To send code to R, place your cursor anywhere on the line, then press the Run button. You can also use Ctrl+Enter in RStudio.
#get the square root of 36 sqrt(36) ## [1] 6
Notice that after executing the line your cursor drops to the next line of code.
You can also highlight multiple lines of code at once and press Run.
#get the square root of 36 sqrt(36) ## [1] 6 #get the square root of 100 sqrt(100) ## [1] 10
To save your R script, go to File | Save or type Ctrl+S in RStudio.
The help tab in this window is useful for beginners. It returns a help file when you use help() in your command prompt:
#call the helpfile for the sqrt funtion help(sqrt) ## starting httpd help server ... done
This tab will hold any plots you create in your R section.
We will create a plot later, but for now let's go ahead and use the help() command on plot() itself.
Each helpfile in R contains a section called Examples, and there you can copy code from the helpfile and run it in the console to see an example of the function in action.
#plot example of plot function plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
On the History tab you will see a history of all the commands you have sent to R console in the session.
Now let's click on the Environment tab.
You will see that R has created an object called "x." Where did this come from? You created this when you copied and ran code from the plot function example. Objects are largely what make R, R.
Earlier we used sqrt to calculate the square root of a number. Wouldn't it be nice to save the result to use for later? Let's do that by creating an object, result. Type the following into your script:
#use sqrt function to calculate sqrt of 10 and store result in object called result result<-sqrt(10)
You should see that the object appears in the section on the Environment tab labeled "Global Environment." A major part of your work involves creating objects.
The objects stored in the Environment and History together make up the workspace environment.
When you close R, R brings up a window that asks whether you'd like to save the workspace image (a snapshot of the current contents in the environment).
As a good practice, rarely save the workspace if the code can be quickly re-run. It is easy to accumulate an unmanageable amount of data and objects.
rm(list=ls()) in your script.ls().rm function. For exampe, rm(x) will remove x from your environment.save function. For example, save(x) will save individual objects in the workspace.Now that you have a brief introduction to RStudio's panes let's look at the program's options where you can control settings such as font size, etc.
Go to Tools | Global Options and you'll see the following box: