6  Tips with R Markdown

Author

R Félix

6.1 Source vs. Visual mode

You may want to work directly in the Visual mode, to “what you see is what you get”, and switch to the Source mode for very specific code things.

Source mode

Visual mode

6.2 Suggestions

/

Type Ctrl + / to search for anything you want to include

6.3 Cross-references

You can refer to sections, figures, tables, and equations in your document using the @ref syntax.

6.3.1 With Sections

You can refer to sections like this: [Section 1](#Introduction). This will show as “Section 1”.

6.3.2 With figures

You can refer to figures like this: \@ref(fig:plot1). This will show as “Figure 1”. For that, you need to add a label to the figure chunk, like this: {#fig:plot1}. or fig.cap="\label{plot1} Caption of the plot" in your r chunk.

Code
plot(cars)

6.3.3 With tables

You can refer to tables like this: \@ref(tab:table1). This will show as “Table 1”. For that, you need to add a label to the table caption like this:

Code
# example with kable
knitr::kable(head(iris),
             caption = "\\label{table1}Caption of the table", # label here
             fotmat = latex,
             booktabs = TRUE)

You don’t need to use an ordered number, but just a name/label to your figures and tables. R markdown will handle then the order of them and provide them sequential numbering.

6.4 Create a table of contents

You can create a table of contents (toc) with the headings of your document, a list of tables (lot) or a list of figures (lof), simply by adding the following in the YAML:

---
title: "My Report"
author: "Your Name"
date: "2024-12-10"
output: pdf_document
toc: true
numbersections: true
number-depth: 3
lof: true
lot: true
---

6.5 Include an image

To include a image that is not produced with R or Python (for instance a methods flowchart), you can click on insert image in Visual mode, browse your image file, adjust the size, and provide a proper caption.

If it is produced with R or Py, simply let your code do it for you. Example:

Code
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()

A proper caption for a ggplot.

6.5.1 Image position

By default, LaTeX puts your images on the top of a page, or arranges them in order to get the minimum white spaces possible. That is why LaTeX documents look so nice an pretty.

Nevertheless, you can force the position of your image by adding the following to your chunk: fig.pos = "H". Other options:

  • h: Place the float here, i.e., approximately at the same point it occurs in the source text.
  • t: Position at the top of the page (default)
  • b: Position at the bottom of the page.
  • p: Put on a special page for floats only.
  • !: Override internal parameters LaTeX uses for determining “good” float positions.
  • H: Place the float at precisely the location in the LaTeX code. This requires the float package (\usepackage{float}).

6.6 Inline math

You can use your results along the text, so every time you run the document, the results are also updated. Example:

Results show that 53.9448583% of the population uses car as a main mode of transport. 3+2 = 5 is a nice number.

This is how you would write it:

Results show that 73.1769811 % of the population uses car as a main mode of transport. 3+2 = 5 is a nice number.

You can directly call your previously set variables.

6.7 LaTeX packages

Sometimes, you need to use some LaTeX packages to customize your document. To use those, you can add the following in the YAML:

preamble: |
  \biboptions{authoryear}
  \usepackage{url}
  \usepackage{pdflscape}
  \usepackage{float}
  \usepackage{booktabs}
  \usepackage{longtable}
  \usepackage{makecell}
  \usepackage{multirow}

6.8 Awsome tables with kbl

kable and kableExtra allows to create pretty tables with R.

Code
library(kableExtra)
cardata = mtcars[1:5, 1:6]

kbl(cardata,
    caption = "An awsome table about cars.") |>
  kable_styling() 
An awsome table about cars.
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

Check out the documentation for .pdf format: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

6.9 Change colors of references and links

To color your references and urls you can add the following to your yaml:

---
link-citations: true
linkcolor: teal
urlcolor: teal
---

6.10 Add a landscape page

To include a landscape page in your pdf, for instance to present a very large table, you should use the \usepackage{lscape} in your yaml preamble, and add the following code in Source mode, before and after your table chunk:

\begin{landscape}

Your code to make a large table

\end{landscape}

6.11 Converting to word .doc

It can be handful to convert your nice .pdf to a .doc file to send it to your supervisor, instead of a LaTeX or pdf file.

To do so, you can simply open your favorite word processor and open your pdf file inside. Click OK to convert when prompt.

Example of this pdf book opened in MS Word

Make some adjustments (sometimes it requires margins, spacing or line numbers), save it as .doc or .docx, and you are ready to go!

No headings, table of contents, citations, cross-references, figures, tables are missing.

6.12 Elsevier paper example

Example of a paper written in R Markdown and submitted in pdf and LaTeX for the Elsevier journal Computers, Environment and Urban Studies:
https://github.com/U-Shift/biclar/blob/master/paper/PaperCEUS/PaperCEUS.Rmd

This can be useful to get an idea how things work, and compare with the final result (Félix, Moura, and Lovelace 2025).

6.13 Knitr in Project vs. Document

If your R Markdown (.Rmd) document is not in the root of your RStudio Project (.Rproj ) (i.e. - in the same folder), and if you have data or images that are in paths reffered to the project, you may want to change the knit configurations for it to work.

Go to Tools > Global options > R Markdown

In that case select Evaluate chunks in Project directory.

6.14 Source Files when submitting to a journal

Usually journals accept the latex file (.tex) along the .pdf.

For that, you should prepare a SourceFiles.zip including:

  • The LaTeX file paper.tex

  • The used bibliography references.bib

  • A citation style if used transportation_research.csl

  • Any images files in a img folder

  • The paper.pdf file