RStudio Intro Handout

Paula Andrea Martinez

2018-01-23

Getting started

Start out by installing R and then RStudio1

Hands-on Training

Starting with programing

Learn things that last longer - pick your battles - Learn the fundamentals2

Remember

What is R and Rstudio

R is a powerful programming language for data analysis, statistics, visualisation and more. RStudio is the program that interacts between you and the language R. R and RStudio are two free available software with a huge community of users and developers.3

What are we going to learn?

At the end of this session you will be able to:

Rstudio interaction

Please create a folder called “RProjects” under “Documents” This is important for our project data management.

Exercise 1 - New Rstudio Project (4 min)

4

Panes or panels

There are four main panels on RStudio.

  1. The upper-left panel is the editor where we interact with scripts.
  2. The pane in the upper-right, where it says “Environment is empty,” will show the objects that you are currently working with.
  3. The lower-left panel is called the console, which runs the R code. It is a testing ground and only saves the code temporarily.
  4. The panel in the bottom-right will display results, files, help and more.

Exercise 2 - Folder structure (3 min)

Create three folders in your project5. Remember to use naming conventions or check6.

Exercise 3 - New R script (2 min)

Functions

A simple function
A simple function

How to get help

There are three ways to find help using RStudio8

  1. ?functioname
  2. help(functioname)
  3. Press F1 or “cmd F1” on the functioname

Exercise 4 - Check the description of these functions (2 min)

sessionInfo
list.files
ls

Exercise 5 - Add comments to your new R script file (3 min)

Comments start with #.

# Description:
# Author:
# Date:

To add a section

# Starting with objects --------------------------

R syntax

To get the hang of R, we start using it as a simple calculator. Type 2 + 2 directly into the console panel and press enter. You should see this:

2 + 2
## [1] 4

R variables or objects

R can calculate and store multiple values in variables or objects so we can access them later. Use: objectname <- value.

R Style

I recommend two style guides:

  1. The short and simple one
  2. The longer and updated

R data types

Data structures

Example of a numeric vector

many_numbers <- c(1, 2, 5.3, 6, -2, 4) # numeric vector
many_numbers
## [1]  1.0  2.0  5.3  6.0 -2.0  4.0

Exercise 6 - Create a vector (3 min)

You can create either a vector of characters or a vector of logicals

## [1] "one"   "two"   "three"
## [1]  TRUE  TRUE FALSE

Import files

Let’s introduce some data to R

download.file(url = "http://tiny.cc/csvexample", 
              destfile = "data/example.csv")

mydata <- read.csv(file = "data/example.csv")

Exercise 7 - Importing data into R (3 min)

Tip10

Install packages

Most R package you can be installed it like this: install.packages("packagename")

Then you need to load it using library(packagename)

Then go to the fourth panel and select the packages tab, after loading a package it should be checked.

You can also check sessionInfo()

Exercise 8 - Install the ggplot2 package for graphics (3 min)

Close project

“File” “close project” (It asks if you want to save your data), then you can close RStudio.

Resources

There are plenty of R resources, this is only a short list.

Feedback

Please send your annonymous feedback through this link http://tiny.cc/elixir_feedback

Open source

This handout was written in Rmarkdown, and uses the open-source Tufte style. It has been published on Github pages and also as a PDF handout.

All of the information of my courses can be found on my Github repo R for Data Analysis. These resources are freely available under the Creative Commons - Attribution Licence. You may re-use and adapt the material in any way you wish, without asking permission, provided you cite the original source. That is a link back to the website R for Data Analysis and my ORCID 0000-0002-8990-1985.

I acknowledge this publication is resulting from support of Elixir-Belgium for my role as data science and bioinformatics trainer.

Last update: 2018-02-09


  1. See installation instructions installation.md

  2. “Learning to code is a never ending journey with a set of challenges and delights unique to each person”

  3. resources

  4. FYI: Projects make managing multiple directories straightforward

  5. In RStudio, you can use the fourth panel then click “Files” then “New Folder”. Or you can use the function dir.create

  6. a style guide

  7. You can run code from a script using “Ctrl+Enter” (line by line or a selection of code)

  8. The help panel will show you the Documentation. How to use a function, input, details, and examples

  9. You can also read other kinds of file using read.table or special packages

  10. Always use the help in RStudio if you don’t know how a function works