-
Notifications
You must be signed in to change notification settings - Fork 1
/
04-Writing.Rmd
1615 lines (1129 loc) · 68.2 KB
/
04-Writing.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
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
# Writing {#chap-rediger}
\toc{1}
R and RStudio make it possible to efficiently write documents of all formats, from simple notepads to theses to slide shows.
The tools to do this are the subject of this chapter, completed by the production of web sites (including a personal site).
Two document production processes are available:
- *R Markdown* with the **knitR** and **bookdown** packages.
This is the classic method, presented here in detail.
- *Quarto*, designed to be used with languages beyond R and in working environments beyond RStudio.
Quarto is under active development but does not yet allow documents to be produced with the same quality as *R Markdown*: for example, punctuation in French documents is not handled correctly in PDF[^rediger-41], tables cannot include equations[^rediger-42] and the width of figures is inconsistent in PDF documents formatted with several columns[^rediger-43].
The use of Quarto is well documented on its site[^rediger-40] and is not presented here.
[^rediger-40]: <https://quarto.org/>
[^rediger-41]: <https://github.com/jgm/pandoc/issues/8283/>
[^rediger-42]: <https://github.com/quarto-dev/quarto-cli/issues/555>
[^rediger-43]: <https://github.com/quarto-dev/quarto-cli/issues/855>
## Markdown notebook (R Notebook)
In an `.R` file, the code should always be commented to make it easier to read.
When the explanation of the code requires several lines of comment per line or block of code, it is time to reverse the logic and place the code in the text.
The concept of literate programming was developed by @Knuth1984.
It consists in describing the objectives and methods by text, in which the code is integrated.
The simplest tool is the Markdown notebook (Menu "File > New File > R Notebook").
The document template contains its instructions for use.
The language for formatting the text is Markdown[^rediger-1], an easy to use markup language:
[^rediger-1]: <https://fr.wikipedia.org/wiki/Markdown>
- Paragraphs are separated by line breaks.
- The document is structured by headings: their line starts with a number of `#` corresponding to their level.
- Character formats are limited to the essentials: italic or bold (text surrounded by one or two `*`).
- Other simple codes allow all useful formatting.
This language is the core of the pandoc[^rediger-2] software, dedicated to converting documents of different formats.
[^rediger-2]: <https://fr.wikipedia.org/wiki/Pandoc>
The **rmarkdown** package [@Xie2015] bridges the gap between R and Markdown, relying on the RStudio interface which is not essential but greatly simplifies its use.
The Markdown dialect used by the package is called *R Markdown*.
Its syntax is summarized in a cheat sheet[^rediger-3].
Its complete documentation is online [@Xie2018].
[^rediger-3]: <https://rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf>
Equations are written in the LaTeX format[^rediger-4].
[^rediger-4]: <https://fr.wikibooks.org/wiki/LaTeX/%C3%89crire_des_math%C3%A9matiques>
The simplest organization of a *R Markdown* document can be seen in the notepad template.
It starts with a header in YAML format[^rediger-5]:
[^rediger-5]: <https://fr.wikipedia.org/wiki/YAML>
```
---
title: "R Notebook"
output: html_notebook
---
```
The first entry is the title, the second is the output format: more precisely the name of the function that will process the document.
The document contains Markdown formatted text and code chunks surrounded by three backquotes (the Markdown syntax of a code block) and a language description, here `r`.
These code chunks are processed by **knitr** which transforms the result of the execution of the R code into Markdown and integrates it into the text of the document.
Processing an R Markdown document is called *knitting*.
The production chain is as follows:
- **knitr** processes the code snippets: calculations, figure production.
- **rmarkdown** integrates the production of code and text snippets to produce a standard Markdown file.
- pandoc (installed with RStudio) converts this file to HTML, LaTeX or Word format.
- LaTeX produces a PDF file when that format is requested.
RStudio allows knitting to be started by buttons rather than commands: in the source window (the top left one), a "Knit" button accompanies R Markdown documents.
For R Markdown notebooks, it is replaced by a "Preview" button with the same functions.
It can be scrolled down to choose the output format: HTML, Word, PDF (via LaTeX) and, for notepads, a "Preview" command that displays the document in HTML without executing the code snippets to save time.
As soon as the first knitting is done in Word or HTML format, you will notice that the "Preview" button disappears.
In the end, using R Markdown combines several advantages:
- Simplicity of writing: the raw text is easier to read and format than in LaTeX for example.
- Automation of the production: formatting and layout are fully automatic.
- Reproducibility: each document can be self-sufficient with its data. Re-knitting regenerates the whole document, including the necessary calculations and the production of figures.
It also has some disadvantages:
- Formatting depends on templates, and developing new templates is not easy.
- Knitting errors are sometimes difficult to correct, especially when they occur at the LaTeX compilation stage.
- Reproducibility consumes computing time.
To limit this problem, a cache system allows not to re-evaluate all the R code bits at each modification of the text.
The production of large documents can also be delegated to a continuous integration system (see chapter \@ref(chap-ci)).
## R Markdown templates
More elaborate document templates than the notepad are provided by packages, including **rmarkdown**.
They are accessible via the menu "File > New File > R Markdown..." (figure \@ref(fig:e-rmd1)).
(ref:e-rmd1) New Markdown document from a template.
```{r e-rmd1, fig.cap="(ref:e-rmd1)", echo=FALSE}
knitr::include_graphics('images/e-rmd1.png')
```
The simplest templates are *Document* and *Presentation*.
The information to be provided is the title and the name of the author, and the format of the expected document (which can be modified later).
These templates create a single file which will only need to be saved when knitting.
The syntax is the same as for the notepad.
In the header, an extra entry is used for the date, which can be calculated by R at each knitting:
```
date: "|r format(Sys.Date(), '%d/%m/%Y')|"
```
Replace the vertical bars `|` in the above example with backquotes: since this document is written with R Markdown, the date would be calculated and displayed instead of the code if the backquotes were used directly.
Inline R code (as opposed to code snippets) can be used anywhere in an R Markdown document, including in the header for the date display.
It starts with a backquote followed by `r' and ends with another backquote.
Documents can be knitted in HTML, PDF (via LaTeX) or Word format.
The header of the R Markdown file is rewritten when the knitting is started by the RStudio button which places the current output format on top of the list.
Presentations can be knitted in two HTML formats, ioslide[^rediger-6] or Slidy[^rediger-7], in Beamer (PDF) format[^rediger-8] or in Powerpoint[^rediger-9].
[^rediger-6]: <https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html>
[^rediger-7]: <https://bookdown.org/yihui/rmarkdown/slidy-presentation.html>
[^rediger-8]: <https://bookdown.org/yihui/rmarkdown/beamer-presentation.html>
[^rediger-9]: <https://bookdown.org/yihui/rmarkdown/powerpoint-presentation.html>
The level 2 outline (`##`) marks the change of slide.
Additional code, presented in the HTML format documentations, allows for specific functionality.
These templates are simple but not very useful: the R notepad is easier to use than the document template for minimalist documents.
More elaborate templates are available.
## Articles with bookdown
R Markdown does not allow you to write a scientific article.
Bibliography is not a problem because it is handled by pandoc for HTML or Word documents and outsourced to LaTeX for PDF documents.
Equations, figures and tables are numbered by LaTeX but not in HTML.
Cross-references (references to a figure number for example) are not supported.
Finally, figure and table captions only support plain text, without any formatting.
**bookdown** fills these gaps.
The package has been designed for writing books with several chapters but can be used for articles.
The **memoiR** package provides the templates shown here.
It must be installed.
### Writing
The main features of Markdown are summarized here.
A quick and more complete training is offered by RStudio[^402].
[^402]: https://rmarkdown.rstudio.com/lesson-1.html
The text is written without any formatting other than line breaks.
A simple line break has no effect on the document produced: it allows to separate sentences to simplify the tracking of the source code by git.
A line break marks a paragraph change.
The different levels of the plan are designated by the number of hashes at the beginning of the line: `#` for a level-1 title, `##` for a level-2 title, etc.
A space separates the hashes and the title text.
Bullet lists are marked by a dash (followed by a space) at the beginning of the line.
A double line break is required before the beginning of the list, but the elements of the list are separated by a simple line break.
Indented lists are created by inserting 4 spaces before the dash at the beginning of the line.
Last, numbered lists are created in the same way by replacing the hyphens by numbers, whose value is not important.
In the text, the italicized parts are surrounded by a star or an underscore (`*italic*`), while two stars mark the bold.
#### R code
R code is included in code chunks that are easily created by clicking on the "Insert a new code chunk" button above the source code window in RStudio.
They start and end with three backquotes on a new line.
These code chunks can contain R code but also Python code for example: the type of code is indicated in the header on the first line, before the name of the code chunk, then a comma-separated list of options, for example:
````
```{r cars, echo=TRUE}`r ''`
```
````
The name and options are optional: the minimum header is `{r}`.
The most useful options are :
- `echo` to show (`=TRUE`) or hide (`=FALSE`) the code.
- `message=FALSE` to hide the opening messages of some packages.
- `warning=FALSE` to hide warnings.
The default options are declared in the code snippet named "Options" at the beginning of the Markdown document, in the `opts_chunk$set()` function.
The `include=FALSE` option removes any display related to the code snippet.
In a document such as a scientific article, which does not display its code, it should be used for all code snippets except those that produce figures.
#### Figures
```{r pressure, fig.cap="Figure Title"}
plot(pressure)
```
Figures can be created by the R code (figure \@ref(fig:pressure)).
With Bookdown, a label is associated with each figure: its name is `fig:xxx` where `xxx` is the name of the R code snippet.
References are made with the command `\@ref(fig:xxx)`.
The header of the code snippet of the figure \@ref(fig:pressure) is:
````
```{r pressure, fig.cap="Caption of the figure"}`r ''`
```
````
It contains at least the name of the figure and its caption.
If the caption is long, the header is not very readable.
Furthermore, the caption is limited to simple text.
For more elaborate captions, it is possible to declare the caption in a separate paragraph that begins with the text `(ref:FigureName)`.
Figure \@ref(fig:pressure2) benefits from an improved caption.
(ref:pressure2) Title with _italic_, math ($\sqrt\pi$) and a reference to figure \@ref(fig:pressure)
```{r pressure2, fig.cap="(ref:pressure2)", echo=FALSE}
plot(pressure)
```
The text in `fig.cap`, "Title of figure" previously, is replaced by `(ref:pressure)` _within the backquotes_ which are retained and the caption is entered in a paragraph starting with `(ref:pressure)` followed by a space.
Captions are limited to a single paragraph.
If a table of figures is used (option `lof: true` in the header), a short caption is required in addition to the full caption.
It is declared in `fig.scap`.
Figures that are not created by R but come from files are integrated in a piece of code by the `include_graphics()` function whose argument is the file containing the image to be displayed.
Always place these files in the `images` folder for good organization.
#### Tables
The horizontal `-` and vertical `|` separators allow to draw a table according to the Markdown syntax, but it is not the best method.
Tables can also be produced by R code.
The content of the table is in a dataframe.
The `kbl` function in the _kableExtra_ package prepares the table for display and passes the result to the `kable_styling` function for final formatting.
```{r kable, echo=TRUE, warning=FALSE}
library("tidyverse")
my_iris <- head(iris)
names(my_iris) <- c("Sepal length ($l_s$)", "Width", "Petal length", "Width", "Species")
kableExtra::kbl(my_iris, caption="Table created by kable", booktabs = TRUE, escape = FALSE) %>%
kableExtra::kable_styling(bootstrap_options = "striped", full_width = FALSE)
```
The caption is specified by the `caption` argument and referencing is possible because the table is given a label whose name is `tab:` followed by the name of the code snippet (table \@ref(tab:kable)).
As with the figures, an enhanced caption can be written in a separate paragraph.
A short caption for a possible list of tables (option `lot: true` in the header) is declared in the `caption.short` argument of `kbl()`.
Always use the `booktabs = TRUE` argument so that the thickness of the separator lines is optimal in LaTeX.
Since the table contains mathematics (in the name of the first column), the `escape = FALSE` option is necessary.
The `bootstrap_options = "striped"` style option provides more readable tables in HTML.
Last, the `full_width = FALSE` option allows to adjust the width of the table to its content instead of occupying the whole available width.
The **flextable** package allows creating more elaborate tables, as in the following example which displays the long sepals in color.
```{r flextable}
library("flextable")
# iris dataset
iris %>%
# First lines
head() %>%
# Create a flextable object
flextable() %>%
# Column titles
set_header_labels(Sepal.Length="Sepal Length",
Sepal.Width="Width",
Petal.Length="Petal Length",
Petal.Width="Width",
Species="Species") %>%
# Select long sepals (>5) and display them in red
color(~Sepal.Length > 5, ~Sepal.Length, color="red")
```
The package documentation[^411] is available online, as well as a gallery[^412].
[^411]: https://ardata-fr.github.io/flextable-book/
[^412]: https://ardata-fr.github.io/flextable-gallery/gallery/
**flextable** does not support caption numbering except in Word documents.
This limitation is prohibitive.
#### Maths
Equations in LaTeX format can be inserted inline, like $A=\pi r^2$ (code: `$A=\pi r^2$`) or in a new line (the $ are doubled) like $$e^{i \pi} = -1.$$
They can be numbered: see equation \@ref(eq:disc), using the `\equation` environment.
\begin{equation}
A = \pi r^2.
(\#eq:disc)
\end{equation}
The numbered equation is created by the following code:
```
\begin{equation}
A = \pi r^2.
(\#eq:disc)
\end{equation}
```
#### Cross-references
Figures and tables have an automatically generated label, identical to the name of the code snippet prefixed with `fig:` and `tab:`.
For equations, the label is added manually by the code `(\#eq:xxx)` before the end of the equation.
Sections can be given a label by completing their title with `{#yyy}`.
Sections receive by default an implicit label[^405] corresponding to their text, in lower case, where special characters are replaced by dashes.
Implicit labels are unstable (they change with the title of the section) and difficult to predict: this is why it is advisable to add an explicit label to each section being cross-referenced.
This is the case for chapters, for which the name of the HMTL file produced is identical to the label.
Chapter labels must follow file naming rules by not containing special characters.
[^405]: https://pandoc.org/MANUAL.html#extension-implicit_header_references
Bookmarks can also be placed freely in the text with the command `(ref:zzz)`.
In all cases, the call to the reference is made by the command `\@ref(ref:zzz)`.
#### Bibliography
Bibliographic references in BibTeX format must be included in the `.bib` file declared in the header of the Markdown document.
```
bibliography: references.bib
```
This file can be created and maintained by Zotero installed with the Better BibTeX extension (see section \@ref(sec:Zotero)).
To do this, you just have to create a Zotero collection corresponding to the project and drag the relevant references into it.
Then use the contextual menu "Export collection..." and select:
- Format: "Better BibTeX" for articles and presentations or "Better BibLaTeX" for memoirs, depending on whether the bibliography is managed by BibTeX and natbib or biber and BibLaTeX for PDF production.
- Check the "Keep up to date" box so that any changes in Zotero are exported automatically.
- Click on "OK" then choose the name of the file (`references.bib`) and its location (the R project folder).
The references can be called in the text, between square brackets by the code `[@Reference]`, or in the text, by removing the brackets.
The bibliography is handled by pandoc when producing Word or HTML documents.
The bibliographic style can be specified, by adding the line
```
csl:nom_du_fichier.csl
```
in the document header and copying the _.csl_ style file to the project folder.
Over a thousand styles are available[^401].
[^401]: <https://github.com/citation-style-language/styles>
For PDF documents, the bibliography is managed by LaTeX.
To prepare the submission of a manuscript to a journal, it will be necessary to open the intermediate _.tex_ file produced by pandoc and copy the contents of the environment \{document\} into the template proposed by the journal, which will take care of the formatting.
#### Languages
The languages are to be declared in the header of the documents produced by the **memoiR** templates.
The main language of the document modifies the name of certain elements, such as the table of contents.
The additional languages allow the creation of multilingual documents.
The header fields are:
```
lang: fr-FR
otherlangs: [en-US, it]
```
The change of language in the document is managed in LaTeX, but not in HTML, by inserting on a new line the following command:
```
\selectlanguage{english}
```
The current language only has an effect in LaTeX output: a space is added before double punctuation in French, the size of spaces is larger at the beginning of sentences in English, etc.
The `\selectlanguage` command is simply ignored in HTML.
The language names are different in the header (IETF codes) and in the text (language name).
The correspondence and the complete list of languages can be found in table 3 of the package documentation **polyglossia**[^403].
[^403]: http://mirrors.ctan.org/macros/unicodetex/latex/polyglossia/polyglossia.pdf
HTML formatting of punctuation in French documents is possible using a filter declared in pandoc [^450].
The `fr-nbsp.lua` file must be copied into the project directory from its GitHub repository and declared into the header of the Markdown document.
```
output:
pandoc_args:
--lua-filter=en-nbsp.lua
```
The filter formats all the punctuation in the document, whatever the language: it should therefore only be used for documents written entirely in French.
[^450]: https://github.com/InseeFrLab/pandoc-filter-fr-nbsp
### Simple Article template {#sec:memo}
The *Simple Article* template of **memoiR** produces a simple HTML document with a floating table of contents (see example[^rediger-10]).
Other HTML formats are available: see the gallery[^404] of the package.
The PDF format is close to the *article* model of LaTeX (example[^rediger-11]).
[^404]: https://ericmarcon.github.io/memoiR/
[^rediger-10]: <https://EricMarcon.github.io/Krigeage/Krigeage.html>
[^rediger-11]: <https://EricMarcon.github.io/Krigeage/Krigeage.pdf>
The template contains its own documentation.
#### Create
Use the menu "File > New File > R Markdown..." then select "From template" (figure \@ref(fig:e-rmd1)).
The list of available templates and the package that offers them is then displayed.
Select the *Simple Article* template from the **memoiR** package, choose the name of the project ("Name:", which will be the name of the folder in which it will be created, and its parent folder ("Location:").
In the organization proposed in section \@ref(sec:solution-dossiers), the parent folder is `%LOCALAPPDATA%\ProjectsR`.
The project name must not contain any special characters (accent, space...) to ensure its portability on all operating systems (Windows, Linux, MacOS).
Advanced templates create a folder with many files (bibliography, styles, LaTeX template...), unlike simple templates which create only one file.
When a folder is created, for example by the *Simple Article* template, you have to make it an RStudio project: in the projects menu (top right of the RStudio window), use the "New Project..." menu, then "Existing Directory" and select the folder that has just been created.
#### Write
The instructions for using the template are contained in the text provided by default.
#### Knit
The document can be knitted in several formats:
- *html_document2* is the HTML format the template was designed for: a notepad with a floating table of contents.
- *gitbook* is an alternative HTML format, normally used for books.
- *downcute* is an HTML format provided by the **rmdformats** package.
- *pdf_book* produces a PDF document following the LaTeX *article* template, commonly used directly in LaTeX.
- *word_document2* creates a Word file.
#### Publish {#sec:article-en-ligne}
The **memoiR** package simplifies the uploading of produced documents to a web server.
The `build_gitignore()` function creates a `.gitignore` file for source control which must be enabled (see section \@ref(sec:git-cds)).
The `build_readme()` function creates a `README.md` file that is needed by GitHub.
It contains the title of the project, its summary and links to the HTML and PDF versions of the documents produced.
The project must be linked to a GitHub repository (section \@ref(sec:creerdepot)).
Two publication strategies are possible.
In the first one, the documents are knitted locally and placed in the `docs` folder, which will be the support of the GitHub pages.
In the second one, the documents are knitted by GitHub Actions each time modifications are pushed on the repository: this is called continuous integration (section \@ref(chap-ci)).
The local production strategy is covered here; continuous integration will be covered in section \@ref(sec:memoiR-ci).
The `build_githubpages()` function places all the knitted documents (HTML and PDF) in the `docs` folder, along with a copy of the `README.md` file.
This way, it is possible to activate the project's GitHub pages (on the `docs` folder of the `master` branch).
The `README.md` file will be the home page of the produced web site.
In practice, we knit in HTML format during the whole writing phase, because the production is very fast.
When the document is stabilized, it should be knitted in HTML and PDF format.
Finally, the execution of `build_githubpages()` places all the files produced in `docs`.
It remains to push the repository on GitHub and activate the GitHub pages.
### Other templates
The *Stylish Article* template of **memoiR** is intended for the production of well-formatted PDF articles for self-archiving (typically, the HAL repository), in A4 format in double column[^rediger-12].
[^rediger-12]: Exemple: <https://EricMarcon.github.io/Rochebrune2018/Entropie.pdf>
The HTML format is the same as the *Simple Article* template.
The **rticles** package aims to provide templates for all scientific journals that accept article submission in LaTeX.
It offers Markdown templates which produce PDF files conforming to the requirements of the journals and the possibility to recover the intermediate `.tex` file (pandoc produces a `.tex` file transmitted to the LaTeX compiler).
The package does not allow HTML knitting because it uses LaTeX syntax in the R Markdown document instead of using **bookdown** to handle bibliographic and cross references.
It is not possible to directly exchange standard R Markdown content with documents written for **rticles**, which limits the interest of the package.
## Beamer Presentation
The *Beamer Presentation* template of **memoiR** allows to create HTML and PDF (beamer) presentations simultaneously, as shown in the example[^rediger-13].
[^rediger-13]: <https://EricMarcon.github.io/Chao1/>, Choose "Lecture" (Read in HTML) ou "Téléchargement" (Download PDF).
The approach is identical to that of articles in the same package.
The title levels allow separating the parts of the presentation (`#`) and the slides (`##`).
Two formats are available in HTML: ioslides[^rediger-14] and Slidy[^rediger-15].
Some specificities in the code allow to refine the presentation of the slides, for a two-column display for example: they are documented in the template.
[^rediger-14]: <https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html>
[^rediger-15]: <https://bookdown.org/yihui/rmarkdown/slidy-presentation.html>
## memoir
The *Memoir* template of the **memoiR** package is intended for long documents, which have an important difference from the previous documents: a long document is composed of several chapters, each placed in its `.Rmd` file.
The HTML format is gitbook[^rediger-16], the standard for reading such documents online.
The PDF format is derived from the LaTeX *memoir*[^rediger-17] template, also optimized for long documents.
[^rediger-16]: <https://www.gitbook.com/>
[^rediger-17]: <https://www.ctan.org/pkg/memoir>
This document was written with this template.
### Create
Creating a work project is identical to the one presented above: the template is: *Memoir*.
The created folder must be turned into a project.
Run `build_git()` and `build_readme()`, enable source control and push the project to GitHub, in the same way as for an article (section \@ref(sec:article-en-ligne)).
Each chapter of the book is an Rmd file, whose name normally starts with its number (e.g.: `01-intro.Rmd`).
All Rmd files in the project folder are actually treated as chapters, sorted by filename, including those provided by the template (startup and syntax) which should be deleted except for `99-references.Rmd` which contains the bibliography, placed at the end.
The `index.Rmd` file is special: it contains the document header and the first chapter.
### Write
The first chapter is placed in the front matter of the printed book: it should not be numbered (hence the `{-}` code next to the title) in the HTML version.
It must end with the LaTeX command `\mainmatter` which marks the beginning of the body of the book.
The outline levels start with `#` for chapters (only one per file), `##` for sections, etc.
### Knit
Compiling to PDF is done by XeLaTeX, which must be installed.
While writing, it is strongly advised to create only the HTML file, which is much faster than a LaTeX compilation.
Each chapter can be viewed very quickly by clicking on the "Knit" button above the source window.
The entire book is created by clicking on the "Build Book" button in the RStudio *Build* window.
The button's drop-down list allows you to create all documents or limit yourself to one format.
The files produced are placed directly in the `docs` folder, which will be used by the GitHub pages to allow online reading and downloading of the PDF.
The home page of the website is created by bookdown from the `index.Rmd` file: the `README.md` file is not duplicated in `docs`.
### Finishing
The layout is done fully automatically by pandoc (in HTML) and LaTeX (in PDF).
It is often useful to help LaTeX to solve some margin overruns due to too large layout constraints: for optimal readability, columns are narrow, but code (formatted text between backquotes) does not allow hyphenation.
If a line of text protrudes into the right margin in the PDF document, the solution is to manually add the `\break` code to the desired location for the line break in the R Markdown document.
The command has no effect on the HTML document but forces the hyphenation in LaTeX.
To break formatted text (between asterisks for italics or more frequently between backquotes for code), you must finish formatting before `\break` and start again afterwards.
For example, to force a line break before `file.Rmd`:
```
The file `/path/`\break`file.Rmd`
```
In HTML, a space will be added between the two pieces of code.
R code snippets are automatically formatted by **knitr** when the `tidy=TRUE` option is applied to them.
The default behavior is specified in the **knitr** options, in a code snippet at the beginning of the `index.Rmd` file:
```{r opts_chunk, eval=FALSE, tidy=FALSE}
# knitr options
knitr::opts_chunk$set(
cache=TRUE, warning=FALSE, echo = TRUE,
fig.env='SCfigure', fig.asp=.75,
fig.align='center', out.width='80%',
tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=55),
size="scriptsize",
knitr.graphics.auto_pdf = TRUE)
```
The maximum width of a line of formatted code here is 55 characters, optimal for the template.
Sometimes automatic formatting does not work because **knitr** cannot find a line break that meets all the constraints, causing the code to overflow.
In this case, manually format the code snippet by adding the `tidy=FALSE` option.
The literal code blocks, delimited by three backquotes, must be formatted manually, avoiding any line longer than 55 characters.
### Gitbook site
The website containing the gitbook document must be set up in `_output.yml` so that :
- The title of the document appears at the top of the table of contents.
- An indication of the use of GitHub and bookdown is displayed at the bottom of the table of contents.
- A GitHub button in the title bar allows to open the project repository.
- Another button allows to download the PDF document.
The `_output.yml` file of this document is the following:
```
bookdown::gitbook:
css: style.css
config:
sharing:
github: yes
facebook: false
twitter: false
toc:
before: |
<li><a href="./">Working with R</a></li>
after: |
<li>
<a href="https://github.com/EricMarcon/WorkingWithR" target="blank">
Hosted on GitHub, published by bookdown
</a>
</li>
download: pdf
```
The `sharing:` section manages the buttons in the title bar.
By default, the links to Facebook and Twitter are enabled but the one to GitHub is not.
For it to work, the GitHub repository must be declared in the header of the `index.rmd` file:
```
github-repo: EricMarcon/WorkingWithR
```
The `toc:` section contains two portions of HTML code in which the title of the document and the link to its GitHub repository must be adapted to the project.
Finally, the `download:` section lists the downloadable document formats and displays a download button in the title bar.
### Continuous integration {#sec:rediger-ouvrage-ci}
Building a book takes time, especially if it contains calculations.
It must be launched in gitbook format and in PDF format.
In production, it can be outsourced to GitHub (chapter \@ref(sec:bookdown-ci)).
### Google Analytics
The tracking of the book's audience can be entrusted to Google Analytics.
To do so, you have to create an account and add a Google Analytics *property*, i.e. a website, then a data feed, here a web feed[^rediger-37].
[^rediger-37]: https://support.google.com/analytics/answer/9304153?hl=fr&ref_topic=9303319
Google Analytics provides a configuration script named `gtag.js` to be placed at the root of the project folder.
Finally, declare the script in the header of the web pages by adding a statement in `_output.yml`, in its first section.
```
bookdown::gitbook:
includes:
in_header: gtag.js
```
## R Markdown web site
A web site made of pages written with R Markdown (without the **bookdown** features) and a menu can be created very easily, with a good result[^rediger-18].
[^rediger-18]: <https://rstudio.github.io/learnr/> for example.
### Template
In RStudio, in the projects menu at the top right, click on "New Project..." then "New Directory" then "Simple R Markdown website".
Enter the name of the project, select the folder in which the project will be created by clicking on "Browse" and finally click on "Create Project".
The default site contains two pages: `index`, the home page, and `about`, the "About" page.
The `_site.yml` file contains the name of the site and the contents of its navigation bar: a title and the corresponding file.
Other pages will be added by creating new `.Rmd` files and adding them to the `_site.yml` file.
### Improvements
The site template can easily be enhanced by adding lines to `_site.yml`:
- To add a GitHub icon in the navigation bar to link to the site source code.
- To choose the method of knitting, to use **bookdown** instead of **rmarkdown**.
- To place the site files in the `docs` folder and thus separate code and production.
The completed `_site.yml` file is as follows:
```
name: "my-website"
navbar:
title: "My Website"
left:
- text: "Home"
href: index.html
- text: "About"
href: about.html
right:
- icon: fa-github
href: https://github.com/rstudio/rmarkdown
output_dir: "docs"
output:
bookdown::html_document2:
theme: sandstone
highlight: tango
toc: true
toc_float: yes
```
The GitHub icon is part of the Font Awesome collection of which all free icons[^rediger-19] are usable with the same syntax: "fa-name".
[^rediger-19]: <https://fontawesome.com/icons?d=gallery&m=free>
The link corresponding to the icon must be the one in the website's GitHub repository.
The syntax of the `output` section is the same as that of the documents seen above.
It applies to all pages (with the YAML header reduced to the minimum).
The available themes are those of rmarkdown[^rediger-20].
[^rediger-20]: <https://bookdown.org/yihui/rmarkdown/html-document.html#appearance-and-style>
The `highlight` option specifies how any R code displayed will be formatted.
Last, the table of contents is floating, which means that its position adjusts as the window scrolls.
### Source control
The project must be put under source control and pushed to GitHub (chapter \@ref(chap-git)).
The `.gitignore` file is the following:
```
# R
.Rbuildignore
.RData
.Rhistory
.Rprofile
.Rproj.user
# Web Site
/_site/
/*_cache/
/*_files/
```
Enable GitHub pages (section \@ref(sec:github-pages)) on the `docs` folder to host the site.
Add an empty file named `.nojekyll` in `docs` so that GitHub pages won't try to reformat the site.
You can use the RStudio terminal to run:
```
touch docs/.nojekyll
```
## Personal web site: blogdown {#sec:blogdown}
To create a personal web page, *Hugo* is a static site generator capable of producing HTML pages from Markdown code.
Static sites have the advantage, compared to dynamic sites managed by a content management system (CMS, for example: Wordpress, Joomla, SPIP), to be portable on any web server without database support or code to run on the server side (such as PHP) and to be very fast since the pages are created once and not at each consultation.
A Hugo site can be hosted for example on the personal page of any GitHub user.
Its address is of the form "GitHubID.github.io".
Hugo offers many themes, which are templates for site structure, so the **Academic** theme, intended for researchers.
In RStudio, the **blogdown** package is provided to easily produce web pages with Hugo.
These pages can contain R code: they are very similar to an article, seen above, whose content can be easily copied and pasted.
So we will use this solution, for a site like the one proposed in example[^rediger-21].
[^rediger-21]: <https://EricMarcon.github.io/>
The structure of the website is simple:
- A home page, containing various customizable components such as the author's biography, a selection of publications, blog posts or other elements and a contact form.
- Pages detailing the various elements (publications, posts, etc.) written in R Markdown.
### Installing the tools
The first step is to install the **blogdown** package in R.
```{r blogdown, eval=FALSE}
install.packages('blogdown')
```
**blogdown** is able to install Hugo on Windows, macOS or Linux.
```{r Hugo, eval=FALSE}
blogdown::install_hugo()
```
The full documentation for **blogdown** is available[^rediger-22].
[^rediger-22]: <https://bookdown.org/yihui/blogdown/>
Recent versions of Hugo use *Go* (the programming language) to install their modules on the fly: here the Academic theme is loaded from GitHub at the time of site creation.
Go must therefore be installed[^rediger-23].
[^rediger-23]: <https://golang.org/doc/install>
### Create
The easiest way is to create a repository on GitHub from the template.
On the *starter-academic*[^rediger-24] repository page, click on the "Use this template" button, optionally authenticate to GitHub, and then enter the name of the repository that will contain the project, for example "MySite".
[^rediger-24]: <https://github.com/wowchemy/starter-academic>
The repository can be the one of the main site of your GitHub account (see section \@ref(sec:github-pages)), at the address https://GitHubID.github.io[^rediger-45].
The name to enter is simply "GitHubID.github.io" (*GitHubID* is the name of the GitHub account).
[^rediger-45]: Exemple: https://EricMarcon.github.io/Krigeage/
Create the repository.
Copy the repository address by clicking on the "Code" button and then on the button to the right of the address (figure \@ref(fig:rediger-GitHub-Clone)).
(ref:rediger-GitHub-Clone) Copy of the address of a repository to clone on GitHub.
```{r rediger-GitHub-Clone, fig.cap="(ref:rediger-GitHub-Clone)", echo=FALSE}
knitr::include_graphics('images/rediger-GitHub-Clone.png')
```
In RStudio, create a new project from GitHub: in the projects menu on the top right, click on "New Project..." then "Version Control" then "Git" then paste the address in the "Repository URL" field (figure \@ref(fig:rediger-Projet-GitHub)).
Select the folder in which the project will be created by clicking on "Browse" and finally click on "Create Project".
(ref:rediger-Projet-GitHub) Copy the address of a repository to clone to GitHub.
```{r rediger-Projet-GitHub, fig.cap="(ref:rediger-Projet-GitHub)", echo=FALSE}
knitr::include_graphics('images/rediger-Projet-GitHub.png')
```
The created project is an exact copy of the template, which must be customized.
RStudio automatically adds a line at the end of the `.gitignore` file to ignore its working files (`.Rproj.user` folder).
Add a comment line to indicate this.
The content of `.gitignore` should be as follows:
```
# R
.Rbuildignore
.RData
.Rhistory
.Rprofile
.Rproj.user
# Hugo
/resources/
/public/
# blogdown
/static/en/
/static/fr/
*.rmarkdown
_index.html
index.html
**/index_files/
```
A bug of **blogdown** requires to move the file `config.toml` from the folder `config/_default/` to the root of the project.
Take into account these modifications in git by making a commit.
### Building the site
Run
```{r build_site, eval=FALSE}
blogdown::build_site(build_rmd = TRUE)
```
to build the website, including its future R Markdown pages.
To display the site, run :
```{r serve_site, eval=FALSE}
blogdown:::serve_site()
```
It appears in the RStudio *Viewer* window, which can be viewed in the system's default web browser by clicking the enlarge button.
To modify the content of the site, it is best to stop the web server with the command:
```{r stop_server, eval=FALSE}
blogdown:::stop_server()
```
The site produced by **blogdown** is located in the `public` folder which can be copied directly to a web server that will host it.
A simple solution is to declare this folder as the root of the GitHub pages of the project
(section \@ref(sec:github-pages)).
The optimal method is to use continuous integration (see section \@ref(sec:blogdown-ci)) to copy it to the root of the `gh-pages` branch which will be declared as the location of the site on GitHub.
### Multilingual site
If the site is multilingual (say English and French), its `content` folder must be copied in a folder corresponding to each language.
The files in `content/authors/admin/` must be duplicated into `content/en/authors`\break`/admin/` and `content/fr/authors/admin/`.
In practice, create an `en` folder and an `fr` folder in `content`.
Move all the original `content` folder `en` and then copy it into `fr`.
### Set up
The site configuration files are well documented and offer many options.
The main ones are reviewed here for a quick creation of a working site.
The `config.toml` file contains the general parameters of the site.
The lines to be updated are the site title (the owner's name since it is a personal site) and its public address.
For the example site:
```
title = "Eric Marcon"
baseurl = "https://EricMarcon.github.io/"
```
It also contains the default language selection line ("en" or "fr") and the line that allows to place the files produced by Hugo in each language folder ("true" mandatory for a multilingual site):
```
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true
```
The `config/_default/` folder contains the other configuration files.
The `languages.toml` folder contains the language settings and menu translations.
For each language, the version used and the content folder are specified:
```
[en]
languageCode = "en-us"
contentDir = "content/en"
[fr]
languageCode = "fr-fr"
contentDir = "content/fr"
```
For additional languages, the site title, date display settings and menu translation are added.
In the section `[fr]`:
```
[fr]
languageCode = "fr-fr"
contentDir = "content/fr"
title = "Eric Marcon"
description = "Page personnelle d'Eric Marcon"
[fr.params]
description = ""
date_format = "02-Jan-2006"
time_format = "15:04"
[[fr.menu.main]]
name = "Accueil"
url = "#about"
weight = 20
(...)
```
These lines are commented out in the template and must therefore be uncommented by removing the `#` at the head of the line.
The menus are described below.
`params.toml` describes the look of the site.
The options are grouped by topic, for example "Theme" for the general appearance.
In "Basic Info", the line
```
site_type = "Person"
```
selects a personal site.
It is possible to use Academic for a scientific project site or a unit site, not documented in detail here.
The main differences are, for a collective site:
- The management of authors: in the `/contents/<language>/outside` folder, only one `admin` folder is used for a personal site, whereas one folder per person is needed for a collective site.
- A component described below, which allows to present the persons, must be activated.
The description of the site in the default language is entered, for search engines:
```
description = "Eric Marcon's Homepage"
```
It must be translated in the file `languages.toml`, in each language.
In "Site Features", we select the coloring of the R code, the activation of the formatting of equations and the legal warning for the use of cookies.
```
highlight_languages = ["r"]
math = true
privacy_pack = true
```
The `edit_page` line needs to be updated: replace the default repository "<https://github.com/gcushen/hugo-academic>" with that of the site.
"Contact details" contains the contact information for the site owner.
They must be entered.
"Regional Settings" contains the date display settings for the default language (those for other languages are in `languages.toml`).
They usually do not need to be changed.
"Comments" allows you to enable visitor comments at the bottom of pages, with Disqus or Comment.io (an account is required with the provider).
"Marketing" allows you to activate the tracking of site traffic by simply entering your Google Analytics ID (to be created with a Google account).
"Content Management System" contains the line `netlify_cms` whose value must be `false` if the site is not hosted by Netlify.
Finally "Icon Pack Extensions" allows you to activate Academicons icons if necessary.
### Write