generated from labordynamicsinstitute/presentation-r-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.Rmd
354 lines (221 loc) · 6.32 KB
/
index.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
---
title: "Working with Restricted Access and Big Data"
author:
- "Lars Vilhuber"
- "David Wasser"
date: "`r Sys.Date()`"
output:
ioslides_presentation:
incremental: false
self-included: true
widescreen: true
---
# Credits
Based on an earlier presentation and tutorial at the [Cornell Day of Data 2021](https://labordynamicsinstitute.github.io/day-of-data-2021/).
## Overview
::: {.columns-2}
:::: {.column}
Part 1:
- Ideal directory and data structure
- Adapting to confidential / big data
Part 2:
- Secure coding techniques
- Using templates for reproducibility
::::
:::: {.column}
Part 3:
- Techniques to handle data extracts / API use
Part 4:
- Documenting what you did
::::
:::
# But first...
## Version your code and your results
- Even in restricted environment, use versioning
- If available, use `git`
- If not available, request `git`
- If not, use regular backups (scripted, automated)
# Part 1 | Ideal structure
## Generic project setup
![TIER protocol](images/tier-protocol.png)
[TIER Protocol](https://www.projecttier.org/tier-protocol/specifications-3-0/)
## Basic project setup
::: {.columns-2}
:::: {.column}
**Structure your project**
- Data inputs
- Data outputs
- Code
- Paper/text/etc.
::::
:::: {.column}
**Version your project (`git`)!**
**Track metadata**
- cite articles you reference
- *cite* data sources you use
::::
:::
## Project setup examples
::: {.columns-2}
:::: {.column}
```
/inputs
/outputs
/code
/paper
```
::::
:::: {.column}
```
/datos/
/brutos
/limpiados
/finales
/codigo
/articulo
```
::::
:::
It doesn't really matter, as long as it is logical. We will get to how this translates to confidential or big data in a moment!
# Computational Empathy
## Consider how the next person will (be able to) compute
- You don't know who that is
- You don’t know what they don’t know
- Will not have any of your add-on packages/ libraries/ etc. pre-installed
- Don’t force them to do tedious things
It might be "Future You!"
## Streamlining
- Master script preferred
- Least amount of manual effort
- No manual copying of results
- dynamic documents!
- Write out/save tables and figures using packages
- Clear instructions
- No manual install of packages
- Use a script to create all directories, install all necessary packages/requirements/etc.
## Reproducibility
- No manual manipulation
- “Change the parameter to 0.2, then run the code again”
- Use *functions*, ado files, programs, macros, subroutines
- Use *loops*, parameters, *parameter files* to call those subroutines
- Use *placeholders* (globals, macros, libnames, etc.) for common locations ($CONFDATA, $TABLES, $CODE)
- Compute all numbers in package
- No manual calculation of numbers
- Use cross-platform programming practices
## Cross-platform programming practices 1
**Use programming-language specific code as much as possible**
Avoid
```{r, eval=FALSE}
system("unzip C:\data\myfile.zip")
```
or
```{stata, eval=FALSE}
shell unzip "C:\data\myfile.zip"
```
## Cross-platform programming practices 1
Most languages have appropriate code:
R:
```{r, eval=FALSE}
unzip(zipfile, files = NULL, list = FALSE, overwrite = TRUE,
junkpaths = FALSE, exdir = ".", unzip = "internal",
setTimes = FALSE)
```
Stata:
```{stata, eval=FALSE}
unzipfile "zipfile.zip" [, replace]
```
## Cross-platform programming practices 2
Use neutral pathnames (mostly forward slashes)
::: {.columns-2}
:::: {.column}
**R**: Use functions to combine paths (and/or use forward slashes), packages to make code more portable.
<div class="red2">
```
basepath <- rprojroot::find_root(rprojroot::has_file("README.md"))
data <- read.dta(file.path(basepath,"path","data.dta"))
```
</div>
::::
:::: {.column}
**Stata**: *always* use forward slashes, even on Windows
<div class="blue2">
```
global data "/my/computer"
use "$data/path/data.dta"
```
</div>
::::
:::
# Data structure when data is confidential
## Back to the TIER protocol
![TIER Protocol again](images/tier-protocol-2.png)
## Back to the TIER protocol
![TIER Protocol again](images/tier-protocol-home.png)
## When data are big/in the cloud
![TIER Protocol Big data](images/tier-bigdata.png)
## When data are confidential
![TIER Protocol Confidential](images/tier-confidential.png)
## When data are confidential
![TIER Protocol Confidential](images/tier-confidential2.png)
## Project setup examples
::: {.columns-2}
:::: {.column}
This may no longer work:
```
/datos/
/brutos
/limpiados
/finales
/codigo
/articulo
```
::::
:::: {.column}
```
/proyecto/
/datos/
/brutos
/limpiados
/finales
/codigo
/articulo
/secretos (read-only)
/impuestos (read-only)
/salarios (read-only)
```
::::
:::
## Stata configuration files {.smaller}
File structure thus becomes more complex, but fundamentally not so different:
```{stata, eval=FALSE}
global taxdata "/secretos/impuestos"
global salarydata "/secretos/salarios"
global outputdata "/proyecto/datos/limpiados" // this is where you would write the data you create in this project
global results "/proyecto/articulo" // All tables for inclusion in your paper go here
global programs "/proyecto/codigo" // All programs (which you might "include") are to be found here
```
# Exercise 1-1
## Set up a project structure
<div class="blue3">
> Follow the lesson learned here and create a basic project structure
> 1. FORK the following repository: [labordynamicsinstitute/test-part-1-1](https://github.com/labordynamicsinstitute/test-part-1-1)
> 2. Populate it with the directory structure
> 3. Push to your Github repository (your own fork)
> Did that work?
</div>
## Populate the project structure
<div class="blue3">
> 4. Add a README describing the purpose of each directory
> 5. Push to your Github repository
> Did that work?
</div>
# Exercise 1-2
## Make a portable repository
<div class="blue3">
Once you are done, at most one line can be changed to make it run!
> 1. FORK the following repository: [labordynamicsinstitute/test-part-1-2](https://github.com/labordynamicsinstitute/test-part-1-2)
> 2. Modify the code, either Stata or R.
> 3. Push to your Github repository (your own fork)
</div>
Do you think your code will work on somebody else's computer or in the cloud?
# Next: [Part 2](part2.html)