Rstudio

Objective: learn to use RStudio, a free and open-source integrated development environment (IDE) for R.

The RStudio IDE cheat sheet gives a very complete and detailed overview of RStudio. Have a look at it.

RStudio panes

R console

Type for example

x <- c(3, 34, 12 40)

in the console then press enter. You should get this result.

RStudio overview

The “Environment” panel on the top right side shows all the variables already used.

To print the content of the variable x, type in the console

x

then press enter.

History pane

optional caption text

Help pane

optional caption text

Plot pane

optional caption text

Setting your working directory

To see what is your current working directory, type in the console

getwd()    # get working directory

then press enter.

optional caption text

To set another working directory, type in the console

setwd("YOUR_DIRECTORY")    # set working directory

then press enter. The variable "YOUR_DIRECTORY" is either:

WARNING You need to use the forward slash / as it is on Linux or Mac system. Alternatively, you can use the function file.path to correctly construct/assemble the file path:

setwd(file.path("C:", "RCourse", "mysubfolder", "run"))

Then check if the path has been correctly set by typing getwd() in the console. Notice that the files panel on the right changed!

optional caption text

You can also set the working directory through the menu interface of RStudio (see below): optional caption text

Installing packages

R packages are collections of functions and data sets. The official R repository for package is the Comprehensive R Archive Network. You can:

Have a look at the documentation of the package RConics:

  1. go to https://cran.rstudio.com/web/packages/RConics/index.html
  2. click on the link to the “reference manual” RConics.pdf Each function is documented (the function documentation is identical to the documentation displayed in the help panel in RStudio.

Packages can be installed by typing in the console

install.packages(c("RConics", "plot3D"))

where c("RConics", "plot3D") is the vector of packages you want to install.

optional caption text

Alternatively, you can install a package through the menu interface of RStudio (see below): optional caption text optional caption text

Once a package is installed, you need to load it in your current session to use it. Load the packages RConics and plot3D with the function library():

library("RConics")
library("plot3D")

Coding with RStudio

Normally you organise your code in a script file and then run it into the R console

Creating a script file

optional caption text optional caption text

Running the code