“Ideally, the computer code that is released will encompass all the steps of computational analysis, including all data preprocessing steps\(....\)
All aspects of the analysis need to transparently reported.’’
One technique is “literate programming”
Concept introduced by Donald Knuth (he’s kinda a big deal)
Write your reports/articles as a stream of code and text
Needs the human/documentation language + a machine/programming language
Programs are meant to be read by humans, and only incidentally for computers to execute.
— Harold Abelson and Gerald Jay Sussman
Each time you build a Quarto document it starts from scratch and executes in order preventing you from making mistakes such as:
Need to know a little about 3-ish computer languages, i.e.,
If you know some HTML/CSS or \(\LaTeX\), you can get even fancier.
Good news! You don’t need to know much of each, and all are fairly simple for the basics. So you will be able to build a great foundation today!
“A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions.”
— John Gruber (inventor of Markdown)
You can create emphasis (italics), strong emphasis (bold), super-/subscripts.
Emphasized and strongly emphasized.
Water’s molecular formula is H2O and Avogadro’s constant is 6.022 x 1023 mol-1.
Beautifully typesetting math is relatively easy too if you know a little \(\LaTeX\)
The series \(a + ar + ar^2 + \dots + ar^{n - 1}\) equals \[ \sum_{k = 0}^{n - 1} ar^k = a \frac{1 - r^n}{1 - r}\]
And you can include blocks of code/algorithms by placing 4 spaces or one tab in front of each line:
# Step 1
printf("Hello world!")
# Step 2
exit()
But most often you can put code in fenced blocks using triple backticks (```)
# Step 1
printf("Hello world!")
# Step 2
exit()
Two styles - style 1
Hello!
Hi!
Two styles - style 2
Separate by one or more blank lines. Newlines are otherwise treated as spaces allowing you to wrap your text as you like.
Paragraph 1 text. Ipsum dolor sit amet, consectetur adipiscing elit.
Paragraph 2 text. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
“Snakes. Why did it have to be snakes?”
—Indiana Jones
An en-dash is used for ranges of numbers: 2–5 pages.
Preserves line breaks like this.
Four space or one tab rule to nest.
The numbers don’t matter (except the first tells it where to start). Nest by indenting four spaces.
But try to be friendly to those reading it in text form or use this alternate form, i.e.,
Attribution
~ *n.*, The act of attributing, especially the act of establishing a particular person as the creator of a work of art.
~ *n.*, Something, such as a quality or characteristic, that is related to a particular possessor; an attribute.
Definition
~ *n.*, A statement conveying fundamental character.
~ *n.*, A statement of the meaning of a word, phrase, or term, as in a dictionary entry.
~ *n.*, The act or process of stating a precise meaning or significance; formulation of a meaning.
A line containing a row of three or more *, -, or _ characters (optionally separated by spaces) produces a horizontal rule:
To external sites are generally autoconverted but can also be done more explicitly and with custom text.
Various items can be cross-referenced including headers, figures, tables, equations, and code blocks.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Varius congue suscipit placerat enim ante id sollicitudin. Enim congue vulputate nisi aptent rhoncus nec.
Various items can be cross-referenced including headers, figures, tables, equations, and code blocks.
Felis praesent sollicitudin vulputate tellus finibus praesent varius quis diam erat litora phasellus.
Yes, good stuff is here too, but maybe you want to go back to the main section and review.
Size it with a little help from CSS (no space around equals sign!)
Here is a footnote reference,[^1] and another.[^longnote]
[^1]: Here is the footnote.
[^longnote]: Here's one with multiple blocks.
Subsequent paragraphs are indented to show that they belong to the previous footnote.
The whole paragraph can be indented, or just the first line.
This paragraph won't be part of the note, because it isn't indented.
Here is a footnote reference,1 and another.2
This paragraph won’t be part of the note, because it isn’t indented.
Here is an inline note.1
Several formats available. Painful to type yourself (so don’t! - see the knitr part later!)
+---------------+---------------+--------------------+
| Fruit | Price | Advantages |
+===============+===============+====================+
| Bananas | $1.34 | - bright color |
+---------------+---------------+--------------------+
| Oranges | $2.10 | - cures scurvy |
+---------------+---------------+--------------------+
Fruit | Price | Advantages |
---|---|---|
Bananas | $1.34 |
|
Oranges | $2.10 |
|
Several formats available. Painful to type yourself (so don’t! - see the knitr part later!)
Right | Left | Default | Center |
---|---|---|---|
12 | 12 | 12 | 12 |
123 | 123 | 123 | 123 |
1 | 1 | 1 | 1 |
A Quarto document has
Contains metadata that becomes titles, headings, etc. and controls output formatting. (Hover over the circled numbers for more detail.)
format:
specifies that we want to produce an HTML document with embedded resources which means that you can send the file to someone else, e.g., by e-mail, and it will still work.
engine: knitr
specifies the use of the knitr engine for code chunks.
Some formatting flexibility is available, but generally you have to live with what it produces unless you want to get into HTML/CSS customization writing special formatters in yet another programming language.
united
here.
Now, start to combine text and R code. (No YAML header this time just to help us focus on the R code blocks.)
echo
Chunk attributes control many features of the display. For example, you can control whether the code is shown or not.
comment
Or what is used to comment the results or if it is commented at all.
eval
Show the code, but do not run it.
include
You can also do neither, but still run the code which is very useful for setup/configuration.
name
Blocks can be named and this is most useful for locating where errors occur.
Plotting just works… most of the time, but there are chunk attributes for when it doesn’t (e.g., figure height/width, format, etc.). Note that if we had already used mtcars
as we did in the blocks above, we would not have to call data(mtcars)
. The document works as a single code file, which is one way that it creates reproducible results.
You can set global options in the YAML header for the most common way that you want each R code block run. You can override those global options just by changing any at the relevant block as we were doing before.
Read more about chunk options at https://quarto.org/docs/output-formats/html-code.html.
Many times you do not want to have raw R output in your files especially if they are for public consumption. One way to deal with that is inline R code.
However, this output is not really ideal either and in a bit we will see how to make it better. Before we do that, let’s see how to create tables too.
There are several ways to make tables. One of the easiest is knitr::kable
because you already have knitr
available.
If knitr::kable
does not meet your needs, you can explore other packages, such as gt
, dt
, or htmlTable
.
Most often use their output options to produce HTML tables in output: asis
blocks just like we did with knitr::kable
.
embed-resources: true
option.sprintf
sprintf
is an R function for text formattingsprintf
is useful for making your R output more user friendly for inline text and tables.Starts with a percent sign %
and ends with a letter (case sensitive)
d
: integer,f
: fixed point decimal notation,e
or E
: scientific notation,
s
: character strings
with integers (R will convert) [1] "1: (1.00)" "2: (1.41)" "3: (1.73)" "4: (2.00)" "5: (2.24)"
[6] "6: (2.45)" "7: (2.65)" "8: (2.83)" "9: (3.00)" "10: (3.16)"
[1] " 1: (1.00)" " 2: (1.41)" " 3: (1.73)" " 4: (2.00)" " 5: (2.24)"
[6] " 6: (2.45)" " 7: (2.65)" " 8: (2.83)" " 9: (3.00)" "10: (3.16)"
%
and letterNo space between the %
and letter means the default - strings as is - 6 decimal places for numbers
%
and letterPlace a number for the field width in characters, only pads with spaces; if things exceed width, they will will not be truncated.
%
and letterIf f
then use .<num>
to specify the number of decimal places.
%
and letterUse -
to left justify within the width. Any order is ok.
%
and letterUse +
to always add signs to numbers
%
and letterUse 0
(zero) to zero pad. Note however this may not work on all platforms, but has on all that I have tried (Windows, Mac, Linux).
Use %%
to get a literal percent sign
If you buy sprintf in the next 10 minutes, I’ll throw in rprintf for free!
[1] "1.56, 2.3, +3.78, 2.34, 2"
agegp alcgp tobgp ncases ncontrols
1 25-34 0-39g/day 0-9g/day 0 40
2 25-34 0-39g/day 10-19 0 10
3 25-34 0-39g/day 20-29 0 6
4 25-34 0-39g/day 30+ 0 5
5 25-34 40-79 0-9g/day 0 27
6 25-34 40-79 10-19 0 7
[1] "25-34, 40" "25-34, 10" "25-34, 06" "25-34, 05" "25-34, 27" "25-34, 07"
Check out the Basic Table Example
Check out the Advanced Table Example