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?
The official website: https://www.r-project.org
In the R console:
>
(The prompt): R is ready for your command. Type your code and press Enter to run it.[1]
(Output): This indicates the start of an output line.+
(Continuation): R is waiting for you to finish your command. You can either finish the command or press Esc to cancel 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)
Copy paste following codes into your script and save the script as test.R
.
# 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!
x + 1
# 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:
Then click “Apply”.
Go to Tools -> Global Options…
In the Display panel, check the box “Use rainbow parentheses”
Then click “Apply”.
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!
Then click “Apply”.
An R project keeps everything for your project in one working directory and helps R to know where to look for your files.
r_week1
) and select where to save it.You’ll notice RStudio restarts, and now your project is set up!
r_week1 # Your working directory
├── r_week1.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
data
and scripts
folders inside your working directory.test.R
into the scripts
folder of your working directory.How to tell your computer where to find the test.R
?
/Users/lijiao/Documents/r_week1/scripts/test.R
C:\Users\lijiao\Documents\r_week1\scripts\test.R
A path tells your computer where to find files/folders.
scripts/test.R
scripts\test.R
r_week1 # Your working directory
├── r_week1.Rproj # The R project configuration file
├── data # Raw data files
└── scripts # Your R code files
└── test.R
What are the absolute and relative paths of the folder “data”?
absolute path: /Users/lijiao/Documents/r_week1/data
relative path: data
Importing your first file: shiny_sample_data.csv
data
folder of the R project r_week1
Think before you import the data
Once you’ve checked these settings and the preview looks correct, click Import.
We’ll use the built-in iris dataset and save it in the CSV format using the write_csv()
function from the {readr} package.
head(iris) # show the first lines of the dataset
View(iris) # preview the whole dataset
?write_csv # get help of the function
Goal: Save the built-in iris
dataset as a CSV file named iris_export.csv
inside the outputs
folder of the project.
outputs
folder.Go to the Files pane, can you find the saved file?
(We’ll talk about other formats in the future!)
Key points:
To save the slides as a PDF:
After saving, press “e” again to return to presentation mode.