-
Notifications
You must be signed in to change notification settings - Fork 5
/
0000_install.Rmd
124 lines (91 loc) · 6.71 KB
/
0000_install.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
title: "Installing R and libaries"
description: Install R, RStudio, and useful libraries/packages.
output:
radix::radix_article:
toc: true
toc_depth: 3
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
R.options = list(width = 80),
tidy = TRUE,
tidy.opts = list(width.cutoff = 80))
```
```{r wrap-hook, include=FALSE}
# function that adds knitr parameter to control line width
# https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.Rmd
library(knitr)
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
# this hook is used only when the linewidth option is not NULL
if (!is.null(n <- options$linewidth)) {
x = knitr:::split_lines(x)
# any lines wider than n should be wrapped
if (any(nchar(x) > n)) x = strwrap(x, width = n)
x = paste(x, collapse = '\n')
}
hook_output(x, options)
})
```
Get source code for this RMarkdown script [here](https://github.com/hauselin/rtutorialsite/blob/master/0000_install.Rmd).
If you're new to R, follow the instructions below to install R and RStudio. Otherwise, check out my tutorials and [articles](articles.html).
Click **[here](https://github.com/hauselin/rtutorialsite/archive/master.zip)** to download everything on this site as a zip folder.
Alternatively, you can also clone or fork the [Github repository for this site](https://github.com/hauselin/rtutorialsite). On the Github page, click the green **Clone or download** button at the top right of the page to download everything you can find on this site. The RMarkdown scripts used to generate all the tutorials and posts have filenames that end with **.Rmd**.
![Download everything from [GitHub](https://github.com/hauselin/rtutorialsite)](./attachments/github.png)
## Consider being a patron and supporting my work?
[Donate and become a patron](https://donorbox.org/support-my-teaching): If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters.
## Installing (or updating) R and RStudio
1. Download R [here](https://www.r-project.org/). Follow the instructions under **Getting Started** to download R. Choose a mirror/location that is closest to you and download R for Linux, Mac, or Windows.
* R is a programming language and an environment. It doesn't look too good and isn't too user-friendly. We generally use RStudio as a pretty and user-friendly interface to R.
2. Download RStudio [here](https://www.rstudio.com/products/rstudio/download/) (the FREE version).
3. After installing both R and RStudio, run RStudio. We usually never use R itself and always run RStudio (RStudio relies on R). R is like your new but empty home (without furniture and quite unlivable unless you're a real minimalist) and RStudio are like furniture and decorations added to your house (R), making your house much more livable and even pretty. Thus, RStudio is useless without R, just like furniture and decorations are useless if you don't already have a house.
**Updating R and RStudio once every few months**
You should check whether there are newer versions of R and RStudio at least once every few months and install them if available. It's good practice to always use the latest versions. Just go to their websites and download and re-install them again.
## Installing R packages
Here's how to install R packages (also called libraries) that extend R's basic functionality. Below are a few packages I use regularly. Make sure you have the latest version of R and RStudio. To install packages, run the following code in RStudio. You only need to install any given package once (but remember to update them when newer versions are available).
I also have my own package [hausekeep](https://hauselin.github.io/hausekeep/), which has functions I use frequently.
which will also try to install a few of these packages if you don't already have them). To install all of them, first
You can install one package at a time or multiple at once. I suggest installing one at a time because it can be can be error-prone if you try to install too many packages at once because the warning/error messages can become difficult quite abstruse (if there are any).
Comments are preceded by the # symbol in R, and you can use # or #' for multi-line comments. R won't run any code that's preceded by the # symbol.
```r
# install packages all at once using c(), which allows you to combine/concatenate stuff
install.packages(c("tidyverse", "data.table",
"lme4", "lmerTest",
"ggbeeswarm", "egg"))
# install hausekeep package from my github
# install.packages("devtools")
devtools::install_github("hauselin/hausekeep") # you might have to install devtools first (see above)
#' or you can install one package at a time
#' I have commented out (added a # sign in front of a line)
#' the line of code below so I won't run it accidentally,
#' or it's to tell myself or others who are reading my code,
#' that I usually don't intend to run this line of code
#' unless I really want to.
# install.packages("tidyverse")
# install.packages("data.table")
```
Every time I start a new R session in RStudio, I usually run the code below at the top of my R script to load all these libraries all at once using `library()`.
```r
library(tidyverse); library(data.table);
library(lme4); library(lmerTest);
library(ggbeeswarm); library(egg); library(hausekeep)
```
### More about these packages
- [data.table](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html)
- powerful, extremely fast, and simple way to manipulate dataframes/datatables
- [tidyverse](https://www.tidyverse.org/) packages (e.g., dplyr, tidyr, ggplot2, stringr)
- manipulate dataframes (and tibbles) easily with tidyr, dplyr
- make beautiful plots with clean syntax (ggplot2)
- [lme4](https://www.jaredknowles.com/journal/2013/11/25/getting-started-with-mixed-effect-models-in-r)
- fit linear mixed effects models
- [lmerTest](https://cran.r-project.org/web/packages/lmerTest/index.html)
- compute probability values and degrees of freedom for mixed effects models fitted using lme4 package (uses Satterthwaite approximation)
- [ggbeeswarm](https://github.com/eclarke/ggbeeswarm)
- produces beautiful and informative ggplots that show distributions of datapoints
- [hausekeep](https://hauselin.github.io/hausekeep/)
- miscellaneous functions I've written over the years
- [egg](https://cran.r-project.org/web/packages/egg/vignettes/Overview.html)
- arrange multiple plots in one ggplot figure
## Support my work
[Support my work and become a patron here](https://donorbox.org/support-my-teaching)!