-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGN.Rmd
382 lines (241 loc) · 7.45 KB
/
GN.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
---
title: "Introduction to Scientific Programming and Simulation 2016"
author: "Gerhard Nachtmann"
date: "`r Sys.Date()`"
output: html_document
---
## Catching up with R: Base R
The code from our catch-up-session can be found in file `2016-10-17.R`,
the transcript including in- and output is here: `2016-10-17.Rt`.
### Data structures
atomic vector (corresponding `NA`)
+ logical (`NA`)
+ integer (`NA_integer_`)
+ double (numeric) (`NA_real_`)
+ character (`NA_character_`)
typeof
list
```{r list}
vector(mode = "list", length = 3)
```
data.frame
trees
`NULL`, `NA`, `NaN`
round
paste
seq_along(letters)
seq_len(4)
gl
http://adv-r.had.co.nz/Data-structures.html
### Subsetting / Indexing
`subset()`
`[]`
`%in%`
### functions
vectorization
imperial2metric
### control structures
if
ifelse vectorized
### loops
Explicit
+ `for`
+ `while`
Implicit
`lapply` and friends, which are much faster
inch2cm
paste
```{r loops}
s1 <- function(x){
res <- 0
for(i in seq_along(x)){
res <- res + x[i]
}
res
}
s2 <- function(x){
res <- 0
i <- 1
while(i <= length(x)){
res <- res + x[i]
i <- i + 1
}
res
}
cs1 <- function(x){
res <- 0
resv <- vector(mode = "numeric", length = length(x))
for(i in seq_along(x)){
res <- res + x[i]
resv[i] <- res
}
resv
}
```
### Your turn: own function in loop
### tidy data
http://vita.had.co.nz/papers/tidy-data.html
http://blog.rstudio.org/2014/07/22/introducing-tidyr/
## Reproducible research
The source from our reproducibility session can be found in file `Report.Rmd`.
To create an HTML document as output, just use `make html` from the command line
or use `rmarkdown::render('Report.Rmd', 'html_document')` within your R-session.
For the latter ensure, that `Report.Rmd` is in your working directory. Otherwise
you could use the path to this file within `render`, or set the working directory
to the directory, where the source file is stored.
As usual, the transcript including in- and output is here: `2016-10-20.Rt`.
Reader: http://kbroman.org/steps2rr/
Make reports using [knitr](http://yihui.name/knitr).
Formatting with [RMarkdown](http://rmarkdown.rstudio.com/).
Write code and text in one document to produce final output including graphics.
Features:
+ Lists
+ emphasize font
+ **bold** (\*\*)
+ *italics* (\*)
+ ~~strikethrough~~ (\~\~)
+ Links
Links:
https://nicercode.github.io/guides/reports/
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
R-Chunks
+ `echo`: show code
+ `eval`: evaluate code
+ `fig`: show figure
+ insert chunk in RStudio: Ctrl+Alt+i
+ inline chunk in backticks with `r`
`purl("document.Rmd")` extracts R-code
Include `ggplot` e.g.
```{r eval = FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, col = Petal.Width)) +
geom_point() + geom_smooth(method = "lm") + facet_wrap(~Species)
```
## R-packages
[Book by Hadley](http://r-pkgs.had.co.nz/)
library(devtools)
Package structure
### Documentation using roxygen.
https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html
library(roxygen2)
Tools - Project options - Build tools
Ctrl+Shift+Alt+R
In cpp file:
```
//' @export
// [[Rcpp::export]]
```
In any R file
```
#' @importFrom Rcpp evalCpp
#' @useDynLib <packagename>
```
The R-Package can be built within RStudio or using the commandline (e.g. to implement it in a Makefile):
```{r, engine="bash", RCMD, eval=FALSE}
R CMD build firstpackage
R CMD check firstpackage_0.1.0.tar.gz
```
If you want to make it online available for other users, create a
github repository, then add remote origin to local repository:
```{r, engine="bash", gitremote, eval=FALSE}
git remote add origin https://github.com/nachti/firstpackage.git
```
If you forked my package to your own Github account, then you have to change
the username to be able to add and commit and push changes:
```{r, engine="bash", gitremotownuser, eval=FALSE}
git remote add origin https://github.com/<youruser>/firstpackage.git
git pull origin master # get updates from upstream
git push origin master # put your changes online
```
The package can be found in its own repository here:
https://github.com/nachti/firstpackage
It can be installed using
```{r instfpkg, eval=FALSE}
devtools::install_github("nachti/firstpackage")
```
### data.table
fread
data.table package extends data.frame especially for large data.
It is quite fast due to optimized code.
https://github.com/Rdatatable/data.table/wiki
Binary search in `data.table`
merge using `[`
The code from our data.table session can be found in file `2016-10-20.R`.
As usual, the transcript including in- and output is here: `2016-10-20.Rt`.
### aggregate
with(x, aggregate(v, by = list(group), sum))
## Batch mode, makefile, version control
R CMD build
R CMD check
Reader: http://kbroman.org/minimal_make/
### Git
https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
```{r, engine="bash", gitconf, eval=FALSE}
git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global core.editor emacs
```
http://r-pkgs.had.co.nz/git.html
First clone the Repository:
```{r, engine="bash", git1, eval=FALSE}
git clone https://github.com/nachti/ISPS16.git
git pull origin master # if something changed
```
### use Makefile
Make documentation using [pandoc](http://pandoc.org/):
```{r, engine="bash", make1, eval=FALSE}
make html # just produce html
make # produces pdf, html, docx
```
Make pdf etc. from Report.Rmd:
```{r, engine="bash", makereport, eval=FALSE}
make FILE=Report
```
### Rscript
Create R Script `i2cm.R` and tell the system, where Rscript is living:
```{r Rscript, eval=FALSE}
#!/usr/bin/Rscript
### first line tells your system where *Rscript* is located
library(firstpackage)
example("inch2cm")
```
Make it executable for everyone using `chmod 755 i2cm.R` and use it in dfferent ways:
```{r, engine="bash", rscriptexamples, eval=FALSE}
Rscript i2cm.R # works even if i2cm.R is not made executable
./i2cm.R # send output to console
R CMD BATCH i2cm.R # send input and output to file i2cm.Rout
nohup ./i2cm.R & # send output to nohup.out
nohup ./i2cm.R > log.out & # send output to log.out
```
If you use `nohup ./i2cm.R > log.out &` to run your script, it's possible to logoff the system while the calculations are done.
### Hints for Mac users
Ensure that pandoc is installed.
Possibly R, pandoc etc. are not exported, so
create the file `.bash_profile ` in your home directory:
```{r, engine="bash", mac, eval=FALSE}
touch ~/.bash_profile
```
Open it using any texteditor and add the following lines:
```{r, engine="bash", mac_bp, eval=FALSE}
export PATH=$PATH:/Library/Frameworks/R.framework/Resources/bin
export PATH=$PATH:/Applications/RStudio.app/Contents/MacOS/pandoc
```
## shiny
You can find the code of our shiny session in the `shiny` folder in this repository.
### some links
http://shiny.rstudio.com/
http://shiny.rstudio.com/tutorial/
[Shiny tutorial by Dean Attall](http://deanattali.com/blog/building-shiny-apps-tutorial/)
[Shiny tutorial by Paul Hiemstra](http://stcorp.nl/R_course/tutorial_shiny.html)
https://github.com/rstudio/shiny-examples
http://shiny.rstudio.com/gallery/
https://www.r-bloggers.com/building-shiny-apps-an-interactive-tutorial/
[drinkR](https://gallery.shinyapps.io/drinkr/)
[Shiny Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2016/01/shiny-cheatsheet.pdf)
### ui
sidebarPanel
+ selectInput
+ checkboxInput
+ radioButtons
+ sliderInput
### server
R-code for output