A whistlestop tour of the R language and its capabilities
- How to install R
- Install base r
pacaur -S r
- Run R in sudo you need sudo here as we're going to check what's been set up
sudo R
- Check where R is going to install its packages
Sys.getenv()
R_LIBS_USER
is where your packages go by default when you're not sudo,R_HOME
is where they go when you are. This is important because there are a number of ways of installing packages in R, and not all of them allow you to specify where to put them! If you want to change any of these locations, then go ahead and just change these environment variables interactively or in your.rprofile
file.
There are a number of ways to install packages.
- From interactive R, you can install open source packages stored on R's CRAN repository.
> R
> install.packages('devtools')
- Using the additional
devtools
package, you can install from other sources (including github)
> devtools::install_github('kater-s/intro_to_r')
- From the terminal, use
R CMD INSTALL
. This is useful if you want to script an install, or install from local (packages you have made).
R CMD INSTALL --help
If you want to follow this demo along, I'm using: R version 3.4.0 The following packages:
- tidyverse (note one of the dependencies here requires gcc-fortran. Don't ask. Really, don't)
- nycflights13
- readr
R started life as a stats language. Because it's pretty easy to get started, and because it contains a lot of pre-written statistical stuff, lots of statiticians use it. As their data requirements have grown, so have the capabilities of R. Some of these things are good, and some are not!
If you want to try out some stats or even just get your hands on some numeric data, R has a lot of this pre-installed, or you can use external packages:
> data()
> library(nycflights13)
> library(dplyr)
> head(mtcars)
> head(diamonds)
> head(flights)
You can also read in data from various files
library(readr)
read_csv("~/trainingMaterials/london.csv")
write_csv(mtcars, "~/trainingMaterials/mtcars.csv)
This just makes trying things out and learning stats really quick and easy. Happy face.
R allows you to be able to view the data that you have in memory as you're developing.
> mycars <- mtcars
> View(mycars)
> mtcars_refactored <- mutate(mtcars```
### stats
Understandably, R is really good for stats. Any methods you would ever want to use are there. Many also follow a convention:
> model_name(formula, data = data)
e.g.
lm(x ~ y, data = mydata)
## things I don't like about R
- a lot of the base packages (because they're old) are now not the best way of doing things (e.g. `plot` vs `ggplot`)
- a lot of code is littered with side-effects. This is OK, but people using these do not know about them (e.g. `acf`)
- most good programming practice isn't obvious/taught to noobs (even though it's easier)
- there are some new nifty things that I don't thing should be in R
### good programming practices
There are ways to write pretty good R code. I'll get on to how often people do this later, but they are there. These include things like:
- the reference class (RC) class system and environments
- function argument and return conventions (see the `tidyverse` for a lot of these)
- function argument assertions (`assert_that`, `assertive`, `checkmate`...)
- linting (`lintr`)
- test frameworks (`testthat`)
- test coverage statistics (`covr`)