# User customizations

## Every time R is started check the working directory
   
The working directory is the directory (aka folder) where by default R expects to find all the files.  By default, it is the directory where you start R from the console unless it is specified in your preferences.

In general, the working directory should be your project directory.  You should set it once in your session.


There are two commands to find out the current working directory and to specify a new directory:

* `getwd()` which returns the path of the current working directory
* `setwd(dir)` which changes the working directory to `dir` which contains the path name of the new working directory; for example to set the working directory to the `Desktop` folder on a Mac, one would use `setwd("~/Desktop")`

We recommend that you use a single directory (folder) for related work (such as a class or research project), and then use subdirectories (subfolders) for individual pieces (such as homeworks or experiments/studies).  For example for the BIOE805 class one might have the following structure:

    /Users/username - bioe805 - hw1
                              - week1
                              - hw2
                              - week2
                              - finalProject

To work on the final project one would use `setwd("/Users/username/bioe805/finalProject")` (replace `/Users/username` by your home directory path); on a Mac this would be simply `setwd("~/bioe805/finalProject")`.

## Installing packages

One of the advantages of using R is being able to use packages that others have written for specific, advanced, often cutting-edge tasks.  This is an area where R excels, specially for statistics and data science.  A large number of individuals have written packages implementing advanced statistical and computational methods.  

One advantage of using these packages is that they are all open source, so you can actually (with some effort) see exactly what the package is doing.  If you want a feature that the author did not provide, you can implement it yourself.  Or, if you find a bug, you can fix it yourself, or suggest the fix to the original author.

A disadvantage of using the packages is that there is no central authority co-ordinating and vetting the packages (this can also be seen as a plus).  Unlike, commercial packages such as SAS, or SPSS, functions from different packages may or may not co-ordinate.  Help functions can be incomplete or confusing, or the package maintainer may have dropped off and not respond to questions or requests.

There are three primary ways to install packages.

### CRAN (Comprehensive R Archive Network)

[CRAN](https://cran.r-project.org/) is the official R repository for source, binaries and [packages](https://cran.r-project.org/web/packages/index.html).  All packages go through a vetting procedure which involves that the code meets some requirements.  The repository has a number of mirrors to ensure that the repository is available to users at all times.  

You can browse the [list of packages](https://cran.r-project.org/web/packages/available_packages_by_name.html), or you can examine them by content area, or [task views](https://cran.r-project.org/web/views/).

To install a package called [`vioplot`](https://CRAN.R-project.org/package=vioplot) that makes violin plots (we will learn about this later), you would use (notice quotes surroinding the package name):

```{r}
install.packages("vioplot")
```

To see what functions are available in that package, and to get an overview use this command (notice no quotes around the package name this time):

```{r}
library(help=vioplot)
```

### Bioconductor

[Bioconductor](https://www.bioconductor.org/) is another repository for R packages, mainly meant for the "analysis and comprehension of high-throughput genomic data".  To install Bioconductor and Bioconductor, follow their [installation instructions](https://www.bioconductor.org/install/).  Bioconductor is a layer on top of base R, and has its own requirements which have to be satisfied before the packages can be registered.

### Github (and other code repositories)

Many packages first appear informally on code repositories such as [Github](https://github.com); these repositories allow easy sharing of code, and the developers can get feedback before putting them on a more official place like CRAN or Bioconductor.  Some developers may never submit to CRAN.  For such packages, you have to install the `devtools` package first.

```{r}
install.packages("devtools")
```

Then you can install the package using the syntax `install_github(username/package)`.  So, to install the `knitr` package by the user `yihui` you would use:

```{r}
devtools::install_github("yihui/knitr")
```

## Recommended cutomizations to be performed once

Each time R starts, two files are read for adjusting the startup parameters:  

* `Renviron.site` (or `.Renviron` in your home directory) to set environment variables; use this to set where packages you wish to install should be stored.
* `Rprofile.site` (or `.Rprofile` in your home directory) runs lines of R code in that file; use this to set the default CRAN mirror, for example.

You should edit the `.Rprofile` or `'Renviron` files in your home directory to permanently set your personal preferences.

For most people, the following two customizations would be sufficient.


### Package directory

The package directory is where all the additional packages you download are stored.

The command `.libPaths()` will tell you where R is searching or packages.

```{r eval=FALSE, echo=TRUE}
.libPaths()
```

This is what it might looks like:   

* On Windows:       

```R
[1] "C:/Users/UserName/Documents/R/win-library/4.0" "C:/Program Files/R/R-4.0.0/library"
```

* On Linux:   

```R
[1] "/home/username/R/x86_64-pc-linux-gnu-library/4.0" "/usr/local/lib/R/site-library"                   
[3] "/usr/lib/R/site-library"                          "/usr/lib/R/library" 
```

<!-- #region -->



-Except for the first one, all the others are system directories, where I may not have access to.  The first one is my personal library of R packages which I can freely modify.  To do that, I put the following line in my `.Rprofile` file in my home directory.
<!-- #endregion -->

```{r eval=FALSE, echo=TRUE}
.libPaths("~/Rlibs")
```

### CRAN repository

To download packages you have to also specify the mirror from where you would download packages.  It is generally preferable to have a location which is near you.  You can look at the list of [CRAN mirrors](https://cran.r-project.org/mirrors.html) to pick your favorite one.  To pick the one at UTK, put this like in your `.Rprofile`.

```{r eval=FALSE, echo=TRUE}
local({
  r <- getOption("repos")
  r["CRAN"] <- "https://mirrors.nics.utk.edu/cran/"
  options(repos = r)
})
```
