5.1 Font color
Rmarkdown Font Size
R Markdown is a format for writing reproducible, dynamic reports with R. Use it to embed R code and results into slideshows, pdfs, html documents, Word files and more. To make a report. Tomighty. Markdown is a lightweight markup language for creating formatted text using a plain-text editor. John Gruber and Aaron Swartz created Markdown in 2004 as a markup language that is appealing to.
R Markdown Language
The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words:
- For HTML, we can wrap the text in the
<span>
tag and set color with CSS, e.g.,<span>text</span>
. - For PDF, we can use the LaTeX command
textcolor{}{}
. This requires the LaTeX package xcolor, which is included in Pandoc’s default LaTeX template.
As an example of changing the color in PDF text:
In the above example, the first set of curly braces contains the desired text color, and the second set of curly braces contains the text to which this color should be applied.
If you want to design an R Markdown document for multiple output formats, you should not embed raw HTML or LaTeX code in your document, because they will be ignored in the other output formats (e.g., LaTeX code will be ignored in HTML output, and HTML tags will be lost in LaTeX output). Next, we provide two possible methods to deal with this issue.
![Converter Converter](https://commongroundchristian.com/wp-content/uploads/2015/04/img_4907.jpg)
5.1.1 Using an R function to write raw HTML or LaTeX code
We can write a custom R function to insert the correct syntax depending on the output format using the
is_latex_output()
and is_html_output()
functions in knitr as follows:![Install Install](https://rworkshop.uni.lu/lectures/img/04/rmd_example.png)
We can then use the code in an inline R expression
`r colorize('some words in red', 'red')`
, which will create some words in red Frndly on apple tv. (you will not see the red color if you are reading this book printed in black and white).5.1.2 Using a Pandoc Lua filter (*)
This method may be a little advanced for R users because it involves another programming language, Lua, but it is extremely powerful—you can programmatically modify Markdown elements via Pandoc’s Lua filters (see Section 4.20). Below is a full example:
In this example, we implicitly used a Pandoc Markdown extension named
bracketed_spans
, which allows us to write text with attributes, e.g., [text]{.class attribute='value'}
. The Lua filter defined in the cat
code chunk7 puts text in <span></span>
if the output format is HTML, and in textcolor{..}{}
if the output format is LaTeX. The Lua filter is written to a file color-text.lua
, and enabled through the command-line option --lua-filter
passed to Pandoc via the pandoc_args
option of the output formats.Compared to the previous method, the advantage of using the Lua filter is that you can still use Markdown syntax inside the brackets, whereas using the R function
colorize()
in the previous section does not allow Markdown syntax (e.g., colorize('**bold**')
will not be bold).2.2 Compile an R Markdown document
The usual way to compile an R Markdown document is to click the
Knit
button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K
(Cmd + Shift + K
on macOS). Under the hood, RStudio calls the function rmarkdown::render()
to render the document in a new R session. Please note the emphasis here, which often confuses R Markdown users. Rendering an Rmd document in a new R session means that none of the objects in your current R session (e.g., those you created in your R console) are available to that session.2 Reproducibility is the main reason that RStudio uses a new R session to render your Rmd documents: in most cases, you may want your documents to continue to work the next time you open R, or in other people’s computing environments. See this StackOverflow answer if you want to know more.If you must render a document in the current R session, you can also call
rmarkdown::render()
by yourself, and pass the path of the Rmd file to this function. The second argument of this function is the output format, which defaults to the first output format you specify in the YAML metadata (if it is missing, the default is html_document
). When you have multiple output formats in the metadata, and do not want to use the first one, you can specify the one you want in the second argument, e.g., for an Rmd document foo.Rmd
with the metadata:You can render it to PDF via:
The function call gives you much more freedom (e.g., you can generate a series of reports in a loop), but you should bear reproducibility in mind when you render documents this way. Of course, you can start a new and clean R session by yourself, and call
rmarkdown::render()
in that session. As long as you do not manually interact with that session (e.g., manually creating variables in the R console), your reports should be reproducible.Another main way to work with Rmd documents is the R Markdown Notebooks, which will be introduced in Section 3.2. With notebooks, you can run code chunks individually and see results right inside the RStudio editor. This is a convenient way to interact or experiment with code in an Rmd document, because you do not have to compile the whole document. Without using the notebooks, you can still partially execute code chunks, but the execution only occurs in the R console, and the notebook interface presents results of code chunks right beneath the chunks in the editor, which can be a great advantage. Again, for the sake of reproducibility, you will need to compile the whole document eventually in a clean environment.
Lastly, I want to mention an “unofficial” way to compile Rmd documents: the function
xaringan::inf_mr()
, or equivalently, the RStudio addin “Infinite Moon Reader.” Obviously, this requires you to install the xaringan package (Xie 2021e), which is available on CRAN. The main advantage of this way is LiveReload: a technology that enables you to live preview the output as soon as you save the source document, and you do not need to hit the Knit
button. The other advantage is that it compiles the Rmd document in the current R session, which may or may not be what you desire. Note that this method only works for Rmd documents that output to HTML, including HTML documents and presentations.A few R Markdown extension packages, such as bookdown and blogdown, have their own way of compiling documents, and we will introduce them later.
Note that it is also possible to render a series of reports instead of single one from a single R Markdown source document. You can parameterize an R Markdown document, and generate different reports using different parameters. See Chapter 15 for details.