Running R Codes

There are two ways to run codes in R. These are:

  • Interactive Mode.

  • Batch Mode.

Running R in Batch Mode

Sometimes it’s preferable to automate the process of running R. For example, we may wish to run an R script that generates a graph output file, and not have to bother with manually running R. Here’s how it could be done.

In RStudio, Go to File > New File > R Scipt. Then type the following in the script.

# set graphical output file
pdf("xh.pdf") 
# generate 100 N(0,1) variates & plot their histogram
hist(rnorm(100)) 

Running in Interactive Mode

We start R from our R console command line, and get the greeting message and the > prompt:

R : Copyright 2005, 
The R Foundation for Statistical Computing
Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0
...
Type ‘q()’ to quit R.
>

Now let’s make a simple data set, a vector in R parlance, consisting of the numbers 1, 2 and 4, and name it x:

x <- c(1,2,4)
  • The standard assignment operator in R is <-.

  • However, there are also ->, = and even the assign() function.

  • The “c” stands for “concatenate.”

Thus we can also do, for instance,

q <- c(x,x,8)

which would set q to (1,2,4,1,2,4,8).

Try to print the vector x to the screen by typing its name.

x
## [1] 1 2 4

Yep, sure enough, x consists of the numbers 1, 2 and 4.

Let’s take a closer look to our results.

  • The “[1]” is an indicator that this is the first row or output. If there were two rows of outputs with six items per row, then the second row would be labeled [7]. Our output in this case consists of only one row, but this notation helps users read voluminous output consisting of many rows.

  • In interactive mode, you can print an object in R by simply typing its name, let us try to print the third element of the vector x.

x[3]
## [1] 4
  • We can also find the mean and standard deviation of x:
    mean(x)
## [1] 2.333333
    sd(x)
## [1] 1.527525
  • We can also save the mean value in a variable, say y:
    y<-mean(x) #assign the mean to y
    print(y) #print thevalue of y
## [1] 2.333333

Note that we use # to write comments.

Built-in datasets

We can work with one of R’s internal datasets, which is intended for demos. You can get a list of these datasets by typing

    data()

The output is as follow:

Figure

One of the datasets is Nile, containing data on the flow of the Nile River. Let’s again find the mean, standard deviation,

    mean(Nile)
## [1] 919.35
    sd(Nile)
## [1] 169.2275

We can also plot the histogram.

    hist(Nile)

Getting help with R functions

Getting help on a specific function

To read more about a given function, for example mean, the R function help() can be used as follow:

help(mean)

Or use this:

?mean

The output look like this:

Figure 2

If you want to see some examples of how to use the function, type this: example(function_name).

example(sum)
## 
## sum> ## Pass a vector to sum, and it will add the elements together.
## sum> sum(1:5)
## [1] 15
## 
## sum> ## Pass several numbers to sum, and it also adds the elements.
## sum> sum(1, 2, 3, 4, 5)
## [1] 15
## 
## sum> ## In fact, you can pass vectors into several arguments, and everything gets added.
## sum> sum(1:2, 3:5)
## [1] 15
## 
## sum> ## If there are missing values, the sum is unknown, i.e., also missing, ....
## sum> sum(1:5, NA)
## [1] NA
## 
## sum> ## ... unless  we exclude missing values explicitly:
## sum> sum(1:5, NA, na.rm = TRUE)
## [1] 15

Note that, typical R help files contain the following sections:

  • Title Description: a short description of what the function does.
  • Usage: the syntax of the function.
  • Arguments: the description of the arguments taken by the function.
  • Value: the value returned by the function
  • Examples: provide examples on how to use the function

General help

If you want to read the general documentation about R, use the function help.start():

help.start()

The output look like this:

Figure 3

Others

  • apropos(): returns a list of object, containing the pattern you searched, by partial matching. This is useful when you don’t remember exactly the name of the function:
# Returns the list of object containing "med"
apropos("med")
## [1] "elNamed"        "elNamed<-"      "median"         "median.default"
## [5] "medpolish"      "runmed"
  • help.search() (alternatively ??): Search for documentation matching a given character in different ways. It returns a list of function containing your searched term with a short description of the function.
