Getting Started

Start a new script file

If a new empty R script file does not open automatically when you launch RStudio you can open a new one^[Alternatively use ctrl + shft + n to launch a new R script]

File -> New File -> Rscript

Make sure that you choose the correct type of file. The RStudio IDE is clever in the way that it treats file extensions. Choosing a R script file will make sure that when you come to save your script it is saved as the correct type. It is also necessary to allow us to easily send R code from the text editor to be evaluated in the R terminal.

Sending your first code

Let's start with sending our first piece of code to be evaluated. In the script editor (likely the top left window) write a simple piece of code, say

x = 5

To send this code from the editor to be run by R you have a few options:

  1. If the cursor is on the same line as the code you can either^[Both of these will only run the one line of code.]
  2. Click Run at the top left of the editor window
  3. Press ctrl + enter
  4. Highlight the code that you would like to run and follow the options in 1.
  5. ctrl + shft + s sends all code from the editor to be evaluated.

r margin_note("I tend to prefer using 'ctrl + enter' as I like using keyboard shortcuts. If you want to see all available keyboard shortcuts either go to Help and choose keyboard shortcuts, alternatively 'Alt + shft + k' is the keyboard shortcut for the keyboard shortcut menu.")

Course R package

Make sure that you have the course package loaded into the current session. Instructions on how to install the package are contained in the appendix to the course notes. You should only need to install a package once using install.packages() and then we need to use library() in every session or script that we want to use the functionality of that package.

library("jrIntroBio")

Vectors

Write the following code in the editor and run it

x1 = GetNumericVector()

This code generates a large vector of random numbers such that everyone has the same.^[This function is part of the course package jrIntroduction, you can view it's help page if you like r knitr::inline_expr('?GetNumericVector()')]

(1) How many elements are in x1? (i.e What is the length of x1) r length(x1)

(1) What is the 55^th^ element of x1? r x1[55]

(1) What is the final value in x1? r x1[length(x1)]

(1) What is the 50^th^ smallest value of x1? r sort(x1)[50]

(1) How many unique values are there in x1? r length(unique(x1))

(1) What is the total of all elements? r sum(x1)

Bonus questions

The following code will load a data set into your current session which contains a vector of the counts of yeast protein subcellular localisations[^1].

[^1]: Paul Horton & Kenta Nakai, "A Probablistic Classification System for Predicting the Cellular Localization Sites of Proteins", Intelligent Systems in Molecular Biology, 109-115. St. Louis, USA 1996.

data(yeast_classes, package = "jrIntroBio")

The vector loaded into the session is called yeast_classes and is the subject of the following questions.

(1) How many loci were located to the nucleus. r yeast_classes["NUC"]

(1) How many loci are in this data set? r sum(yeast_classes)

(1) What was the most common localisation in the dataset? Note: You should use R and its functions to find this without having to look through the numbers. r sort(yeast_classes, decreasing = TRUE)[1] ## We haven't seen this function yet ## but you could also use yeast_classes[which.max(yeast_classes)]

Solutions

Solutions to the practical questions are contained within the package

vignette("solutions1", package = "jrIntroBio")


jr-packages/jrIntroBio documentation built on Dec. 24, 2019, 8:03 a.m.