-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide_r_environment.Rmd
271 lines (191 loc) · 6.42 KB
/
slide_r_environment.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
---
title: "Working in the R environment"
subtitle: "R Foundations for Life Scientists"
author: "Marcin Kierczak"
keywords: bioinformatics, course, scilifelab, nbis, R
output:
xaringan::moon_reader:
encoding: 'UTF-8'
self_contained: false
chakra: 'assets/remark-latest.min.js'
css: 'assets/slide.css'
lib_dir: libs
include: NULL
nature:
ratio: '4:3'
highlightLanguage: r
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%/%total%"
---
exclude: true
count: false
```{r,echo=FALSE,child="assets/header-slide.Rmd"}
```
<!-- ------------ Only edit title, subtitle & author above this ------------ -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, width=60)
```
```{r,include=FALSE}
# load the packages you need
#library(dplyr)
#library(tidyr)
#library(stringr)
#library(ggplot2)
library(mkteachr)
```
---
name: working_with_r
# Working with R
There are several ways to work with/in R:
- from a command line,
- in batch mode,
- from a native GUI,
- using external editor, e.g. RStudio.
During this course, we will be focusing on working with [RStudio](https://www.rstudio.com) and also in *batch mode*.
---
name: cline_working
# Working from command line
1. Open Terminal.
2. Type `R`.
3. Type R commands...
4. Type `q()` to quit R.
- Arrows let you browse throughout the history.
- .kbd[TAB] attempts to autocomplete the command you have just started typing.
---
name: batch_mode
# The batch mode
If you are working on a computational cluster, such as the Uppmax, it is very likely you would like to run large jobs that one has to enqueue. This makes interactive work from the console virtually impossible. The solution is to run R code from a file, using the so-called **batch mode**:
1. Create a file with your code and give it extension **.R**.
2. In the console (or in the queue script) write:
`R --vanilla < mycode.R` [two minus signs in front of *vanilla*].
Should you like to log the output add either:
- `R --vanilla < mycode.R > output.log` or like this
- `R --vanilla < mycode.R | tee output.log`
---
name: help
# Getting help
```{r help,echo=TRUE,eval=FALSE}
help(t.test) # function level
?t.test # same as above
??t.test # extensive search
vignette("GenABEL") # package level demo(graphics)
example(barplot) # run help examples for barplot
demo() # see all currently available demos
demo('graphics') # run demo for 'graphics'
```
[Stackoverflow](http://stackoverflow.com) is a great resource.
---
name: work_with_packages
# Working with packages
Packages are organised in repositories. The three main repositories are:
* [CRAN](https://cran.r-project.org)
* [R-Forge](http://r-forge.r-project.org)
* [Bioconductor](http://www.bioconductor.org)
We also have [GitHub](https://github.com).
--
# Working with packages -- CRAN example.
```{r,out.width="400pt",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/cran-package.png")
```
---
name: pkg_cran_inst
# Working with packages -- installation
Only a few packages are pre-installed:
```{r pkg.err.ex,eval=TRUE,error=TRUE}
library(XLConnect)
```
In order to install a package from command line, use:
```{r pkg.inst,eval=FALSE}
install.packages("GenABEL",dependencies=TRUE)
```
---
name: work_pkg_details
# Working with packages -- details
It may happen that you want to also specify the repository, e.g. because it is geographically closer to you or because your default mirror is down:
```{r pkg.inst.repo,eval=FALSE}
install.packages('GenABEL',dependencies=TRUE,repos="http://cran.se.r-project.org")
```
But, sometimes, this does not work either because the package is not available for your platform. In such case, you need to *compile* it from its *source code*.
---
name: work_pkg_details2
# Working with packages -- details cted.
```{r,out.width="400pt",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/cran-package.png")
```
---
name: source_pkg_inst
# Working with packages -- installing from source.
- Make sure you have appropriate tools installed, e.g. XCode or build-essentials.
- Download the source file, in our example *GenABEL_1.8-0.tar.gz*.
- Install it:
```{r pkg.inst.src,eval=FALSE}
install.packages("path/to/GenABEL_1.8-0.tar.gz",
repos=NULL,
type='source',
dependencies=TRUE)
```
- Load it:
```{r pkg.load,eval=FALSE}
library('GenABEL') # always forces reloading
require('GenABEL') # load only if not already loaded
```
- Enjoy!
---
name: pkg_github
# Packages -- GitHub
Nowadays, more and more developers contribute their packages via GitHub. The easiest way to install packages from the GitHub is via the *devtools* package:
- Install the *devtools* package.
- Load it.
- Install.
- Enjoy!
```{r pkg.inst.devtools.github,eval=FALSE}
install.packages('devtools',dependencies=TRUE)
library('devtools')
install_github('talgalili/installr')
```
---
name: pkg_bioconductor
# Packages -- Bioconductor
```{r,out.width="200pt",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/logo_bioconductor.png")
```
First install Bioconductor Manager:
```{r inst.biocond,eval=FALSE}
if (!requireNamespace("BiocManager",quietly = TRUE))
install.packages("BiocManager")
```
---
name: pkg_bioconductor2
# Packages -- Bioconductor cted.
Now, you can install particular packages from Bioconductor:
```{r biocond.inst.pkg,eval=FALSE}
BiocManager::install("GenomicRanges")
```
For more info, visit [Bioconductor website](http://www.bioconductor.org/install/).
---
# One package to rule them all -- the magic of `renv`
- first time do `renv::activate()` and `renv::init()`
- while working: `renv::hydrate()` and `renv::snapshot()`
Now, send `renv.lock` to your friend to share the environment and she can:
- restore the environment `renv::restore()`
**Pure magic!**
---
name: rstudio
# RStudio -- a live demonstration
![RStudio screenshot](data/slide_r_environment/RStudio.png)
<!-- --------------------- Do not edit this and below --------------------- -->
---
name: end_slide
class: end-slide, middle
count: false
# See you at the next lecture!
```{r, echo=FALSE,child="assets/footer-slide.Rmd"}
```
```{r,include=FALSE,eval=FALSE}
# manually run this to render this document to HTML
#rmarkdown::render("presentation_demo.Rmd")
# manually run this to convert HTML to PDF
#pagedown::chrome_print("presentation_demo.html",output="presentation_demo.pdf")
```