IOC-R Week 1
R is a programming language and software environment designed for statistical computing and data analysis.
Why R?
What you can do with R?
Type a command after the prompt “>” and press Enter to execute it.
If you use the RStudio Server, you need to connect to your account first.
Integrated Development Environment (IDE)
Rstudio cheat sheet (and French version)
# 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()
Go to Tools -> Global Options…
Under the General tab:
Go to Tools -> Global Options…
Under the Code tab:
In the Display panel, check the box “Use rainbow parentheses”
Go to Tools -> Global Options…
You can select a theme for you RStudio, e.g., the “Cobalt” for a dark theme.
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!
An R project keeps everything for your project in one working directory and helps R to know where to look for your files.
my_project
) and select where to save it.You’ll notice RStudio restarts, and now your project is set up!
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
A path tells your computer where to find files.
/home/test_user/my_project/data/myfile.csv
C:\Users\test_user\my_project\data\myfile.csv
data/myfile.csv
or data\myfile.csv
(Windows)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
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
Official website: https://quarto.org
To create: File -> New File -> Quarto Document -> click Create.
Save the document and click Render button.
Rendered document in HTML format:
Key points:
To save the slides as a PDF:
After saving, press “e” again to return to presentation mode.