Kickstart with R:
Your Gateway to Data Science

IOC-R Week 1

Unlock the Power of Your Data with

Welcome to R!

R is a programming language and software environment designed for statistical computing and data analysis.

Why R?

  • Biologist friendly!
  • Free and open-source
  • Supported by a huge community

What you can do with R?

  • Analyze your data
  • Visualize your findings
  • Automate your workflow
  • Share reproducible research

The R Console

Type a command after the prompt “>” and press Enter to execute it.

Rstudio

Rstudio Login

If you use the RStudio Server, you need to connect to your account first.

Rstudio IDE

Integrated Development Environment (IDE)

  1. Console: enter and execute R commands interactively.
  2. Environment/History: tracks your variables and past commands.
  3. Files/Plots/Packages: managing your files, viewing graphs, or installing tools.

Getting Started in R

  • Open your RStudio.
  • Create an R script: File -> New File -> R Script or
  • Save script: Cmd + S or Ctrl + S
  • Basic commands1:
# Use `#` to write a comment (line) in your script
# Math with R:
2 + 2    # Yes, R can do math!
sqrt(16) # Square root.

# Create a variable:
x <- 5 # Now x is 5. You can reuse it!

# Inspect your environment:
## Look at the "Environment" pane, do you see your x?
ls()

# Load a package:
library(stats)

# Get help of a function (documentation):
?mean

# Quit RStudio properly
q()

Some Configuration

Go to Tools -> Global Options…

Under the General tab:

  • Uncheck the box “Restore .RData…”
  • Set “Save workspace to .RData on exit” to Never.

Some Configuration

Go to Tools -> Global Options…

Under the Code tab:

In the Display panel, check the box “Use rainbow parentheses”

Some Configuration

Go to Tools -> Global Options…

  • Under the Appearance tab:

You can select a theme for you RStudio, e.g., the “Cobalt” for a dark theme.

  • Under the Pane Layout tab:

You can arrange the four panels as you prefer.

In addition, click “Add Column” lets you open an extra “Source” code column, allowing you to view your scripts side by side!

Setting Up an R Project

Create an R Project

An R project keeps everything for your project in one working directory and helps R to know where to look for your files.

  1. In RStudio, go to File -> New Project
  2. Choose New Directory and click New Project
  3. Set a folder name (e.g., my_project) and select where to save it.
  4. Click Create Project

You’ll notice RStudio restarts, and now your project is set up!

A Well Organized Project Folder

Tree structure:

my_project              # Your working directory    
├── my_project.Rproj    # The R project configuration file
├── README.md           # A short introduction of the project
├── data                # Raw data files
│   └── raw_count.tsv
├── scripts             # Your R code files
│   └── 01-analysis.R
│   └── 02-figures.R
└── outputs             # Results, e.g., graphs, reports


  • To create new folders:
    • via RStudio: in File pane, click . The folder will be created in the current directory.
    • or use File Explorer (Windows) / Finder (Mac).

Understanding File Paths

A path tells your computer where to find files.

  • Absolute path, starts from the very top of your computer’s folder
    • E.g.:
      • In real-life: France, Paris, 9 Quai St Bernard, Building B, office 725
      • On Linux/Mac: /home/test_user/my_project/data/myfile.csv
      • On Windows: C:\Users\test_user\my_project\data\myfile.csv
  • Relative path, starts from your working directory (folder)
    • E.g.:
      • Building B, office 725
      • data/myfile.csv or data\myfile.csv (Windows)

Where Are You?

my_project              # Your working directory
├── my_project.Rproj    # The R project configuration file
├── README.md           # A short introduction of the project
├── data                # Raw data files
│   └── raw_count.tsv
├── scripts             # Your R code files
│   └── 01-analysis.R
│   └── 02-figures.R
└── outputs            # Results, e.g., graphs, reports


getwd() # Get working directory
[1] "/home/test_user/my_project"

What are the absolute and relative paths of the script “02-figures.R”?

absolute path: /home/test_user/my_project/scripts/02-figures.R

relative path: scripts/02-figures.R

file.exists("/home/test_user/my_project/scripts/02-figures.R")
[1] TRUE
file.exists("scripts/02-figures.R")
[1] TRUE

Import Dataset

  • Supported formats:
    • text files (.txt, .csv, etc.)
    • Excel files
    • data from other softwares (e.g.: SAS)
  • Click-button methods:
    • From menu bar: File -> Import Dataset -> Select the format -> Choose your file
    • From the “Environment” pane: click Import Dataset -> Select the format -> Choose your file
    • From the “Files” pane: Navigate to your file location -> Click the file and select Import Dataset

Automated Reporting

Quarto

  • Open-source tools for creating dynamic and shareable document.
  • Combines narrative text and code (multi-language) in one script (.qmd).
  • Supports multiple output formats: .html, .docx, .pptx, .pdf, etc.

Official website: https://quarto.org

An Example

To create: File -> New File -> Quarto Document -> click Create.

Save the document and click Render button.

Rendered document in HTML format:

Need Some Help?

  • Google it!
  • Forums: Stack Overflow, Cirad( francophone), etc.
  • Ask/discuss your question via Slack.
  • AI (like ChatGPT) is your friend 😉, but be careful of false answers! (More details in session 6)

Key points:

  • Explain your question step-by-step.
  • Share the context.
  • Use keywords.
  • Try to rephrase the question.

PDF of Slides

To save the slides as a PDF:

  • Open the presentation in a web browser
  • Press “e” to enable PDF export mode
  • Press Ctrl + p (Windows) or Cmd + P (Mac) to print
  • Select “save as PDF” as the printer option and save the file

After saving, press “e” again to return to presentation mode.

Let’s Practice !

Today’s Goals

  • Get familiar with the RStudio
  • Create an R project in RStudio
  • Import data file into RStudio
  • Generate your first Quarto report