-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_rrcompendium.qmd
1382 lines (978 loc) · 46.9 KB
/
09_rrcompendium.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Creating a research compendium with rrtools"
subtitle: "Research Compendia"
---
# Background
- **Research is increasingly computational**
- **Code and data are important research outputs**
- yet, we still focus mainly on curating papers.
- **Calls for openness**
- stick: reproducibility crisis
- carrot: big rewards from working open
Yet we lag in conventions and technical infrastructure for such openness.
<br>
## Enter the Research Compendium
> The goal of a research compendium is to provide a **standard** and easily recognizable way for **organizing the digital materials** of a project to enable others to **inspect, reproduce, and extend the research**.
## Three Generic Principles
1. **Organize its files according to prevailing conventions**:
- help other people recognize the structure of the project,
- supports tool building which takes advantage of the shared structure.
2. **Separate of data, method, and output**, while making the relationship between them clear.
3. **Specify the computational environment** that was used for the original analysis.
# R community response
> R packages can be used as a research compendium for organising and sharing files!
## R package file system structure for reproducible research
- Take advantage of the power of convention.
- Make use of great package development tools.
See Ben Marwick, Carl Boettiger & Lincoln Mullen (2018) [*Packaging Data Analytical Work Reproducibly Using R (and Friends)*](https://www.tandfonline.com/doi/abs/10.1080/00031305.2017.1375986?journalCode=utas20), The American Statistician, 72:1, 80-88, DOI: \<10.1080/00031305.2017.1375986\>
![images: [*Kartik Ram: rstudio::conf 2019 talk*](https://github.com/karthik/rstudio2019)](assets/reproducible-data-analysis-10.png){fig-align="center" width="400"}
# R Developer Tools
## Leverage tools and functionality for **R package development**
- manage dependencies
- make functionality available
- document functionality
- validate functionality
- version contol your project
[`devtools`](https://www.tidyverse.org/articles/2017/11/usethis-1.0.0/), [`usethis`](https://www.tidyverse.org/articles/2017/11/usethis-1.0.0/), [`rrtools`](https://github.com/benmarwick/rrtools), [Rstudio](https://support.rstudio.com/hc/en-us/articles/200486488-Developing-Packages-with-RStudio)
## ***minimal analysis project***
1. An **`scripts/`** directory that contains R scripts (`.R`), notebooks (`.qmd`), and intermediate data.
2. A **`DESCRIPTION`** file that provides metadata about the compendium. Most importantly, it would list the packages needed to run the analysis. Would contain field to indicate that this is an analysis, not a package.
## A ***reproducible*** analysis project might also contain:
1. An **`R/`** directory which contains R files that provide high-stakes functions.
2. A **`data/`** directory which contains high-stakes data.
3. A **`tests/`** directory that contains unit tests for the code and data.
4. A **`vignettes/`** directory that contains high-stakes reports.
## Autogenerated components:
1. A **`man/`** directory which contains **roxygen2-generated documentation** for the reusable functions and data.
2. Online documentation in a **`docs/`** folder.
## A ***shareable*** reproducible analysis project would also:
- Use Git + GitHub (or other public Git host)
- Use GitHub Actions or other continuous integration service
- Capture the computational environment so it can easily be recreated on a different computer. This involves at a minimum capturing package versions, but might also include capturing R version, and other external dependencies.
## Start small and build as necessary
![images: [*Kartik Ram: rstudio::conf 2019 talk*](https://github.com/karthik/rstudio2019)](assets/reproducible-data-analysis-11.png){fig-align="center" width="400"}
![images: [*Kartik Ram: rstudio::conf 2019 talk*](https://github.com/karthik/rstudio2019)](assets/reproducible-data-analysis-12.png){fig-align="center" width="400"}
![images: [*Kartik Ram: rstudio::conf 2019 talk*](https://github.com/karthik/rstudio2019)](assets/reproducible-data-analysis-13.png){fig-align="center" width="400"}
# `rrtools`: Research Compendia in R
> The goal of rrtools is to provide **instructions, templates, and functions** for making a **basic compendium** suitable for writing **reproducible research with R**.
**`rrtools` build on tools & conventions for R package development to**
- organise files
- manage dependencies
- share code
- document code
- check and test code
**`rrtools` extends and works with a number of R packages:**
- [`devtools`](https://cran.r-project.org/package=devtools): functions for package development
- [`usethis`](https://www.tidyverse.org/articles/2017/11/usethis-1.0.0/): automates repetitive tasks that arise during project setup and development
- [`Quarto`](https://quarto.org/docs/authoring/front-matter.html): facilitates writing of Scientific and Technical Documents.
<br>
------------------------------------------------------------------------
# Practical: Create a Research Compendium
## Practical Aims and Objectives
In this section we'll **use materials associated with a published paper** (text, data and code) to **create a research compendium** around it.
By the end of the workshop, you should be able to:
- **Create a Research Compendium** to manage and share resources associated with an academic publication.
- **Produce a reproducible manuscript from a single rmarkdown document**.
- Appreciate the power of convention!
# Create your first reproducible research compendium
Let's go ahead and create our first reproducible research compendium!
## Copy **rrcompendium** Project
In our shared space click on the {{< fa plus-square >}} copy button next to the **rrcompendium** Project.
The project already has the `rrtools` package and all the necessary dependencies installed.
::: {.callout-tip collapse="true"}
## Installing required packages locally
If you are working locally, you would need to **install the following packages starting with `rrtools`**.
```{r, eval=FALSE, code=readLines("setup/install_rrtools.R")}
```
You might also need to install `tinytex` (if you get errors when rendering your qmd paper).
:::
# Create compendium
Now that we've got a project to work in, let's start by **creating a blank research compendium** for us to work in.
We use function **`rrtools::create_compendium`**.
Because we are already in the project we want to create the compendium in, we can just call the function without any arguments.
```{r}
#| eval: false
rrtools::create_compendium()
```
_But see below for information on how to create a compendium locally in a new directory._
::: {.callout-note collapse="true"}
## Creating a Research Compendium in a new directory locally
Locally we could supply a path at which our compendium will be created. The final part of our path becomes the compendium name. Because the function effectively creates a package, only a **single string of lowercase alpha characters is accepted as a name**, for example, `rrcompendium` as the final part of our path.
To **create `rrcompendium` on my desktop I might use:**
```{r, eval=FALSE}
rrtools::create_compendium("~/Desktop/rrcompendium")
```
:::
If the call was successfull you should see the following console output:
```
✔ Setting active project to '/cloud/project'
✔ Writing 'LICENSE'
✔ Writing 'LICENSE.md'
✔ Adding '^LICENSE\\.md$' to '.Rbuildignore'
✔ Creating 'README.Rmd' from template.
✔ Adding 'README.Rmd' to `.Rbuildignore`.
• Modify
✔ Adding code of conduct.
✔ Creating 'CONDUCT.md' from template.
✔ Adding 'CONDUCT.md' to `.Rbuildignore`.
✔ Adding instructions to contributors.
✔ Creating 'CONTRIBUTING.md' from template.
✔ Adding 'CONTRIBUTING.md' to `.Rbuildignore`.
✔ Adding .binder/Dockerfile for Binder
✔ Creating '.binder'
✔ Creating '.binder/Dockerfile' from template.
✔ Adding '.binder/Dockerfile' to `.Rbuildignore`.
• Modify
✔ Adding 'here' pkg to Imports
✔ Creating 'analysis' directory and contents
✔ Creating 'analysis'
✔ Creating 'analysis/paper'
✔ Creating 'analysis/figures'
✔ Creating 'analysis/templates'
✔ Creating 'analysis/data'
✔ Creating 'analysis/data/raw_data'
✔ Creating 'analysis/data/derived_data'
✔ Creating 'analysis/supplementary-materials'
✔ Creating 'references.bib' from template.
✔ Creating 'paper.qmd' from template.
Next, you need to: ↓ ↓ ↓ ↓
• Write your article/report/thesis, start at the paper.qmd file
• Add the citation style library file (csl) to replace the default provided here, see https://github.com/citation-style-language/
• Add bibliographic details of cited items to the 'references.bib' file
• For adding captions & cross-referencing in an qmd, see https://quarto.org/docs/authoring/cross-references.html
• For adding citations & reference lists in an qmd, see https://quarto.org/docs/authoring/footnotes-and-citations.html
Note that:
⚠ Your data files are tracked by Git and will be pushed to GitHub
This project was set up by rrtools.
You can start working now or apply some more basic configuration.
Check out https://github.com/benmarwick/rrtools for an explanation of all the project configuration functions of rrtools.
```
## Initiate `git`
Let's initialise our compendium with `.git`. We'll need to configure git again as this is a new Rstudio cloud project.
```{r, eval=FALSE}
# configure git
usethis::use_git_config(user.name = "Jane",
user.email = "jane@example.org")
# intialise git and commit
usethis::use_git()
```
```
✔ Initialising Git repo
✔ Adding '.Rhistory', '.Rdata', '.httr-oauth', '.DS_Store', '.quarto' to '.gitignore'
There are 12 uncommitted files:
* '.binder/'
* '.gitignore'
* '.Rbuildignore'
* 'analysis/'
* 'CONDUCT.md'
* 'CONTRIBUTING.md'
* 'DESCRIPTION'
* 'LICENSE'
* 'LICENSE.md'
* 'NAMESPACE'
* ...
Is it ok to commit them?
1: No
2: Nope
3: Definitely
Selection: 3
```
Let's agree to commit the files and also to restart Rstudio.
Let's also change the default branch to the more modern convention of `main` instead of `master`. We can use `usethis::git_default_branch_rename()`.
```{r}
#| eval: false
usethis::git_default_branch_rename()
```
```
ℹ Local branch 'master' appears to play the role of the default branch.
✔ Moving local 'master' branch to 'main'.
• Be sure to update files that refer to the default branch by name.
Consider searching within your project for 'master'.
```
![](assets/master-to-main.png)
# Inspect templates
`rrtools::create_compendium()` creates the **bare backbone of infrastructure required for a research compendium**. At this point it provides facilities to store general metadata about our compendium (eg bibliographic details to create a citation) and manage dependencies in the `DESCRIPTION` file and store and document functions in the `R/` folder. Together these allow us to **manage, install and share functionality associated with our project**.
```
.
├── CONDUCT.md <- .............................code of conduct
├── CONTRIBUTING.md <- .........................instructions for
| contributors
├── DESCRIPTION <- .............................package metadata
| dependency management
├── LICENSE <- .................................license files
├── LICENSE.md
├── NAMESPACE <- ...............................AUTO-GENERATED on build
├── README.Rmd <- ..............................High level documentation
├── analysis
│ ├── data
│ │ ├── DO-NOT-EDIT-ANY-FILES-IN-HERE-BY-HAND
│ │ ├── derived_data
│ │ └── raw_data
│ ├── figures
│ ├── paper
│ │ ├── paper.qmd
│ │ └── references.bib
│ ├── supplementary-materials
│ └── templates
│ ├── author-info-blocks.lua
│ ├── journal-of-archaeological-science.csl
│ ├── pagebreak.lua
│ ├── scholarly-metadata.lua
│ ├── template.Rmd
│ └── template.docx
└── project.Rproj
```
# Get session materials
Today we'll be working with **a subset of materials from the published compendium** of code, data, and author's manuscript:
> Carl Boettiger. (2018, April 17). *cboettig/noise-phenomena: Supplement to: "From noise to knowledge: how randomness generates novel phenomena and reveals information"* (Version revision-2). *Zenodo*. <http://doi.org/10.5281/zenodo.1219780>
accompanying the publication:
> Carl Boettiger [![](https://orcid.org/sites/default/files/images/orcid_16x16.png)](https://orcid.org/0000-0002-1642-628X). *From noise to knowledge: how randomness generates novel phenomena and reveals information*. Published in *Ecology Letters*, 22 May 2018 <https://doi.org/10.1111/ele.13085>
<img src="assets/Boettiger-2018.png" heigth="250px" width="400px"/>
## Create `attic` directory
Let's first create a folder to dowload the materials into. It's sometimes useful to have a folder to hold materials that are not formally part of the project (i.e. we will not commit these files to git). I like to call such folders `attic`, so let's create such a folder
```{r}
#| eval: false
fs::dir_create("attic")
```
Let's also add the `attic/` dir to `.gitignore` and `.buildignore` (used to ignore files which should not be included as part of an installed R package) using `usethis` functions.
```{r}
usethis::use_git_ignore("attic")
usethis::use_build_ignore("attic")
```
Let's also commit the changes to `,gitignore` and `.buildignore`.
## Download materials
You can **download the materials using `usethis::use_course()`** and supplying a path to a destination folder to argument `destdir`. Let's download everything into the `attic/` folder.
```{r, eval=FALSE}
usethis::use_course(url = "bit.ly/rrtools_wks", destdir = "attic")
```
```
✔ Downloading from 'https://bit.ly/rrtools_wks'
Downloaded: 0.43 MB
✔ Download stored in 'attic/rrtools-wkshp-materials-master.zip'
✔ Unpacking ZIP file into 'rrtools-wkshp-materials-master/' (7 files extracted)
Shall we delete the ZIP file ('rrtools-wkshp-materials-master.zip')?
1: Yes
2: Nope
3: Absolutely not
Selection:
Enter an item from the menu, or 0 to exit
Selection: 1
✔ Deleting 'rrtools-wkshp-materials-master.zip'
```
This will **download** everything we need from a GitHub repository as a `.zip` file, **unzip** it and launch it in a **new Rstudio session** for us to explore.
## Inspect materials
The follwing files should now be contained in your `attic/rrtools-wkshp-materials-master` folder
```
├── README.md <- .......................materials README
├── analysis.R <- ......................analysis underlying paper
├── gillespie.csv <- ...................data
├── paper.pdf <- .......................LaTex pdf of the paper
├── paper.txt <- .......................text body of the paper
└── refs.bib <- ........................bibtex bibliographic file
```
In this workshop we'll attempt **a partial reproduction of the original paper** using the materials we've just downloaded.
We'll **use this as an opportunity to create a new research compendium using `rrtools` and friends!** :confetti:
------------------------------------------------------------------------
# Update `DESCRIPTION` file
Let's **update some basic details in the `DESCRIPTION` file**:
```
Package: project
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
ByteCompile: true
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports: here
Suggests: devtools,
git2r
```
<br>
## Package name
To get correct the limitation of working in Posit Cloud, were all projects are called `project`, let's correct the `Package:` field and **rename our package to `rrcompendium`**.
```
Package: rrcompendium
```
## Title
Let's also **give our compendium a descriptive title**:
```
Title: Partial Reproduction of Boettiger Ecology Letters 2018;21:1255–1267
with rrtools
```
## Version
We don't need to change the version now but using [semantic versioning](https://semver.org/) for our compendium can be a really useful way to track versions. In general, **versions below `0.0.1` are in development**, hence the `DESCRIPTION` file defaults to `0.0.0.9000`.
## Authors
Next let's **specify the author of the compendium**. Edit with **your own details**.
```
Authors@R:
person(given = "Anna",
family = "Krystalli",
role = c("aut", "cre"),
email = "annakrystalli@googlemail.com")
```
For more details on specifying authors, check documentation for `?person`
## Description
Let's **add a bit more detail about the contents of the compendium** in the Description.
```
Description: This repository contains the research compendium of the
partial reproduction of Boettiger Ecology Letters 2018;21:1255–1267.
The compendium contains all data, code, and text associated with this sub-section of the analysis
```
## Date
Add a `Date` field to the `DESCRIPTION` file to indicate when the compendium was created.
```
Date: 2024-04-19
```
## License
Finally, let's **update the license for the material we create with copyright holder details**. We'll continue using the [**MIT license**](https://opensource.org/licenses/MIT) already created (_Note however that **his only covers the code**_). We can do this with:
```{r, eval=FALSE}
usethis::use_mit_license("Anna Krystalli")
```
```
Overwrite pre-existing file 'LICENSE'?
1: No
2: Definitely
3: Not now
Selection: 2
✔ Writing 'LICENSE'
Overwrite pre-existing file 'LICENSE.md'?
1: Yup
2: Absolutely not
3: Not now
Selection: 1
✔ Writing 'LICENSE.md'
```
This overwrites the current files `LICENSE` and `LICENSE.md` and updates the `DESCRIPTION` file, embedding our name in the details of the license.
## Recap
We've finished updating our `DESCRIPTION` file! :tada:
It should look a bit like this:
```{r, code=readLines("appendices/rrcompendium/DESCRIPTION")}
#| eval: false
#| filename: "DESCRIPTION"
```
and your project folder should contain:
```
.
├── CONDUCT.md
├── CONTRIBUTING.md
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── NAMESPACE
├── README.Rmd
├── analysis
│ ├── data
│ │ ├── DO-NOT-EDIT-ANY-FILES-IN-HERE-BY-HAND
│ │ ├── derived_data
│ │ └── raw_data
│ ├── figures
│ ├── paper
│ │ ├── paper.qmd
│ │ └── references.bib
│ ├── supplementary-materials
│ └── templates
│ ├── author-info-blocks.lua
│ ├── journal-of-archaeological-science.csl
│ ├── pagebreak.lua
│ ├── scholarly-metadata.lua
│ ├── template.Rmd
│ └── template.docx
├── attic
│ └── rrtools-wkshp-materials-master
│ ├── README.md
│ ├── analysis.R
│ ├── gillespie.csv
│ ├── paper.pdf
│ ├── paper.txt
│ └── refs.bib
└── project.Rproj
```
Let's commit our work and move on to preparing our compendium for sharing on GitHub.
# Create `paper.qmd` from template
When we first created our compendium, a `paper.qmd` file was created in the `analysis/paper` directory. This file is a template for a reproducible manuscript that we can use to write up our analysis and also serve to demonstrate how a reproducible paper should e set up.
However, the template is quite generic and we want to take advantage of the fact that Quarto offers templates for specific journals.
Given the original paper was published in Ecology Letters, an Elsevier journal, we'll use the [`elsevier` quarto template](https://github.com/quarto-journals/elsevier).
## Delete current `paper/` directory
First, let's **delete the current `paper/` directory** and its contents.
```{r}
#| eval: false
fs::dir_delete("analysis/paper")
```
## Create new `paper.qmd` from template
Next let's move over to the Terminal and use the `quarto` CLI to create a new `paper.qmd` file from the `elsevier` template.
```{bash}
#| eval: false
#| filename: Terminal
quarto use template quarto-journals/elsevier
```
```
Quarto templates may execute code when documents are rendered. If you do not
trust the authors of the template, we recommend that you do not install or
use the template.
? Do you trust the authors of this template (Y/n) › Yes
```
When asked whether you trust the authors of the template, select `Yes`.
Next, **you'll be asked to provide a name for the new directory** the new `paper.qmd` will be created in. **Specify: `analysis/paper`.**
```{bash}
#| eval: false
#| filename: Terminal
? Directory name: › analysis/paper
```
```
[✓] Downloading
[✓] Unzipping
Found 1 extension.
[✓] Copying files...
Files created:
- paper.qmd
- placeholder.png
- _extensions
- bibliography.bib
- style-guide
```
This creates the following files and directories in the `analysis/paper` directory:
```
analysis/paper
├── _extensions/
├── bibliography.bib
├── paper.qmd
├── placeholder.png
└── style-guide/
```
## Copy materials into project
Let's also copy some files we'll be using in our project from the materials we downloaded into our `attic/`. In particular, we need to copy:
- `gillespie.csv` (the paper data) into `analysis/data`
- `refs.bib` (the paper references) into `analysis/paper`
We can do this programmatically using the `fs` package.
```{r}
#| eval: false
fs::file_copy(
"attic/rrtools-wkshp-materials-master/gillespie.csv",
"analysis/data"
)
fs::file_copy(
"attic/rrtools-wkshp-materials-master/refs.bib",
"analysis/paper"
)
```
Commit changes to the project.
## Inspect `paper.qmd`
The `paper.qmd` file is a template for a reproducible manuscript that we can use to write up our analysis. It contains a lot of information about the features of the `elsevier` template and how to use it.
Let's open the new `paper.qmd` file, take a look at the contents and render it.
```{r}
#| echo: false
knitr::include_graphics("assets/pdfs/elsevier-tmpl.pdf")
```
# Complete paper Front Matter
Let's start by **completing the front matter of the `paper.qmd` file**.
## Add title and author details
Let's start with the `title` and `author` fields.
First delete the following lines:
```yaml
title: Short Paper
subtitle: A Short Subtitle
author:
- name: Alice Anonymous
email: alice@example.com
affiliations:
- id: some-tech
name: Some Institute of Technology
department: Department Name
address: Street Address
city: City
state: State
postal-code: Postal Code
attributes:
corresponding: true
note: This is the first author footnote.
- name: Bob Security
email: bob@example.com
affiliations:
- id: another-u
name: Another University
department: Department Name
address: Street Address
city: City
state: State
postal-code: Postal Code
note: |
Another author footnote, this is a very long footnote and it should be a really long footnote. But this footnote is not yet sufficiently long enough to make two lines of footnote text.
- name: Cat Memes
email: cat@example.com
affiliations:
- ref: another-u
note: Yet another author footnote.
- name: Derek Zoolander
email: derek@example.com
affilations:
- ref: some-tech
```
and replace them with information we can find in the downloaded materials, and in particular the `paper.txt` file. We'll complete the information with the orginal author details.
```yaml
title: "From noise to knowledge: how randomness generates novel phenomena and reveals information"
author:
- name: "Carl Boettiger"
email: "cboettig@berkeley.edu"
affiliations:
- id: a
address: "Dept of Environmental Science, Policy, and Management, University of California Berkeley, Berkeley CA 94720-3114, USA"
attributes:
corresponding: true
```
## Add abstract and keywords
Next, let's replace the `abstract` and `keywords` fields with the following. Again, we can find this information in the `paper.txt` file.
```yaml
abstract: |
Noise, as the term itself suggests, is most often seen a nuisance to ecological insight, a inconvenient reality that must be acknowledged, a haystack that must be stripped away to reveal the processes of interest underneath. Yet despite this well-earned reputation, noise is often interesting in its own right: noise can induce novel phenomena that could not be understood from some underlying determinstic model alone. Nor is all noise the same, and close examination of differences in frequency, color or magnitude can reveal insights that would otherwise be inaccessible. Yet with each aspect of stochasticity leading to some new or unexpected behavior, the time is right to move beyond the familiar refrain of "everything is important" (Bjørnstad & Grenfell 2001). Stochastic phenomena can suggest new ways of inferring process from pattern, and thus spark more dialog between theory and empirical perspectives that best advances the field as a whole. I highlight a few compelling examples, while observing that the study of stochastic phenomena are only beginning to make this translation into empirical inference. There are rich opportunities at this interface in the years ahead.
keywords:
- Coloured noise
- demographic noise
- environmental noise
- quasi-cycles
- stochasticity
- tipping points
```
## Change reference style to `authoryear`
Finally, in `journal`, let's **change the reference style** to match the original paper by changinging `cite-style` to `authoryear` and set the `name` of the journal to `Ecology Letters`.
```yaml
format:
elsevier-pdf:
keep-tex: true
journal:
name: Ecology Letters
formatting: preprint
model: 3p
cite-style: authoryear
```
------------------------------------------------------------------------
# Complete paper body
## Clear template content and paste text
Next, let's **clear the template body contents** (everything outside the YAML Front Matter header) and **paste the main text from `paper.txt`**.
Copy everything between and including the `# Introduction: ...` and `## References` sections in `paper.txt` and paste it into the `paper.qmd` file, below the YAML Front matter header.
## Add Mathematical Notation
Through the `## Demographic stochasticity` section of the paper, there is discussion simulation parameters that in the orginal paper are formatted using mathematical notations used for equations. To format such in-line text using such notation, surround it with `$` dollar signs.
Here's an example of what that looks like in `paper.qmd`:
```markdown
We summarize the myriad lower-level processes that
mechanistically lead to the event of a 'birth' in the population
as a probability: In a population of $N$ identical individuals at
time t, a birth occurs with probability $b_t(N_t)$ (_i.e._ a rate
that can depend on the population size, $N$), which increases the
population size to $N+1$. Similarly, death events occur with probability
$d_t(N_t)$, decreasing the population size by one individual, to $N-1$.
```
And what it looks like when rendered:
> We summarize the myriad lower-level processes that
mechanistically lead to the event of a 'birth' in the population
as a probability: In a population of $N$ identical individuals at
time t, a birth occurs with probability $b_t(N_t)$ (_i.e._ a rate
that can depend on the population size, $N$), which increases the
population size to $N+1$. Similarly, death events occur with probability
$d_t(N_t)$, decreasing the population size by one individual, to $N-1$.
Read through that section and format any mathematical notation accordingly.
## Update references
Next we'll replace the flat citations in the text with real linked citation which can be used to **auto-generate formatted in-line citations and the references section**.
### Add bibliography
To do this, first we'll need to switch our bibliography to the `refs.bib` file. So in the YAML Front Matter, replace the `bibliography` field with the following:
```yaml
bibliography: refs.bib
```
### Insert citation cross-references
Firstly, to use the interactive citation features in Quarto, we need to move over to the **Visual** editor.
```markdown
To many, stochasticity, or more simply, noise, is just that -- something which obscures patterns we are trying to infer [@Knape2011]; and an ever richer batteries of statistical methods are developed largely in an attempt to strip away this undesirable randomness to reveal the patterns beneath [@Coulson2001]. Over the past several decades, literature in stochasticity has transitioned from thinking of stochasticity in such terms; where noise is a nuisance that obscures the deterministic skeleton of the underlying mechanisms, to the recognition that stochasticity can itself be a mechanism for driving many interesting phenomena [@Coulson2004].
```
![](assets/refs-rendered.png)
#### Commit changes
# Add analysis code
Now that we've set up the text for our paper, the next step is to add the executable code that generates the figures in the paper. This code is provided in the `analysis.R` file in the materials we downloaded.
## Inspect analysis.R file
Let's also **open `analysis.R`** in the course materials and run the code. The script has some **initial setup**, then **loads the data**, **recodes one of the columns** for plotting and then **plots the results** of the simulation, which **generates figure 1 in [`paper.pdf`](https://github.com/annakrystalli/rrtools-wkshp-materials/blob/master/paper.pdf)**.
```{r}
#| eval: false
#| filename: "analysis.R"
library(dplyr)
library(readr)
library(ggplot2)
library(ggthemes)
theme_set(theme_grey())
# create colour palette
colours <- ptol_pal()(2)
# load-data
data <- read_csv(here::here("gillespie.csv"), col_types = "cdiddd")
# recode-data
data <- data %>%
mutate(system_size = recode(system_size,
large = "A. 1000 total sites",
small = "B. 100 total sites"))
# plot-gillespie
data %>%
ggplot(aes(x = time)) +
geom_hline(aes(yintercept = mean), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = minus_sd), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = plus_sd), lty=2, col=colours[2]) +
geom_line(aes(y = n), col=colours[1]) +
facet_wrap(~system_size, scales = "free_y")
```
```{r}
#| eval: true
#| echo: false
#| message: false
#| warning: false
library(dplyr)
library(readr)
library(ggplot2)
library(ggthemes)
theme_set(theme_grey())
# create colour palette
colours <- ptol_pal()(2)
# load-data
data <- read_csv(here::here("data","gillespie.csv"),
col_types = "cdiddd")
# recode-data
data <- data %>%
mutate(system_size = recode(system_size,
large = "A. 1000 total sites",
small = "B. 100 total sites"))
# plot-gillespie
data %>%
ggplot(aes(x = time)) +
geom_hline(aes(yintercept = mean), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = minus_sd), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = plus_sd), lty=2, col=colours[2]) +
geom_line(aes(y = n), col=colours[1]) +
facet_wrap(~system_size, scales = "free_y")
```
Let's move back to our `paper.qmd` file and start adding the code to generate the plot.
## Add document level code block configuration
Before we begin, let's add some **document level code block configuration** which will apply to all code blocks in our document. We can do this by adding a `knitr` block to bottom of the YAML Front Matter of the `paper.qmd` file.
```yaml
knitr:
opts_block:
echo: false
warning: false
message: false
```
This sets the following options for all code blocks in the document:
- `echo: false` suppresses the code output
- `warning: false` suppresses warnings
- `message: false` suppresses messages
## Add block to load libraries
Next let's insert a `libraries` code block right at the top of the document to set up our analysis. Because it's a setup block we set `#| include: false` which suppresses all output resulting from block evaluation.
```{r}
#| include: false
#| echo: fenced
library(dplyr)
library(readr)
library(ggplot2)
library(ggthemes)
```
## Add block to set plot themes
Right below the `libraries` block, **insert a new block**
Copy the **code to set the plot theme** and pasted into the block:
```{r}
#| echo: fenced
theme_set(theme_grey())
```
## Add `figure 1` block
Now **scroll down** towards the bottom of the document and **create a new block just above the Conclusions** section.
### Add code
````
```{{r}}
# create colour palette
colours <- ptol_pal()(2)
# load-data
data <- read_csv(here::here("gillespie.csv"), col_types = "cdiddd")
# recode-data
data <- data %>%
mutate(system_size = recode(system_size, large = "A. 1000 total sites", small= "B. 100 total sites"))
# plot-gillespie
data %>%
ggplot(aes(x = time)) +
geom_hline(aes(yintercept = mean), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = minus_sd), lty=2, col=colours[2]) +
geom_hline(aes(yintercept = plus_sd), lty=2, col=colours[2]) +
geom_line(aes(y = n), col=colours[1]) +
facet_wrap(~system_size, scales = "free_y")
```
````
Edit path to data file to reflect the correct path to the data file in the project.