- Intend to run every year going forward
- This one is aimed at beginners
- Spring Into Bioinformatics in Sept-Oct
- Should have two streams: Beginner & Intermediate
- Organised along with BIG-SA
15 April 2019
Steve Pederson
gitS (Chambers et al, Bell Labs 1976)R is run by a volunteer community (R Core)tidyverse, bookdown, reticulate, roxygen2 etc)Rcpp)With great power comes great responsibility - Uncle Ben
To use R we need to understand a little about:
Today we will start with:
RStudioRRMarkdownBy the end of the day, we'll be able to
R and RStudio are two separate but connected things
R is like the engine of your carRStudio is the 'cabin' we use to interact with the engineRStudio even comes with extra flashy features not related to RR does all the calculations, manages the data, generates plotsRStudio helps manage our code, displays the plots plus moreRStudio<tab> keyR Projects to manage code/data for each project/analysis(There is no need to save your R workspace if asked)
File > New Project > New Directory > New Project
R_Training as the project name (check capitals & underscores)Create ProjectNew Folder then name it dataR Projects are simply a wrapper for keeping an analysis organised
R Project name will always be the directory nameFile > New File > R ScriptIntroduction.R# symbolConsole1 + 1
## [1] 2
2 * 2
## [1] 4
As well as performing simple calculations:
R has what we call an Environment (i.e. a Workspace)R performs calculations & runs processes on these objectsR objectSheet1)nameOfObject <- data
<- symbol is like an arrowR to put the data in the objectIn the Console type:
x <- 5
xx by entering it's name directly in the Console, or by calling print()x print(x)
Where have we created the object x?
x in our R WorkspaceGlobal EnvironmentEnvironment is like your desktop.RData objectsave.image()
Once an object is in the Environment we can perform calculations on it
1 + x
## [1] 6
x^2
## [1] 25
R has a series of inbuilt functions, e.g. sqrt(), log(), max(), min() etc.
() after the name of a functionsqrt(x)
## [1] 2.236068
log(x)
## [1] 1.609438
base
R?base
Help pane
Index at the bottom for a list of functions in the base packagesR object multiple valuesR requires all elements of a vector to be the same typex <- 1:5 x
## [1] 1 2 3 4 5
length(x)
## [1] 5
Functions & calculations work on the entire vector
x + 1 x^2 sqrt(x) max(x) sum(x) mean(x) sd(x)
Best practice for all analysis is to enter our code in the Script Window
RStudio will:
#
# Create an object called x x <- 5
# Create an object called x x <- 5
To send this to the Console:
Ctrl+Enter (Cmd+Enter on MacOS), orCtrl+Enter (or Cmd+Enter)Run button
As well as creating objects, we can use this to write general code
# I'm not sure. Is x greater than zero? x > 0
## [1] TRUE
Including comments describing your intention, is highly advisable
RStudio will give us suggestions when we ask it to.
?bas then hit the <tab> key
Environment Tab is the History Tab
ConsoleBest coding practice is to enter code in the Script Window and execute
In the bottom right are a series of tabs
Files: This shows your current working directoryPlots: Every time you make a graph it appears herePackages: NEVER CLICK OR UN-CLICK ANYTHING HEREHelp: We all use this a lot!Help > Cheatsheets > RStudio IDE Cheat Sheet
Page 2 has lots of hints:
Ctrl + 1 places focus on the Script WindowCtrl + 2 places focus on the ConsoleCtrl + 3 places focus on the Help Tab