- **`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:


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


### The same console as before (in Red box)
## The same console as before (in Red box)


# 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` :
@@ -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.).


\
### A code editor
### A code editor


\
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 :