Skip to Main Content

Datalab: R

About R and RStudio

R is an open source and free programming language which is often used as a data analysis and statistical software tool. R is most commonly used together with the IDE (Integrated Development Environment) application RStudio which can be downloaded and used for free. RStudio provides access to source, console, environment, history, files, graphs on a single screen.

 

   
   
   

Distributed under the terms of the
Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0)

 

Installation of R and RStudio

It is important that you install R before you download and install RStudio.

R is an open source and free programming language. It is important that you install R before you download and install RStudio. Please follow these instructions:

Installation of R for Windows

  1. Go to the CRAN R project mirrors website
  2. Choose your country mirror, for Norway we recommend https://cran.uib.no/
  3. Click on the Download R link for your operating system (Windows, Mac or Linux)
  4. For Mac: Click on the download file for the latest release
    For Windows: Click on the base subdirectory link 
  5. Click Download R-(version) for Windows
  6. Start the installation by clicking the downloaded file
     

It is important that you install R before you download and install RStudio.

  1. Go to the download site for RStudio
  2. Choose the RStudio Desktop - Free version and click Download
  3. Choose the installer for your operating system

Windows

Click the Windows-button and scroll down to find RStudio in the list of applications

Mac

Open Spotlight and search for RStudio

 

Create R Script

Now we're ready to get started. The first thing we should do is to create a new R-file to work with.

Go to File -> New File -> R Script

 

Saving R Script

Save your script in a place where you can find it later

Click File -> Save As

 

 

 

  1. Go to Session
  2. Choose Set Working Directory - > Choose Working Directory


     
  3. Choose the folder where your files are, in this example C:\Users\[username]\Downloads

The console should now show your working directory

You should also see the correct path and file in Files section

Course Recordings

Follow this link to download the course. 

This course covers the following

  • RStudio Interface
  • Setting Eorking Directory
  • Variables/Objects
  • Built-in Functions 
  • Inspect, select and manipulate one column in your dataset 
  • Visulize data using boxplot
  • Give your plot color and title
  • Read files (which are saved on your computer) 
  • Visualize data using histogram
  • Manipulate date formats 
  • Aggregate data using the Stats library
  • Visulize data using the ggplot library

 

 

 

 

 

Follow this link to download the course. 

This course covers the following:

  • R Operators and Functions
  • Correlations
  • Regression Models
  • Statistical Tests
  • Visualizations with ggplot
  • R Markdown

Adviced background knowledge: Basic experience with R and RStudio, equivalent to our introdutory course

Interface

The default interface in RStudio consists of four boxes.

  • The Source window (Top left)
    • This is where you type your code and notes. It is basically your own R-file/-Script. This is where you can open, write and edit R Scripts and code.  It is possible to open and edit other text-based file formats like plain text (.txt), websites (.html), LaTex (.tex), BibTex (.bib), SPSS (.sav) and markdown (.md).
  • The Console window (Bottom left)
    • The console is where you can see the output of your coding. It is possible to write code commands directly in the console if needed but we recommend to use the Source window for larger scripts.
  • The Environment / History / Connections / Tutorial window (Top right)
    • Environment: The default tab is Environment. This is where you can find objects available for computation. This can typically be a dataset you have imported and want to work with and analyse. Other object types could be plots, vectors and functions.
    • History: This window shows the history of computation you have made, so it is possible to go back to see your code and computations.
    • Connections: This pane allows you to tab directly into external databases. This can be useful when you are collaborating with others on the same data or want to work with datasets without having to download the data.
    • Tutorial: Here you can find learning materials for R and RStudio.
  • The Files / Plots / Packages / Help / Viewer window (Bottom Right)
    • Files: This tab lists all files and folders in your Working Directory. This is where your files will be saved and the place where you can import datasets you want to work with.
    • Plots: Here you can see the plots you have created in your scripts.
    • Packages: Packages are add-ins you can import and use in your analyses. 
    • Help: If you need help in R/RStudio you can search in the Help tab. 
    • Viewer: This is where dynamic data visualisations and websites created in R will be shown.