-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusefulcodesnippets.Rmd
388 lines (246 loc) · 10.5 KB
/
usefulcodesnippets.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
382
383
384
385
386
387
388
---
title: "Useful code snippets for everyday tasks"
description: |
Helpful bits of code for life
author:
- name: Murray Cadzow
url:
affiliation: University of Otago
affiliation_url:
date: "2021-07-07"
output:
distill::distill_article:
toc: yes
css: style.css
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
Time: 90 min
Description: This session will cover useful code snippets that are designed to improve your overall experience doing computational research. This will include how to customise your BASH and R environments, useful keyboard short cuts, and short pieces of code to do common tasks e.g. read a directory of files into R.
# R
## Increasing Efficiency
RStudio is the most popular environment for using R and so learning some keyboard shortcuts can make your life much nicer and prevent typos
Useful keyboard shortcuts:
- `alt` + `shift` + `K`: keyboard shortcut to display common keyboard shortcuts
- `ctrl` + `shift` + `M`: insert magrittr pipe
- `alt` + `-`: assignment arrow `<-`
- `ctrl` + `alt` + `I`: insert R code chunk in Rmarkdown script
- `ctrl` + `.`: jump to file/function
- `ctrl` + `shift` + `.`: navigate through open scripts
- `ctrl` + `shift` + `F10`: restart R
### Rprofile
```{r, eval = FALSE}
# Load helper packages if using interactive session (doesn't alter your environment)
# Don't add 'analysis' packages here
if (interactive()) {
suppressMessages(require(devtools))
suppressMessages(require(usethis))
suppressMessages(require(testthat))
}
# set CRAN
options(repos = c(CRAN = "https://cloud.r-project.org/"))
# warn on partial matches
options(
warnPartialMatchArgs = TRUE,
warnPartialMatchDollar = TRUE,
warnPartialMatchAttr = TRUE
)
# fancy quotes are annoying and lead to
# 'copy + paste' bugs / frustrations
options(useFancyQuotes = FALSE)
```
## RMarkdown
### Standard template:
Below is the standard template I use for creating an Rmarkdown document.
It creates a floating table of contents, and lets you toggle the code on or off, and also dates the report for when it is made.
The first code chunk sets my default of echoing all of my code. The second will load the `tidyverse` which for me is pretty much always used and the `quitely = FALSE` means that I don't get the usual loading messages about conflicts coming through into my document.
````
---
title: a cool title
author: Murray Cadzow
date: "`r knitr::inline_expr("Sys.Date()")` "
output:
html_document:
toc: true
toc_float: true
toc_depth: 4
code_folding: "show" # "hide" if code less important for audience
---
```{r setup, include=FALSE}`r ''`
knitr::opts_chunk$set(echo = TRUE)
```
```{r}`r ''`
suppressMessages(library(tidyverse))
```
````
### Rmarkdown tricks
Here are a couple of other Rmarkdown tricks that can be useful. The [RMarkdown cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/) is a more comprehensive resource for these though.
#### Selective evaluation of lines within a code chunk
In rmd the code chunk `eval` option takes more than just `TRUE` or `FALSE` - if you want to selectively run lines within a rmd code chunk you can exclude them explicitly:
````
```{r, eval = c(-1,-3)}`r ''`
1
3
5 # only this line will evaluate
```
````
```{r, eval = c(-1,-3), echo = TRUE}
1
3
5 # only this line will evaluate
```
#### Creating verbatim code chunks
Sometimes, such as in the creation of this workshop, there is a need to show the code chunk verbatim. There is a section in the rmarkdown book about this and is extremely useful -
[https://bookdown.org/yihui/rmarkdown-cookbook/verbatim-code-chunks.html](https://bookdown.org/yihui/rmarkdown-cookbook/verbatim-code-chunks.html)
To create verbatim code chunks, add `` `r knitr::inline_expr(" ''")` `` after the `{r}` part of the code chunk.
### Making better tables
The `knitr` package provides a function to make alright looking standard tables with options for custom column names, alignments and rounding.
```{r}
library(knitr)
mtcars %>% kable(caption = "A better table from knitr::kable")
```
```{r}
mtcars %>%
select(1:4) %>%
head() %>%
kable(caption = "A table caption",
col.names = c("MPG", "Cylinders", "Displacement","Horse Power"))
```
`kableExtra` brings in extra table styling. Although this website formatting prevents this displaying as it should in a normal RMarkdown document. Check out [https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) for the documentation and examples for `kableExtra`.
```{r}
library(kableExtra)
mtcars %>%
select(1:4) %>%
head() %>%
kbl(caption = "A table caption",
col.names = c("MPG", "Cylinders", "Displacement","Horse Power")) %>%
row_spec(0, angle = -45) %>%
kable_styling(bootstrap_options = "striped")
```
## Workflow advice
- Rprojects
Read in a directory of files
```{r, eval = FALSE}
library(tidyverse)
files <- list.files(pattern = "*.csv", full.names = TRUE)
my_csvs <- map(files, read_csv)
```
- `here`
```{r, eval = FALSE}
library(here)
here()
```
- `janitor`
```{r, eval = FALSE}
library(janitor)
clean_names()
```
- `usethis`
```{r, eval = FALSE}
library(usethis)
create_project(path = "path/to/new/project") # creates a new rstudio project and opens it
use_r(name = "new_r_script") # creates a new script with the name provided
edit_r_profile() # opens your Rprofile so you can edit it
```
quickly find out the number of blank entries in a column:
```{r, eval = FALSE}
table(is.na(df$colname))
```
replace blank cells in a data.frame with NA:
```{r, eval = FALSE}
data[data == ""] = NA
```
Reorder a vector
```{r}
x <- c("b", "c", "a")
# sorts the vector then returns the indices
order(x)
x[order(x)]
df <- data.frame(x = rep(1:3, each = 2), y = 6:1, z = letters[1:6])
```
format your numbers into a fixed width (turns from numeric to character)
```
# returns character type of number rounded to 3 decimal places
sprintf('%.3f', 0.123456)
```
format **all** numeric columns to 3 decimal places and make into a table
```{r}
library(tidyverse)
mtcars %>%
head() %>%
mutate(across(where(is.numeric), list(~sprintf('%.3f', .) ) )) %>%
kableExtra::kbl()
```
*tidy* way to transpose a dataframe/tibble
```{r}
mtcars %>%
head() %>%
tibble::rownames_to_column() %>% # may or may not be needed
tidyr::pivot_longer(-rowname,
names_to = "var",
values_to = "value") %>%
tidyr::pivot_wider(names_from = "rowname",
values_from = "value")
```
pull out the nth string after a string split
```
purr::map_chr(stringr::str_split(string_vec, "pattern"), n)
```
```{r purrr}
purrr::map_chr(stringr::str_split(c("chr1","chr2","chr3"), "chr"), 2) # you would get back c("1","2","3")
```
# Bash
## Configuration
### .bashrc or .zshrc
BASH is a common UNIX commandline or terminal, but for MacOS the default is zsh.
To find out which shell you are using enter this command:
```
echo $0
```
Creating your .bashrc or .zsh
Bash profile for login
`.bashrc` is the common file that controls your bash set up and usually can be found at `~/.bashrc`. Some systems (such as MacOS) also have a file `.bash_profile`. If your system uses the `.bash_profile` file, you can make it refer to `.bashrc` by having this as the contents of `.bash_profile`:
```
[[ -r ~/.bashrc ]] && . ~/.bashrc
```
In the `.bashrc` file it is useful to set a customised prompt, set variables that are useful - e.g. PATH to define where bash looks for installed software - and set up some custom commands (aliases) to make common tasks easier.
Custom prompt
Creating your own prompt in bash can be really useful rather than having a straight `$`. http://ezprompt.net provides a nice way of modifying your prompt and providing the code to add to your `.bashrc`.
Things you might want to do:
- add your username
- add the hostname (the name of the computer)
- add the current directory
- add the full path to the current directory
- have colour
### Exported variables
Exporting variables is a useful way for defining environmental settings. Often this is setting a bash variable to tell programs where to look for things. This website has a few examples of bash variables (https://www.thegeekstuff.com/2010/08/bash-shell-builtin-commands/).
It is useful to include the RSTUDIO_PANDOC variables below.
Rmarkdown Pandoc
A useful one on the server, is defining where R is going to look for pandoc for compiling RMarkdown documents.
I have the following in my .bashrc file
```
export RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc
```
But the location is likely different on your computer. In R, use the command `rmarkdown::find_pandoc()` to find out where the RStudio version of pandoc is located.
Setting this in your `.bashrc is important because there might be another instance of pandoc that is available on your PATH and might cause issues if you run R from the commandline. RSTUDIO_PANDOC is the name that R has specified to use if you want to customise which pandoc is used.
### Better bash history
Bash records your history as it goes but if you are operating across multiple windows it doesn’t work the way you would hope for - e.g. it is only recorded from a single given session, even if you work in multiple. PROMPT_COMMAND is a bash variable that is run as part of running commands. This particular one is designed to time and date stamp commands (not run as root) and their working directory into a daily log file. The logs live in `~/.logs/` so this needs to be made for the command to run `mkdir -p ~/.logs`.
```
export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'
```
If I want to search my logs I can use `grep <command> ~/.logs/*` and it will tell me all the times and directories I ran a command, and how I ran it. The history in these log files is made up of all commands you run on the computer, regardless of how many terminal windows you have open.
Aliases
If you have commands that you're always typing out such as `ls -lrth` it can be useful to create an `alias` for the command that is shorter and easier to type out.
e.g.
```
alias ll="ls -lrth"
```
This can be written in your `.bashrc` so that you can use them in new sessions. These can be very useful but remember they are only available on machines you have been able to customise your `.bashrc`.
# Python
Virtual environments
- conda
- conda create
- conda activate
- conda deactivate