Skip to content
Snippets Groups Projects
Verified Commit afa86e3c authored by Laurent Modolo's avatar Laurent Modolo
Browse files

work on session_1

parent 158b8e95
No related branches found
No related tags found
3 merge requests!6Switch to main as default branch,!4update contributing,!3Carine dev
--- ---
title: 'R#1: Introduction to R and RStudio' title: 'R#1: Introduction to R and RStudio'
author: "Laurent Modolo [laurent.modolo@ens-lyon.fr](mailto:laurent.modolo@ens-lyon.fr), Hélène Polvèche [hpolveche@istem.fr](mailto:hpolveche@istem.fr)" author: "Laurent Modolo [laurent.modolo@ens-lyon.fr](mailto:laurent.modolo@ens-lyon.fr), Hélène Polvèche [hpolveche@istem.fr](mailto:hpolveche@istem.fr)"
date: "Mars 2020" date: "2021"
output: output:
html_document: default rmdformats::downcute:
pdf_document: default self_contained: true
use_bookdown: true
default_style: "dark"
lightbox: true
css: "../src/style.css"
--- ---
<style type="text/css">
h3 { /* Header 3 */
position: relative ;
color: #729FCF ;
left: 5%;
}
h2 { /* Header 2 */
color: darkblue ;
left: 10%;
}
h1 { /* Header 1 */
color: #034b6f ;
}
#pencadre{
border:1px;
border-style:solid;
border-color: #034b6f;
background-color: #EEF3F9;
padding: 1em;
text-align: center ;
border-radius : 5px 4px 3px 2px;
}
legend{
color: #034b6f ;
}
#pquestion {
color: darkgreen;
font-weight: bold;
}
}
</style>
```{r setup, include=FALSE} ```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(comment = NA)
```
```{r klippy, echo=FALSE, include=TRUE}
klippy::klippy(
position = c('top', 'right'),
color = "white",
tooltip_message = 'Click to copy',
tooltip_success = 'Copied !')
``` ```
...@@ -64,25 +43,17 @@ The objectives of this session will be to: ...@@ -64,25 +43,17 @@ The objectives of this session will be to:
### Acknowledgments ### Acknowledgments
<div id='pencadre'>
![](./img/software_carpentry_logo.svg){width=300px} ![](./img/software_carpentry_logo.svg){width=300px}
https://software-carpentry.org/ https://software-carpentry.org/
</div>
![](./img/ModernDive.png)
https://moderndive.com
<div id='pencadre'>
![](./img/r_for_data_science.png){width=100px} ![](./img/r_for_data_science.png){width=100px}
http://swcarpentry.github.io/r-novice-gapminder/ http://swcarpentry.github.io/r-novice-gapminder/
</div>
\
- **Margot** and **Alexandre** for assistance!
# Some R background # Some R background
...@@ -102,31 +73,62 @@ Reasons to use it: ...@@ -102,31 +73,62 @@ Reasons to use it:
- it has a large (and growing) user base among scientists - it has a large (and growing) user base among scientists
- it has a large library of external packages available for performing diverse tasks. - it has a large library of external packages available for performing diverse tasks.
- **15,068** available packages on https://cran.r-project.org/ ```{r echo=F}
- **3,087** available packages on http://www.bioconductor.org cran_packages <- nrow(available.packages(repos = "http://cran.us.r-project.org"))
- **122,720** available repository on https://github.com/ library(rvest)
url <- 'https://www.bioconductor.org/packages/release/bioc/'
biocPackages <- url %>% read_html() %>% html_table() %>%.[[1]]
bioconductor_packages <- nrow(biocPackages)
````
- **`r cran_packages`** available packages on https://cran.r-project.org/
- **`r bioconductor_packages`** available packages on http://www.bioconductor.org
- **>500k** available repository using R on https://github.com/
## How do I use R ?
R is usually used in a terminal: Unlike other statistical software programs like Excel, SPSS, or Minitab that provide [point-and-click](https://en.wikipedia.org/wiki/Point_and_click) interfaces, R is an [interpreted language](https://en.wikipedia.org/wiki/Interpreted_language).
This means that you have to write instructions for R. Which means that you are going to learn to write code / program in R.
R is usually used in a terminal in which you can type or paste your R code:
![](./img/R_terminal.png) ![](./img/R_terminal.png)
But navigating between your terminal, your code and your plots can be tedious, this is why in `r format(Sys.time(), "%Y")` there is a better way to do use R !
# RStudio, the R Integrated development environment (*IDE*) ## RStudio, the R Integrated development environment (*IDE*)
IDE application that provides **comprehensive facilities** to computer programmers for An IDE application provides **comprehensive facilities** to computer programmers for
software development. Rstudio is **free** and **open-source**. software development. Rstudio is **free** and **open-source**.
To open RStudio, you can install the [RStudio application](https://www.rstudio.com/products/rstudio/) and open the app.
Otherwise you can use the link and the login details provided to you by email. The web version of Rstudio is the same as the application expect that you can open it any recent browser.
### An interface ## Rstudio interface
![](./img/RStudio.png) ![](./img/RStudio.png)
### The same console as before (in Red box) ## The same console as before (in Red box)
![](./img/RStudio_console.png) ![](./img/RStudio_console.png)
# Errors, warnings, and messages
The R console is a textual interface, which means that you will enter code, but it also means that R is is going to write informations back to you and that you will have to pay attention at what is written.
There are 3 categories of messages that R can send you: **Errors** prefaced with `Error in…`, **Warnings** prefaced with `Warning:` and **Messages** which don’t start with either `Error` or `Warning`.
- **Errors**, you must consider them as red light. You must figure out what is caussing it. Usually you can find usefull clue in the errors message about how to solve it.
- **Warning**, warnings are yellow light. The code is running but you have to pay attention. It's almost always a good idea to try to fix warnings.
- **Message** are just frindly messages from R telling you how things are running.
# R as a calculator # R as a calculator
Now that we know what we should do and what to expect, we are going to try some basic R instructions.
- Add: `+` - Add: `+`
- Divide: `/` - Divide: `/`
- Multiply: `*` - Multiply: `*`
...@@ -134,39 +136,35 @@ software development. Rstudio is **free** and **open-source**. ...@@ -134,39 +136,35 @@ software development. Rstudio is **free** and **open-source**.
- Exponents: `^` or `**` - Exponents: `^` or `**`
- Parentheses: `(`, `)` - Parentheses: `(`, `)`
<div id='pencadre'> > Now Open RStudio.
**Now Open RStudio.** > Write the commands in the grey box in the terminal.
> The expected results will always be printed in a white box here.
**Write the commands in the grey box in the terminal.** > You can `copy-paste` but I advise you to practice writing directly in the terminal. To validate the line at the end of your command: press `Return`.
**The expected results will always be printed in a white box here.**
**You can `copy-paste` but I advise you to practice writing directly in the terminal. To validate the line at the end of your command: press `Return`.**
</div>
## First commands
### First commands
```{r calculatorstep1, include=TRUE} ```{r calculatorstep1, include=TRUE}
1 + 100 1 + 100
``` ```
\
```R ```R
1 + 1 +
``` ```
The console displays `+`. The console displays `+`.
It is waiting for the next command. Write just `100` : It is waiting for the next command. Write just `100` :
```R ```R
100 100
``` ```
```{r calculatorstep2, echo=FALSE} ```{r calculatorstep2, echo=FALSE}
1 + 100 1 + 100
``` ```
### R keeps to the mathematical order ## R keeps to the mathematical order
```{r calculatorstep3, include=TRUE} ```{r calculatorstep3, include=TRUE}
3 + 5 * 2 3 + 5 * 2
``` ```
...@@ -175,13 +173,11 @@ It is waiting for the next command. Write just `100` : ...@@ -175,13 +173,11 @@ It is waiting for the next command. Write just `100` :
(3 + 5) * 2 (3 + 5) * 2
``` ```
\
```{r calculatorstep5, include=TRUE} ```{r calculatorstep5, include=TRUE}
(3 + (5 * (2 ^ 2))) # hard to read (3 + (5 * (2 ^ 2))) # hard to read
3 + 5 * (2 ^ 2) # if you forget some rules, this might help 3 + 5 * (2 ^ 2) # if you forget some rules, this might help
``` ```
\
**Note :** The text following a `#` is a comment. It will not be interpreted by R. In the future, I advise you to use comments a lot to explain in your own words what the command means. **Note :** The text following a `#` is a comment. It will not be interpreted by R. In the future, I advise you to use comments a lot to explain in your own words what the command means.
### Scientific notation ### Scientific notation
...@@ -189,7 +185,6 @@ It is waiting for the next command. Write just `100` : ...@@ -189,7 +185,6 @@ It is waiting for the next command. Write just `100` :
2/10000 2/10000
``` ```
`2e-4` is shorthand for `2 * 10^(-4)` `2e-4` is shorthand for `2 * 10^(-4)`
```{r calculatorstep7, include=TRUE} ```{r calculatorstep7, include=TRUE}
...@@ -210,16 +205,14 @@ log10(10) # base-10 logarithm ...@@ -210,16 +205,14 @@ log10(10) # base-10 logarithm
exp(0.5) exp(0.5)
``` ```
\
Compute the factorial of 9 (`9!`) Compute the factorial of 9 (`9!`)
```{r calculatorstep11, include=TRUE} ```{r calculatorstep11, include=TRUE}
9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
``` ```
\
or or
```{r calculatorstep12, include=TRUE} ```{r calculatorstep12, include=TRUE}
factorial(9) factorial(9)
``` ```
...@@ -228,38 +221,32 @@ factorial(9) ...@@ -228,38 +221,32 @@ factorial(9)
Comparisons can be made with R. The result will return a `TRUE` or `FALSE` value (`boolean` type). Comparisons can be made with R. The result will return a `TRUE` or `FALSE` value (`boolean` type).
equality (note two equal signs read as "is equal to") equality (note two equal signs read as "is equal to")
```{r calculatorstep13, include=TRUE} ```{r calculatorstep13, include=TRUE}
1 == 1 1 == 1
``` ```
inequality (read as "is not equal to") inequality (read as "is not equal to")
```{r calculatorstep14, include=TRUE} ```{r calculatorstep14, include=TRUE}
1 != 2 1 != 2
``` ```
less than less than
```{r calculatorstep15, include=TRUE} ```{r calculatorstep15, include=TRUE}
1 < 2 1 < 2
``` ```
less than or equal to less than or equal to
```{r calculatorstep16, include=TRUE} ```{r calculatorstep16, include=TRUE}
1 <= 1 1 <= 1
``` ```
greater than greater than
```{r calculatorstep17, include=TRUE} ```{r calculatorstep17, include=TRUE}
1 > 0 1 > 0
``` ```
\ \
<fieldset id='pencadre' style='text-align: left'> <fieldset id='pencadre' style='text-align: left'>
<legend style='border: 0px;'>Summary box</legend> <legend style='border: 0px;'>Summary box</legend>
<li> R is a programming language and free software environment for statistical <li> R is a programming language and free software environment for statistical
...@@ -271,9 +258,6 @@ software development.</li> ...@@ -271,9 +258,6 @@ software development.</li>
</fieldset> </fieldset>
\ \
\ \
# Variables and assignment # Variables and assignment
`<-` is the assignment operator in R. (read as left member take right member value) `<-` is the assignment operator in R. (read as left member take right member value)
...@@ -293,9 +277,6 @@ x ...@@ -293,9 +277,6 @@ x
You now see the `x` value in the environment box (*in red*). You now see the `x` value in the environment box (*in red*).
![](./img/RStudio_environment.png) ![](./img/RStudio_environment.png)
\
This **variable** is present in your work environment. You can use it to perform different mathematical applications. This **variable** is present in your work environment. You can use it to perform different mathematical applications.
...@@ -309,15 +290,12 @@ x <- 100 ...@@ -309,15 +290,12 @@ x <- 100
log(x) log(x)
``` ```
\
```{r VandAstep5, include=TRUE} ```{r VandAstep5, include=TRUE}
x <- x + 1 # x become 101 (100 + 1) x <- x + 1 # x become 101 (100 + 1)
y <- x * 2 y <- x * 2
y y
``` ```
\
A variable can be assigned a `numeric` value as well as a `character` value. A variable can be assigned a `numeric` value as well as a `character` value.
Just put our character (or string) between double quote `"` when you assign this value. Just put our character (or string) between double quote `"` when you assign this value.
...@@ -328,11 +306,9 @@ a <- "Hello world" # Multiple characters == String ...@@ -328,11 +306,9 @@ a <- "Hello world" # Multiple characters == String
a a
``` ```
\
```R ```R
x + z x + z
``` ```
\
How to test the type of the variable? How to test the type of the variable?
```{r VandAstep20, include=TRUE} ```{r VandAstep20, include=TRUE}
...@@ -352,7 +328,7 @@ They cannot start with a number nor contain spaces at all. ...@@ -352,7 +328,7 @@ They cannot start with a number nor contain spaces at all.
Different people use different conventions for long variable names, these include: Different people use different conventions for long variable names, these include:
``` ```r
periods.between.words periods.between.words
underscores_between_words underscores_between_words
camelCaseToSeparateWords camelCaseToSeparateWords
...@@ -360,21 +336,30 @@ camelCaseToSeparateWords ...@@ -360,21 +336,30 @@ camelCaseToSeparateWords
What you use is up to you, but be consistent. What you use is up to you, but be consistent.
\
<div id="pquestion"> Which of the following are valid R variable names?</div> <div id="pquestion"> Which of the following are valid R variable names?</div>
```{r eval=F, }
min_height
max.height
_age
.mass
MaxLength
min-length
2widths
celsius2kelvin
``` ```
<details><summary>Solution</summary>
<p>
```{r eval=F, }
min_height min_height
max.height max.height
_age # no
.mass .mass
MaxLength MaxLength
min-length # no
2widths # no
celsius2kelvin celsius2kelvin
``` ```
\ </p>
</details>
### Functions are also variables ### Functions are also variables
...@@ -385,7 +370,7 @@ logarithm <- log ...@@ -385,7 +370,7 @@ logarithm <- log
A R function can have different arguments A R function can have different arguments
``` ```r
function (x, base = exp(1)) function (x, base = exp(1))
``` ```
...@@ -393,8 +378,6 @@ function (x, base = exp(1)) ...@@ -393,8 +378,6 @@ function (x, base = exp(1))
- named arguments breaks the reading order - named arguments breaks the reading order
- named arguments make your code more readable - named arguments make your code more readable
\
To know more about the `log` function we can read its manual. To know more about the `log` function we can read its manual.
```{r VandAstep8, include=TRUE} ```{r VandAstep8, include=TRUE}
...@@ -407,21 +390,12 @@ or ...@@ -407,21 +390,12 @@ or
?log ?log
``` ```
\
This block allows you to view the different outputs (?help, graphs, etc.). This block allows you to view the different outputs (?help, graphs, etc.).
![](./img/formationR_VandAstep8_encadre.png) ![](./img/formationR_VandAstep8_encadre.png)
\
### A code editor ### A code editor
![](./img/RStudio_editor.png) ![](./img/RStudio_editor.png)
\
RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can
- click on the `Run button` above the editor panel, or - click on the `Run button` above the editor panel, or
...@@ -430,11 +404,8 @@ RStudio offers you great flexibility in running code from within the editor wind ...@@ -430,11 +404,8 @@ RStudio offers you great flexibility in running code from within the editor wind
If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made. If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made.
Copy your `function` into a `tp_1.R` file Copy your `function` into a `tp_1.R` file
\
We can define our own function with : We can define our own function with :
- function name, - function name,
...@@ -442,7 +413,7 @@ We can define our own function with : ...@@ -442,7 +413,7 @@ We can define our own function with :
- arguments, - arguments,
- `{` and `}` top open and close function, - `{` and `}` top open and close function,
``` ```R
function_name <- function(a, b){ function_name <- function(a, b){
...@@ -450,7 +421,7 @@ function_name <- function(a, b){ ...@@ -450,7 +421,7 @@ function_name <- function(a, b){
``` ```
- a series of operations, - a series of operations,
``` ```R
function_name <- function(a, b){ function_name <- function(a, b){
result_1 <- operation1(a, b) result_1 <- operation1(a, b)
result_2 <- operation2(result_1, b) result_2 <- operation2(result_1, b)
...@@ -460,7 +431,7 @@ function_name <- function(a, b){ ...@@ -460,7 +431,7 @@ function_name <- function(a, b){
- `return` operation - `return` operation
``` ```R
function_name <- function(a, b){ function_name <- function(a, b){
result_1 <- operation1(a, b) result_1 <- operation1(a, b)
result_2 <- operation2(result_1, b) result_2 <- operation2(result_1, b)
...@@ -468,12 +439,8 @@ function_name <- function(a, b){ ...@@ -468,12 +439,8 @@ function_name <- function(a, b){
} }
``` ```
\
<div id="pquestion">How to write a function to test if a number is even?</div> <div id="pquestion">How to write a function to test if a number is even?</div>
\
```{r VandAstep11, include=TRUE} ```{r VandAstep11, include=TRUE}
even_test <- function(x){ even_test <- function(x){
modulo_result <- x %% 2 # %% is modulo operator modulo_result <- x %% 2 # %% is modulo operator
...@@ -486,9 +453,7 @@ even_test(4) ...@@ -486,9 +453,7 @@ even_test(4)
even_test(3) even_test(3)
``` ```
**Note :** A function can write in several forms. **Note :** A function can be written in several forms.
\
No We can now clean your environment No We can now clean your environment
...@@ -513,8 +478,6 @@ rm(list = ls()) ...@@ -513,8 +478,6 @@ rm(list = ls())
ls() ls()
``` ```
\
<fieldset id='pencadre' style='text-align: left'> <fieldset id='pencadre' style='text-align: left'>
<legend style='border: 0px;'>Summary box</legend> <legend style='border: 0px;'>Summary box</legend>
<li> Assigning a variable is done with ` <- `.</li> <li> Assigning a variable is done with ` <- `.</li>
...@@ -522,13 +485,8 @@ ls() ...@@ -522,13 +485,8 @@ ls()
<li> Variable names can contain letters, numbers, underscores and periods. </li> <li> Variable names can contain letters, numbers, underscores and periods. </li>
<li> Functions are also variable and can write in several forms</li> <li> Functions are also variable and can write in several forms</li>
<li> An editing box is available on Rstudio.</li> <li> An editing box is available on Rstudio.</li>
</fieldset> </fieldset>
\
\
# Complex variable type # Complex variable type
### Vector (aka list) ### Vector (aka list)
...@@ -543,9 +501,6 @@ or ...@@ -543,9 +501,6 @@ or
c(1:5) c(1:5)
``` ```
\
\
A mathematical calculation can be performed on the elements of the vector: A mathematical calculation can be performed on the elements of the vector:
```{r Vecstep3, include=TRUE} ```{r Vecstep3, include=TRUE}
...@@ -558,9 +513,6 @@ x <- c(1:5) ...@@ -558,9 +513,6 @@ x <- c(1:5)
2^x 2^x
``` ```
\
\
To determine the type of the elements of a vector: To determine the type of the elements of a vector:
```{r Vecstep5, include=TRUE} ```{r Vecstep5, include=TRUE}
...@@ -578,15 +530,10 @@ x + 0.5 ...@@ -578,15 +530,10 @@ x + 0.5
is.vector(x) is.vector(x)
``` ```
\
\
```{r Vecstep8, include=TRUE} ```{r Vecstep8, include=TRUE}
y <- c(a = 1, b = 2, c = 3, d = 4, e = 5) y <- c(a = 1, b = 2, c = 3, d = 4, e = 5)
``` ```
\
We can compare the elements of two vectors: We can compare the elements of two vectors:
```{r Vecstep9, include=TRUE} ```{r Vecstep9, include=TRUE}
...@@ -595,10 +542,6 @@ y ...@@ -595,10 +542,6 @@ y
x == y x == y
``` ```
\
\
<fieldset id='pencadre' style='text-align: left'> <fieldset id='pencadre' style='text-align: left'>
<legend style='border: 0px;'>Summary box</legend> <legend style='border: 0px;'>Summary box</legend>
<li> A variable can be of different types : `numeric`, `character`, `vector`, `function`, etc.</li> <li> A variable can be of different types : `numeric`, `character`, `vector`, `function`, etc.</li>
...@@ -606,10 +549,6 @@ x == y ...@@ -606,10 +549,6 @@ x == y
<li> Do not hesitate to use the help box to understand functions! </li> <li> Do not hesitate to use the help box to understand functions! </li>
</fieldset> </fieldset>
\
\
# Packages # Packages
### Installing packages ### Installing packages
...@@ -621,8 +560,6 @@ or click on `Tools` and `Install Packages...` ...@@ -621,8 +560,6 @@ or click on `Tools` and `Install Packages...`
![](./img/formationR_installTidyverse.png) ![](./img/formationR_installTidyverse.png)
\
```R ```R
install.packages("ggplot2") install.packages("ggplot2")
``` ```
...@@ -633,8 +570,6 @@ install.packages("ggplot2") ...@@ -633,8 +570,6 @@ install.packages("ggplot2")
sessionInfo() sessionInfo()
``` ```
\
```{r packagesstep2, include=TRUE} ```{r packagesstep2, include=TRUE}
library(tidyverse) library(tidyverse)
``` ```
...@@ -654,6 +589,4 @@ unloadNamespace("tidyverse") ...@@ -654,6 +589,4 @@ unloadNamespace("tidyverse")
sessionInfo() sessionInfo()
``` ```
\
##See you to Session#2 : "Introduction to Tidyverse" ##See you to Session#2 : "Introduction to Tidyverse"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment