You have already learned how to do basic formatting of R Markdown document and you can also take a look at the .Rmd files that I post with each lecture as this allows you to see my code and exactly how I format my R Markdown documents.
Below are just a few basics that you will likely use when formatting your final project. Make sure to look at the .Rmd version of this file so that you can see exactly how the code is implemented.
When loading in libraries in your final project you can specify
message = FALSE
so that the messages that print when you
typically load in a library are not displayed in your knit html
document. This makes you document look nicer.
Chunk options are specified right at the top of your code chunk –
directly within the {r}
portion. Thus to when specifying
message = FALSE
the top of your code chunk would look
like:
{r message = FALSE}
Below, I’ve loaded in tidyverse
and suppressed the
messages.
library(tidyverse)
Sometimes when you run code you will get warnings that inform you of
something to be aware of (e.g. points not shown in plots or values
dropped). It is good to be aware of these warnings and make sure that
they are not actually a problem with respect to your analysis. However,
oftentimes the warning, while useful to be aware of, is not actually a
problem and thus you do not want to see it in your final knit document.
To suppress warning when your code is knit yo use the
warning = FALSE
chunk option.
{r warning = FALSE}
echo = FALSE
hides the code in your knit document,
however the code is still run.
{r echo = FALSE}
You can combine multiple chunk options
{r message = FALSE, echo = FALSE}
There are a number of other chunk options available. The table below shows these options (adapted from R4DS)
Option | Run code | Show code | Output | Plots | Messages | Warnings |
---|---|---|---|---|---|---|
eval = FALSE |
N | Y | N | N | N | N |
include = FALSE |
Y | N | N | N | N | N |
echo = FALSE |
Y | N | Y | Y | Y | Y |
results = "hide" |
Y | Y | N | Y | Y | Y |
fig.show = "hide" |
Y | Y | Y | N | Y | Y |
message = FALSE |
Y | Y | Y | Y | N | Y |
warning = FALSE |
Y | Y | Y | Y | Y | N |
If you want to generate a table or figure – but want to have it displayed later in the document, you can save the figure or table to an R object
fig_1 <- ggplot(...) + ...
table_1 <- code that generates your table
Then later on in the document you can display the figure or table by
simply typing fig_1
or table_1
in your code
chunk
To explore the many formatting and output options for R Markdown documents you should look at the freely available book R Markdown: The Definitive Guide. This book is an excellent resource that provides a range of easy to follow examples that will allow you to create professionally formatted documents from your Rmd files.
In particular take a look at chapter 3, which covers formatting HTML document output (which is what we have been doing in this class) when knitting your Rmd file. This chapter shows you how to implement many useful formatting features and I strongly recommend you look through this chapter and apply features that you think will be helpful.
Below I mention a few notable document features that you might be interested in (though consult chapter 3 for a wide range of options).
You should also consult your R Markdown cheatsheet, which has a bunch of helpful examples for formatting your documents (e.g., including equations, adding images, text formatting, lists, footnotes,…).
Themes set the style of the document (e.g., font type, font colors, section header styles, headers/footers,…). There are a number of built-in themes, which you can see here and you can see how to set these themes in your Rmd file by following the examples here.
You can add a table of contents (which I add to all of our lecture documents) by modifying the YAML header at the top of your Rmd file. Take a look at chapter 3.1.1 for details.
If you are including tables of output/results in your knit document,
you should consider using the kable
and
kableExtra
packages. These packages allow you to control
table formatting in your HTML file (e.g., fonts, colors, table
dimensions, interactive/responsive tables,…). You can find a tutorial
on these packages here.