Code
x <- rnorm(100)
mean(x)[1] -0.001136062
Scientific communication requires more than just words—we need formatted text, mathematical equations, code, figures, and tables. Traditionally, researchers used word processors for this purpose, but word processors have significant limitations for technical writing. They obscure the structure of documents behind visual formatting. They make collaboration difficult because different versions become hard to reconcile. They separate code from the documents that describe it, making it easy for analyses and their descriptions to get out of sync.
Markup languages offer a different approach. You write in plain text, adding simple annotations that specify how the document should be formatted. A processor then converts this annotated text into beautifully formatted output. Because the source is plain text, it can be version-controlled, compared across versions, and edited with any text editor.
Markdown is a lightweight markup language designed to be easy to read and write. Created by John Gruber in 2004, it uses intuitive syntax that looks reasonable even before processing. A document written in Markdown can be rendered into HTML, PDF, Word documents, presentations, websites, and more.
The basic philosophy is that common formatting should be quick to type and not interrupt the flow of writing. Bold text is wrapped in double asterisks. Italics use single asterisks. Headers are indicated by hash marks at the start of a line.
To make text italic, wrap it in single asterisks or underscores:
*italic text* or _italic text_For bold text, use double asterisks or underscores:
**bold text** or __bold text__You can combine them for bold italic:
***bold and italic***Headers structure your document into sections. Use hash marks at the start of a line, with more hashes indicating lower-level headers:
# First-Level Header
## Second-Level Header
### Third-Level HeaderCreate bullet lists by starting lines with dashes, asterisks, or plus signs:
- First item
- Second item
- Sub-item (indent with spaces or tabs)
- Another sub-item
- Third itemFor numbered lists, use numbers followed by periods:
1. First step
2. Second step
3. Third stepBlock quotes are useful for highlighting important passages or attributing quotes:
> "You know the greatest danger facing us is ourselves,
> an irrational fear of the unknown. But there's no such
> thing as the unknown — only things temporarily hidden,
> temporarily not understood."
>
> --- Captain James T. KirkThis renders as:
“You know the greatest danger facing us is ourselves, an irrational fear of the unknown. But there’s no such thing as the unknown — only things temporarily hidden, temporarily not understood.”
— Captain James T. Kirk
Links use square brackets for the text and parentheses for the URL:
[Link text](https://www.example.com)Images use the same syntax with an exclamation mark prefix:
Inline code is wrapped in single backticks: `code`. Code blocks use triple backticks, optionally specifying the language for syntax highlighting:
```r
x <- rnorm(100)
mean(x)
```LaTeX (pronounced “LAH-tek” or “LAY-tek”) is a document preparation system for high-quality typesetting, particularly of technical and scientific documents. Originally created by Leslie Lamport in the 1980s, LaTeX builds on the TeX typesetting system developed by Donald Knuth.
LaTeX excels at mathematical notation. Complex equations that would be tedious or impossible to create in a word processor can be expressed elegantly in LaTeX. The system handles numbering, cross-references, bibliographies, and other scholarly apparatus automatically.
Most importantly for our purposes, LaTeX can be embedded directly in Markdown documents. This gives us the best of both worlds: the simplicity of Markdown for prose and the power of LaTeX for mathematics.
Mathematical expressions can appear inline with text or as centered display equations. Inline math is wrapped in single dollar signs: $x^2$ produces \(x^2\). Display equations use double dollar signs:
$$E = mc^2$$Produces:
\[E = mc^2\]
Greek letters are typed as their names preceded by a backslash:
$$\alpha, \beta, \gamma, \delta, \epsilon$$
$$\mu, \sigma, \pi, \theta, \lambda$$\[\alpha, \beta, \gamma, \delta, \epsilon\] \[\mu, \sigma, \pi, \theta, \lambda\]
Common mathematical symbols:
$$\neq, \approx, \leq, \geq, \pm, \times, \div$$\[\neq, \approx, \leq, \geq, \pm, \times, \div\]
Superscripts use the caret ^ and subscripts use the underscore _:
$$x^2, x_i, x_i^2, x_{ij}$$\[x^2, x_i, x_i^2, x_{ij}\]
For multi-character superscripts or subscripts, use curly braces:
$$x^{n+1}, x_{i,j}$$\[x^{n+1}, x_{i,j}\]
Fractions use the \frac{numerator}{denominator} command:
$$\frac{a}{b}, \frac{x^2 + 1}{x - 1}$$\[\frac{a}{b}, \frac{x^2 + 1}{x - 1}\]
Square roots and nth roots:
$$\sqrt{x}, \sqrt[3]{x}, \sqrt[n]{x}$$\[\sqrt{x}, \sqrt[3]{x}, \sqrt[n]{x}\]
Sums and products with limits:
$$\sum_{i=1}^{n} x_i, \prod_{i=1}^{n} x_i$$\[\sum_{i=1}^{n} x_i, \prod_{i=1}^{n} x_i\]
Integrals with limits:
$$\int_{a}^{b} f(x) \, dx$$
$$\iint f(x,y) \, dx \, dy$$\[\int_{a}^{b} f(x) \, dx\] \[\iint f(x,y) \, dx \, dy\]
Here are some common statistical formulas in LaTeX:
The sample mean:
$$\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i$$\[\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i\]
The sample variance:
$$s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i - \bar{x})^2$$\[s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i - \bar{x})^2\]
The binomial probability formula:
$$P(X=k) = \binom{n}{k} p^{k} (1-p)^{n-k}$$\[P(X=k) = \binom{n}{k} p^{k} (1-p)^{n-k}\]
The Poisson probability formula:
$$P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}$$\[P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}\]
The normal distribution density:
$$f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$$\[f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\]
Matrices are created with the matrix environment:
$$\begin{matrix}
a & b \\
c & d
\end{matrix}$$\[\begin{matrix} a & b \\ c & d \end{matrix}\]
For brackets around the matrix, use pmatrix (parentheses) or bmatrix (square brackets):
$$\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}$$\[\begin{pmatrix} a & b \\ c & d \end{pmatrix}\]
Quarto and R Markdown extend Markdown by allowing you to embed executable code chunks. When the document is rendered, the code runs and its output—whether text, tables, or figures—is automatically included in the final document.
A Quarto document has three main parts:
---)The YAML header appears at the top of the document and configures how it will be rendered:
---
title: "My Analysis Report"
author: "Your Name"
date: today
format: html
---Common YAML options include:
| Option | Description |
|---|---|
title |
Document title |
author |
Author name(s) |
date |
Publication date (today for current date) |
format |
Output format: html, pdf, docx |
toc |
Include table of contents (true/false) |
code-fold |
Collapse code by default |
execute |
Global code execution options |
A more complete header might look like:
---
title: "Genomic Analysis Report"
author: "Jane Scientist"
date: today
format:
html:
theme: cosmo
toc: true
code-fold: true
execute:
echo: true
warning: false
---A code chunk in Quarto looks like this:
```{r}
x <- rnorm(100)
mean(x)
```When rendered, this shows both the code and its output:
x <- rnorm(100)
mean(x)[1] -0.001136062
Chunk options control how code is displayed and executed. In Quarto, options are specified with #| comments at the start of the chunk:
```{r}
#| label: my-analysis
#| echo: false
#| fig-width: 6
#| fig-height: 4
hist(rnorm(1000))
```Essential chunk options:
| Option | Description | Default |
|---|---|---|
echo |
Show code in output | true |
eval |
Execute the code | true |
include |
Include chunk in output | true |
warning |
Show warnings | true |
message |
Show messages | true |
fig-cap |
Figure caption | none |
fig-width |
Figure width (inches) | 7 |
fig-height |
Figure height (inches) | 5 |
Quarto provides special callout blocks for highlighting information:
::: {.callout-note}
This is a note with additional information.
:::
::: {.callout-warning}
This warns readers about potential issues.
:::
::: {.callout-tip}
This provides helpful tips.
:::These render as visually distinct boxes that draw attention to important content.
This is an example of a callout note—useful for supplementary information.
This is a warning callout—use for potential pitfalls or important cautions.
Quarto supports cross-references to figures, tables, and sections. Label items and reference them with @:
```{r}
#| label: fig-scatter
#| fig-cap: "Relationship between x and y"
plot(x, y)
```
See @fig-scatter for the scatter plot.For sections, add a label after the header:
## Methods {#sec-methods}
As described in @sec-methods, we used...To render a Quarto document:
Ctrl+Shift+K (Windows/Linux) or Cmd+Shift+K (Mac)quarto render document.qmdYou can render to multiple formats:
quarto render document.qmd --to html
quarto render document.qmd --to pdf
quarto render document.qmd --to docxThe combination of Markdown, LaTeX, and executable code enables truly reproducible research. Your analysis code lives in the same document as your prose. When data change, you re-render the document and everything updates automatically. Collaborators can see exactly what you did. Future you can understand past you’s work.
These tools have become standard in data science. Learning them now will pay dividends throughout your career, whether you are writing homework assignments, thesis chapters, journal articles, or technical reports.
Create a new R Markdown document and practice:
#, ##, and ###Experiment with code chunk options:
echo=TRUE and eval=TRUE (default behavior)echo=FALSE—what happens?eval=FALSE—what happens?fig.width and fig.height to control plot dimensionsPractice writing equations in LaTeX:
$...$) and display math ($$...$$)Create tables using:
kable() function from the knitr packagelibrary(knitr)
kable(head(iris, 5))