---
title: "Topic 1 - First steps"
author: "by Vianney DENIS"
date: "February 12, 2019"
output:
  slidy_presentation: default
  beamer_presentation: default
  ioslides_presentation: default
---

### Website

All documents for this class will be available at https://www.dipintothereef.com/. You need me to create you an access. I may have some trouble sometimes to upload everything, so sorry in advance if there is some delay.


### What is R?

R is:

+ a programming language
+ an environment for statistics (software)
+ open source software (free for everybody to use)
+ on cutting age: close to 4,000 third party add-ons

Homepage [here](http://www.r-project.org)

### R gallery

https://www.r-graph-gallery.com/all-graphs/

### Good resources

https://www.springer.com/series/6991?detailsPage=titles

### Structure of R software

R consists of:

+ core
+ add-on packages (CRAN, Bioconductor)

The core is the minimal installation and can be expanded using the add-on packages

### Download and install R

Go to [link](http://cran.r-project.org/bin/windows/base/) for windows system or [link](https://cran.r-project.org/) for mac. 

Run the installer, and follow the instructions, untick *'Save version number in registry'* 

R is a language, what you are installing is a graphical user interface (GUI) = a way to communicate. You have several GUI using R langage, the one you install is be default. We will later use [R studio](https://www.rstudio.com/).

### Check for update

```{r,  eval=FALSE}
if(!require(installr)) {
  install.packages("installr"); require(installr)} 
updateR()
```

This will start the updating process of your R installation.  It will check for newer versions, and if one is available, will guide you through the decisions you need to make 

<span style="color: red"> It will tell you if your R version is out-of-date </span>

### Install and use a package

Can be done using either using the menu "Packages" or by writing following code chunks:

+ Install the package </span> <span style="color: blue">```abc```</span>

```{r,  eval=FALSE}
install.packages("abc")
```

<span style="color: red"> It will tell you if your package is updated </span>

<span style="color: red"> To do only once, unless you remove or change of version of R </span>

+ Use the package <span style="color: blue">```abc```</span>

```{r,  eval=FALSE}
library("abc")
```

<span style="color: red">To do everytime: load your package </span>

### Find the correct package

Go to the [link](http://cran.r-project.org/web/views/)
), which use task views. It groups packages by subject areas (Environmetrics, multivariate, etc.)

Package are maintained regularly by authors, if not they are excluded from CRAN archives.


### <span style="color: green">Practice 1</span>

> *<span style="color: green"> Install and load the package </span>* <span style="color: blue">```vegan```</span>

### Help

+ R comes with built-in manuals and helps files

Third-party manuals guides and books are available at http://cran.r-project.org/other-docs.html, but frozen and frozen and no longer actively maintained

[R for beginner](https://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf) is a very good read for beginners

+ Using "Help > Html help", look for some help on the function <span style="color: blue">```median```</span>

### Calling for help

```{r,  eval=FALSE}
?median # or
help(median)
median
```

Help on the function <span style="color: blue">```median```</span>

```{r,  eval=FALSE}
?median
```

It will give you details on how to use the function <span style="color: blue">```median```</span>

```{r,  eval=FALSE}
??median
```
It will give you all functions with median in their description. You can also find several active [forums](http://stats.stackexchange.com) online. 

### Working directory

**Important**: use of working directory (wd) = file where you put all doc from a project

```{r,  eval=FALSE}
getwd() #Get Working Directory
```

Get the location of your current working directory

```{r,  eval=FALSE}
setwd() #Set Working Directory
```

Set up a new working directory. Also check "File>Change dir".

### Quit R

```{r,  eval=FALSE}
q()
```
It will leave your R session, do not save your workspace otherwise it will save all objects your create in R memory (.Rdata file). It will also save all commands given during the session (.Rhistory file). Those files can be opended by "File>Load workspace and file > Load history" 

### List objects

```{r,  eval=FALSE}
ls() # list
```

It will list all objects in memory

```{r,  eval=FALSE}
rm(list=ls())
# remove list
```

It will remove all objects in memory

**Tip**: Use CTRL+L to clean your screen

### R is a calculator

```{r,  eval=FALSE}
3+2 # addition
3-2 # substraction
3*2 # multiplication
3/2 # division
3^3 # power
log(2) # logarithm
exp(2) # exponential
(5 + 3) / 4 # define priority using () or {} 
pi*4 # common function
```

R is case sensitive <span style="color: blue">```pi```</span> will work, whereas <span style="color: blue">```Pi```</span> will not </span> 

### Text editor
Using "File>New Script"", open a script editor

This is the simple and built-in R script editor, you have plainty of alternatives. If R crash you may lose everything.

You can write you code inside, select the lines, and send them to the console using "Run lines or selection"

**Alternatives**: Notepad (Ctrl C + V), Tinn R, Sublime, WinEdt package <span style="color: blue">```rite```</span>, R Studio, and many others.

### Data preparation

**Always, always, always** keep simple: variable in column, observation in rows

No format, no color, no merging, no fancy decoration: be boring!

Format: most format can be read. ```.txt``` or ```.csv``` files represent usually the best choices 

### Reading data

Using the built-in script editor of R, you can practice importing data:

```{r,  eval=FALSE}
setwd ('D:/.../dataset')# set your working directory
read.table ("taiwan_coral.txt",header = TRUE,sep="\t",dec=".")# opening a simple datasheet
```

You can also target directly your file without setting up your working directory
 
```{r,  eval=FALSE}
read.table ("D:/.../Class 1/taiwan_coral.txt",header = TRUE,sep="\t", dec=".")# long version
read.table("taiwan_coral.txt", TRUE, "\t",".")# you can also shorten some part once you get use to it, but be careful when using this
```

Your data are not stored in R memory

### Storing, importing data

Now you can store it in R using the operator <span style="color: blue">```<-```</span>or <span style="color: blue">```=```</span> (to avoid at first)

```{r,  eval=FALSE}
taiwan_coral <- read.table ("taiwan_coral.txt", 	header = TRUE, sep="\t", dec=".")
```

<span style="color: blue">```read.delim```</span>
or
<span style="color: blue">```read.csv```</span> 
 and many others are alternative to
<span style="color: blue">```read.table```</span>  

Now, you can call your data by calling your object
<span style="color: blue">```taiwan_coral```</span> 

```{r,  eval=FALSE}
taiwan_coral
```

Always think about the name of your file, if it has column **titles**, the **separator** (tab, space, comma, semi-colon), the character use for **decimals** (dot, comma)

**CONGRATS**, this is your first script in R

### Foreign files

+ Using the package <span style="color: blue">```foreign```</span>: SPSS, SAS 

+ Using the package <span style="color: blue">```sas7bdat```</span>: SAS 

+ etc.

### ![](https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-125.png)

For this class, and especially if you are beginner in R you must use use [R studio](https://www.rstudio.com/). This will be the easiest for me to debug you if you have any problem. 

### <span style="color: green"> Practice 2 </span>

> *<span style="color: green"> Using the built-in script editor of R Studio ("File>New File>R Script"):</span>*  
*<span style="color: green">1) change your working directory to the one you created for this class</span>*  
*<span style="color: green">2) import the file <span style="color: blue">```rairuoho.txt```</span> (see below) in an object called rairuoho (extra: try importing the url of the file using:<span style="color:blue">```read.table```</span>)</span>*  
*<span style="color: green">3) using the help facilities, find out, how you could read the first 5 lines only (using the <span style="color: blue">```read.table```</span> function) </span>*  
*<span style="color: green">4) <span style="color: blue">```sum```</span> for this table, values from the 2nd column <span style="color: blue">```rairuoho```</span><span style="color: blue">```[```</span> no. line<span style="color: blue">```,```</span> no. column<span style="color: blue">```]```</span> </span>*  
*<span style="color: green"> 5) 'Knit' your script ("File>Knit Document") in <span style="color: blue">`html`</span> format and upload it below</span>*