help.search("mean")
# Or use this
??mean

Help on the internet

There are many excellent resources on R on the Internet. Here are a few:

• The R Project’s own manuals are available from the R home page,http://www.r-project.org/. Click Manuals.

• Various R search engines are listed on the R home page. Click Search.

• The sos package offers highly sophisticated searching of R materials.

• I use the RSeek search engine quite often: http://www.rseek.org/.

• You can post your R questions to r-help, the R list server. You can obtain information about this and other R list servers at http://www.r-project.org/mail.html. You can use various interfaces. I like Gmane http://www.gmane.org/.

Because of its single-letter name, R is difficult to search for using general purpose search engines such as Google. But there are tricks you can employ. One approach is to use Google’s filetype criterion. To search for R scripts (files having a .R suffix) pertaining to, say, permutations, enter this:

filetype:R permutations -rebol

The -rebol asks Google to exclude pages with the word “rebol,” as the REBOL programming language uses the same suffix.

R Packages

What is R packages?

  • An R package is an extension of R containing data sets and specific functions to solve specific questions.

  • R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work.

  • There are also thousands other R packages available for download and installation from CRAN, Bioconductor and GitHub repositories.

  • After installation, you must first load the package for using the functions in the package.

Installing R packages

  • Packages can be installed either from CRAN (for general packages), from Bioconductor (for biology-related packages) or from Github (developing versions of packages).

Install a package from CRAN

The function install.packages() is used to install a package from CRAN. The syntax is as follow:

install.packages("package_name")

For example, to install the package named readr, type this:

install.packages("readr")

Note that every time you install an R package, R may ask you to specify a CRAN mirror (or server). Choose one that’s close to your location, and R will connect to that server to download and install the package files.

It’s also possible to install multiple packages at the same time, as follow:

install.packages(c("readr", "ggplot2"))

or you can use the RStudio point and click options to do this: Packages > Install > type the name of the package you want to install.

Figure 4

Install a package from Bioconductor

Bioconductor contains packages for analyzing biological related data. In the following R code, we want to install the R/Bioconductor package limma, which is dedicated to analyse genomic data.

To install a package from Bioconductor, use this:

source("https://bioconductor.org/biocLite.R")
biocLite("limma")

Install a package from Github

GitHub is a repository useful for all software development and data analysis, including R packages. It makes sharing your package easy. You can read more about GitHub here: Git and GitHub, by Hadley Wickham.

To install a package from GitHub, the R package devtools (by Hadley Wickham) can be used. You should first install devtools if you don’t have it installed on your computer.

For example, the following R code installs the latest version of survminer R package developed by A. Kassambara (https://github.com/kassambara/survminer).

install.packages("devtools")
devtools::install_github("kassambara/survminer")

View the list of installed packages

To view the list of the already installed packages on your computer, type :

installed.packages()

or you can go to Packages tab to check the install packages.

Figure 5

Folder containing installed packages

R packages are installed in a directory called library. The R function .libPaths() can be used to get the path to the library.

.libPaths()
## [1] "C:/Users/Roel Ceballos/Documents/R/win-library/4.0"
## [2] "C:/Program Files/R/R-4.0.2/library"

Load and use an R package

To use a specific function available in an R package, you have to load the R package using the function library().

In the following R code, we want to import a file into R using the R package readr, which has been installed in the previous section.

The function read_tsv() [in readr] can be used to import a tab separated .txt file:

# Import my data
library("readr")
my_data <- read_tsv("http://www.sthda.com/upload/decathlon.txt")
# View the first 6 rows and thee first 6 columns
# syntax: my_data[row, column]
my_data[1:6, 1:6]

View loaded R packages

To view the list of loaded (or attached) packages during an R session, use the function search():

search()

If you’re done with the package readr and you want to unload it, use the function detach():

detach("readr", unload = TRUE)

Remove installed packages

To remove an installed R package, use the function remove.packages() as follow:

remove.packages("package_name")

Update installed packages

If you want to update all installed R packages, type this:

update.packages()

To update specific installed packages, say readr and ggplot2, use this:

update.packages(oldPkgs = c("readr", "ggplot2"))

or you can go to : Packages > Update > Check the package to update

Figure 6