R Markdown is an essential tool for reproducible research in biostatistics. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. This document will guide you through:
For more details, visit the R Markdown website
To work with R Markdown, if necessary:
The YAML header (at the top between ---) configures your
document. Key settings include:
R Markdown supports various text formatting options:
*italic* or underscores _italic_**bold** or
underscores __bold__Code: Use backticks for inline code~~strikethrough~~^2^~2~Unordered lists use asterisks, plus, or minus signs:
Ordered lists use numbers:
The following document is mainly based on https://bookdown.org/yihui/rmarkdown/r-code.html.
You can insert R code chunks using:
Insert button)Ctrl + Alt + I
(Cmd + Option + I on macOS)There are many chunk options in knitr documented at https://yihui.name/knitr/options. Here are some important ones:
eval: Controls whether to execute
the code chunk
echo: Shows/hides source code (some
may prefer to see only the results and not the source code)
results: Controls output
display:
'hide' to hide text output.'asis' to write text output "as-is". e.g., you
can write raw Markdown text from R code (like
cat('*Markdown** is cool.\n')). By default, text output
will be wrapped in verbatim elements.collapse: Controls whether text
output and source code are merged into a single code block in the
output. This is mostly cosmetic: collapse = TRUE makes the
output more compact by displaying R source code and its text output in a
single block. The default collapse = FALSE separates R
expressions and their text output into different blocks.
warning, message, and
error: Control display of
warnings/messages/errors
include: Controls chunk inclusion
in output. When include = FALSE, the entire code chunk is
excluded from the output, but will still be evaluated if
eval = TRUE. When you want to set
echo = FALSE, results = 'hide',
warning = FALSE, and message = FALSE, you
might simply use the single option
include = FALSE.
Figure dimensions: Use fig.width,
fig.height
When you click the Knit button, a document will be generated that includes both content and the output of any embedded R code chunks within the document.
You can produce various outputs in a code chunk: text output, tables,
or graphics. You have fine control over all these outputs via chunk
options, which can be provided inside the curly braces (between
```{r} and }).
The value of a chunk option can be any valid R expression, which
makes chunk options extremely flexible. For example, the
eval option controls whether to evaluate (execute) a code
chunk, and you can conditionally evaluate a chunk using a previously
defined variable.
## [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774
Besides code chunks, you can also insert values of R objects inline in text. For example:
For a circle with the radius r x, its area is
r round(pi * x^2, 2).
You can write LaTeX code easily. For example: \(y_{i} = \mathbf{x}_{i}^{\top}\mathbf{\beta} + \epsilon_{i}\), \(\epsilon_{i} \sim N(0, \sigma^{2})\).
\(\left(\int_{a}^{b} f(x) \; dx\right)\), \(\displaystyle\sum_{x = a}^{b} f(x)\), \(\displaystyle\prod_{x = a}^{b} f(x)\), \(\log(x)\), \(\exp(x)\)
Greek letters: \(\epsilon \varepsilon E\)
To write an equation:
\[ \sum_{n = 1}^{10} n^2 \]
To effectively cite references, start by creating a reference file named references.bib. Heres how to format your citations:
@smith2020example[@smith2020example][@smith2020example; @brown2021manual][@smith2020example, pp. 33-35]@smith2020example argues that...Consider using the following tools to manage your bibliography: * Zotero with Better BibTeX extension * JabRef * Mendeley * EndNote