-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.4-PP_Rscript.qmd
228 lines (165 loc) · 4.47 KB
/
3.4-PP_Rscript.qmd
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
---
title: "Parallel Processing Methods"
subtitle: "Rscript"
author:
- Elizabeth King
- Kevin Middleton
format:
revealjs:
theme: [default, custom.scss]
standalone: true
self-contained: true
logo: QMLS_Logo.png
slide-number: true
show-slide-number: all
code-annotations: hover
---
## Introduction to Parallelization in R
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
library(tidyverse)
library(cowplot)
library(readxl)
library(viridis)
library(parallel)
library(parallelly)
library(furrr)
ggplot2::theme_set(theme_cowplot(font_size = 18))
```
1. Within a single R script
- Base R: `parallel`
- [parallely](https://parallelly.futureverse.org/)
- [foreach](https://github.com/RevolutionAnalytics/foreach) and `%dopar%` from `doParallel`
- [future](https://www.futureverse.org/) and [furrr](https://furrr.futureverse.org/)
2. Using bash to run many R scripts via a scheduler (e.g., SLURM)
## Using bash to run many R scripts
- Use `.R` not `.Rmd`
- Run the script from the shell
- Pass inputs & collect outputs
- Consider number of files
- Sometimes base R will behave preferably compared to tidyverse
## Non-parallel
```{r}
#| echo: true
MM <- read_excel("Data/Jackals.xlsx")
obs <- MM |> filter(Sex == "F") |> summarize(m = mean(Mandible)) |> pull(m) -
MM |> filter(Sex == "M") |> summarize(m = mean(Mandible)) |> pull(m)
set.seed(3850234) # <1>
nreps <- 1e4
diffs <- numeric(length = nreps)
diffs[1] <- obs
for (ii in 2:nreps) { # <2>
Rand_Sex <- sample(MM$Sex)
diffs[ii] <- mean(MM$Mandible[Rand_Sex == "F"]) -
mean(MM$Mandible[Rand_Sex == "M"])
}
#two-tailed test
round(mean(abs(diffs) >= abs(diffs[1])), 4) # <3>
```
1. Setting different seeds for iterations
2. Splitting up iterations
3. Keeping and combining the output
## `Rscript`
```{bash}
#| echo: true
Rscript my_R_file.R variables >console_output.txt 2>errors_and_warnings.txt &
```
- You can run R from the shell with the command `Rscript`
- Any variables you pass come in as characters
- `as.numeric()` if you need numbers
- Allows you to start many R processes
## Setting up the script
```{r}
#| echo: true
#| eval: false
args=(commandArgs(TRUE)) # <1>
MM <- read_excel("Data/Jackals.xlsx")
set.seed(as.numeric(args[1])) # <2>
iter_id <- args[2] # <3>
nreps <- 100 # <4>
diffs <- numeric(length = nreps)
for (ii in 1:nreps)
{
Rand_Sex <- sample(MM$Sex)
diffs[ii] <- mean(MM$Mandible[Rand_Sex == "F"]) -
mean(MM$Mandible[Rand_Sex == "M"])
}
saveRDS(file = paste0("/Output/jackal_iters_", iter_id, ".Rds")) # <5>
```
1. take arguments from bash
2. seed will be passed to script
3. track which set this is
4. do 100 iterations per core
5. keep the output as a Rds file (keeps R attributes)
## Setting up the bash commands
```{r}
#| echo: true
#| eval: false
set.seed(87239)
ncores <- 10
iter_ids <- 1:ncores
seeds <- sample(1:10000000, ncores)
cat("", file = "jackal_cmds.txt", append = FALSE)
for(jj in 1:ncores)
{
cat(paste0("Rscript Jackals_iter.R ",
seeds[jj], " ",
iter_ids[jj],
" >temp", jj,".txt",
" 2>error", jj,".txt",
" \n"),
file = "jackal_cmds.txt",
append = TRUE)
}
```
## Setting up the bash commands
```{bash}
#| echo: true
#| eval: false
head jackal_cmds.txt
```
```{r}
#| echo: false
#| eval: true
set.seed(87239)
ncores <- 10
iter_ids <- 1:ncores
seeds <- sample(1:10000000, ncores)
for(jj in 1:ncores)
{
cat(paste0("Rscript Jackals_iter.R ",
seeds[jj], " ",
iter_ids[jj],
" >temp", jj,".txt",
" 2>error", jj,".txt",
" \n"))
}
```
## Running the commands
## Processing the results
```{r}
#| echo: true
#| eval: false
MM <- read_excel("Data/Jackals.xlsx")
obs <- MM |> filter(Sex == "F") |> summarize(m = mean(Mandible)) |> pull(m) -
MM |> filter(Sex == "M") |> summarize(m = mean(Mandible)) |> pull(m)
ncores <- 10
iter_ids <- 1:ncores
nreps <- 100
alldiffs <- rep(NA, ncores*nreps)
counter <- 1
for(jj in 1:ncores)
{
diffs <- readRDS(file = paste0("/Output/jackal_iters_", iter_ids[jj], ".Rds"))
alldiffs[counter:(counter + 99)] <- diffs
counter <- counter + 100
}
empP <- mean(abs(alldiffs) >= abs(obs))
```
## Learn more
- [Software Carpentry](https://software-carpentry.org/lessons/)
- [RSS Help Pages](https://docs.rnet.missouri.edu/)
- [High-Performance and Parallel Computing with R](https://cran.r-project.org/web/views/HighPerformanceComputing.html)