Introduction to R

Learning Objectives

  • Articulating motivations for this lesson
  • Introduce participants to the RStudio interface
  • Set up participants to create project from Git
  • Introduce R syntax
  • Point to relevant information on how to get help, and understand how to ask well-formulated questions

Before we get started

  • Head to GitHub (making sure you are logged in): https://github.com/mistrm82/2015-03-23-hbc-swc
  • From here you want to Fork the repo by clicking in the top right corner. This will create a copy in your own account. Now in your forked version you want to copy the `HTTPS clone url'
  • Start RStudio
  • Under the File menu, click on New project, choose Version Control, then Git
  • Here you will be prompted to enter a Repository URL. The forked repo (copied link) can now we be pasted
  • Once the url is copy/pasted, the Project directory name will automatically populate with the name of the repo 2015-03-23-hbc-swc. You will need to specify where you would like to place this directory. This will become your working directory for the rest of class. (e.g., ~/R/2015-03-23-hbc-swc).
  • Check your working directory using getwd()
  • Create a new R script (File --> New File --> R script) and save it in your working directory (e.g. softwarecarpentry-script.R)

Presentation of RStudio

Let's start by learning about our tool. RStudio is freely available open-source IDE (Integrated Development Environment). It is a great alternative to working on R in the terminal for many reasons:

  • automatic syntax highlighting/formatting in the editor
  • direct code execution from editor to console
  • real-time access to environment, plotting and history
  • good tool for workspace management

Point out the different panels in RStudio.

  1. Console: where you can type commands and see output
  2. Editor: where you can type out commands and save to file. You can also run in console with Ctrl Enter
  3. Workspace/History: workspace shows all active objects and history keeps track of all commands run in console
  4. Files/Plots/Packages/Help

Shortcuts

Navigating between Console and Script: Ctrl 2 will take you to the console, Ctrl 1 will take you back to the > script. Try it out!

Type in your script 6 + 6. Keeping your cursor on the line, if you press Ctrl Enter you will see that the code has been run in the console. Move to the console using shortcuts and use the up arrow to run the last command. You can clear the console with Ctrl L There are many shortcuts and they differ slightly based on the OS you are using.

Best practices

  • Code and workflow are more reproducible if we can document everything that we do.
  • Our end goal is not just to "do stuff", but to do it in a way that anyone can easily and exactly replicate our workflow and results.
  • All code should be written in the editor and saved to file, rather than working in the console. The R console should be used to inspect objects, test a function or get help.
  • Use # signs to comment. Comment liberally in your R scripts. This will help future you and other collabrators know what each line of code (or code block) was meant to do. Anything to the right of a # is ignored by R. A shortcut for this is Ctrl + Shift + C if you want to comment an entire chunk of text.
  • Separate the original data (raw data) from intermediate datasets that you may create for the need of a particular analysis. For instance, at the moment we have all of our data contained in the data directory within the current working directory that stores the raw data. You should create a results directory for intermediate datasets and a figures directory for the plots you will generate. You can do this by clicking on New folder and giving each the appropriate name.

Seeking help

I know the name of the function I want to use, but I'm not sure how to use it

If you need help with a specific function, let's say barplot(), you can type:

?barplot

If you just need to remind yourself of the names of the arguments, you can use:

args(lm)

If the function is part of a package that is installed on your computer but don't remember which one, you can type:

??geom_point

I want to use a function that does X, there must be a function for it, but I don't know which one...

If you are looking for a function to do a particular task, you can use help.search() (but only looks through the installed packages):

help.search("scatterplot")

If you can't find what you are looking for, you can use the rdocumention.org website, which searches through the help files across all packages available.

I am stuck... I get an error message that I don't understand

Start by googling the error message. However, this doesn't always work very well because often, package developers rely on the error catching provided by R. You end up with general error messages that might not be very helpful to diagnose a problem (e.g. "subscript out of bounds").

However, you should check stackoverflow. Search using the [r] tag. Most questions have already been answered, but the challenge is to use the right words in the search to find the answers: http://stackoverflow.com/questions/tagged/r

There is also a Support site for questions about Bioconductor packages, where you might find your question has already been asked.

The Introduction to R can also be dense for people with little programming experience, but it is a good place to understand the underpinnings of the R language.

The R FAQ is dense and technical, but it is full of useful information.

Once you feel you have mastered some of the basics, you may want to check out Hadley Wickham's online book to improve programming skills and better understand the R language.

Asking for help

The key to get help from someone is for them to grasp your problem rapidly. You should make it as easy as possible to pinpoint where the issue might be.

Try to use the correct words to describe your problem. For instance, a package is not the same thing as a library. Most people will understand what you meant, but others have really strong feelings about the difference in meaning. The key point is that it can make things confusing for people trying to help you. Be as precise as possible when describing your problem.

If possible, try to reduce what doesn't work to a simple reproducible example. If you can reproduce the problem using a very small data.frame instead of your 50,000 rows and 10,000 columns one, provide the small one with the description of your problem. When appropriate, try to generalize what you are doing so even people who are not in your field can understand the question.

To share an object with someone else, if it's relatively small, you can use the function dput(); it will output R code that can be used to recreate the exact same object as the one in memory:

dput(head(iris)) # iris is an example data.frame that comes with R
## structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), 
##     Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, 
##     1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 
##     0.2, 0.2, 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, 
##     1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length", 
## "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = c(NA, 
## 6L), class = "data.frame")

If the object is larger, provide either the raw file (i.e., your CSV file) with your script up to the point of the error (and after removing everything that is not relevant to your issue). Alternatively, in particular, if your question is not related to a data.frame, you can save any R object to a file:

saveRDS(iris, file="/tmp/iris.rds")

The content of this file is, however, not human readable and cannot be posted directly on stackoverflow. It can how be sent to someone by email who can read it with this command:

some_data <- readRDS(file="~/Downloads/iris.rds")

Last, but certainly not least, always include the output of sessionInfo() as it provides critical information about your platform, the versions of R and the packages that you are using, and other information that can be very helpful to understand your problem.

sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## 
## 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  base     
## 
## loaded via a namespace (and not attached):
## [1] evaluate_0.5.5 formatR_1.0    knitr_1.9      stringr_0.6.2 
## [5] tools_3.1.2

Where to ask for help?

  • Your friendly colleagues: if you know someone with more experience than you,they might be able and willing to help you.
  • Stackoverlow: if your question hasn't been answered before and is well crafted, chances are you will get an answer in less than 5 min.
  • The R-help: it is read by a lot of people (including most of the R core team), a lot of people post to it, but the tone can be pretty dry, and it is not always very welcoming to new users. If your question is valid, you are likely to get an answer very fast but don't expect that it will come with smiley faces. Also, here more than everywhere else, be sure to use correct vocabulary (otherwise you might get an answer pointing to the misuse of your words rather than answering your question). You will also have more success if your question is about a base function rather than a specific package.
  • Bioconductor Support site: for questions about Bioconductor packages
  • If your question is about a specific package, see if there is a mailing list for it. Usually it's included in the DESCRIPTION file of the package that can be accessed using packageDescription("name-of-package"). You may also want to try to email the author of the package directly.
  • There are also some topic-specific mailing lists (GIS, phylogenetics, etc...), the complete list is here.