R
code.introDS
package by using eitherRaw code
shows the original R code that generates the output, Compiled html
shows the compiled output based on the Raw code
, and BibTex file
shows the formatting lists of references.Code
in the toolbar for some example code:Compiled html
, we can see the following output:Code
and Emoji
in the toolbar, we can see the following R code used:File
→ New File
→ R Markdown
. Give the file a title and the author(your name) and select the default output, HTML. We can change this later so don’t worry about it for the moment.pdf_document
to output a pdf. However, the pdf format requires additional installation and configuration of a TeX distribution such as MikTeX. Once available, the user can also include raw LaTeX and even define LaTeX macros in the RMarkdown document if necessary (we’ll discuss more about LaTeX further on).number_sections: yes
as part of the YAML Metadata as shown below.code style
. To understand how to format text, our previous sentence would be typed out as follows in RMarkdown:#
produces the largest heading text while, to produce smaller headings, you simply need to add more #
s! Heading level also impacts section and subsection nesting in documents and tables of contents, as well as slide breaks in presentation formats.$math expression$
. For example, if we want to write “where (alpha) is in degrees” we would write:$$math expression$$
) we can obtain centered mathematical expressions. For example, the binomial probability distribution in LaTeX is written as$$f(y|N,p) = frac{N!}{y!(N-y)!}cdot p^y cdot (1-p)^{N-y} = {{N}choose{y}} cdot p^y cdot (1-p)^{N-y}$$
@ref(label)
to reference sections, where label is the section identifier (ID). By default, Pandoc will generate IDs for all section headers, e.g., # Hello World
will have an ID hello-world
. To call header hello-world
as a header, we type @ref(hello-world)
to cross-reference the section. In order to avoid forgetting to update the reference label after you change the section header, you may also manually assign an ID to a section header by appending {#id} to it.bibliography: biblio.bib
and your RMarkdown will now be able to recognize the references within your “.bib” file.@
symbol (without spaces). So, for example, say that we wanted to cite the publication on the “taste of cutlery”: in your RMarkdown all you have to do is to type @harrar2013taste
at the point where you want this citation in the text and you will obtain: Harrar and Spence (2013). Moreover, it is often useful to put a citation in braces and for example if you want to obtain (see e.g. Harrar and Spence 2013) you can simply write [see e.g. @harrar2013taste]
.Fruit | Price | Advantages |
---|---|---|
Bananas | $1.34 |
|
Oranges | $2.10 |
|
knitr::kable()
which we will explain later on this book.echo = FALSE
which is a parameter that was added to the code chunk to prevent printing the R code that generated the plot. This is a useful way to embed figures. More options can be found from the RMarkdown Cheatsheet and Yihui’s notes on knitr options. Here are some explanations of the most commonly used chunk options taken from these references:eval
: (TRUE; logical) whether to evaluate the code chunk;echo
: (TRUE; logical or numeric) whether to include R source code in the output file;warning
: (TRUE; logical) whether to preserve warnings (produced by warning()) in the output like we run R code in a terminal (if FALSE, all warnings will be printed in the console instead of the output document);cache
: (FALSE; logical) whether to “cache” a code chunk. This option is particularly important in practice and is discussed in more details in Section 2.4.4.fig.path
: (‘figure/’; character) prefix to be used for figure filenames (fig.path and chunk labels are concatenated to make filenames);fig.show
: (‘asis’; character) how to show/arrange the plots;fig.width
, fig.height
: (both are 7; numeric) width and height of the plot, to be used in the graphics device (in inches) and have to be numeric;fig.align
: (‘default’; character) alignment of figures in the output document (possible values are left, right and center;fig.cap
: (NULL; character) figure caption to be used in a figure environment.x
to which we assign a specific value, then in RMarkdown we can reference this variable by using r x
: this will affix the value of the variable directly in a sentence. Here is a practical example:a
. We can use the value of a
as follows:cache
argument. By default this argument is assigned the value FALSE
and therefore the R code is run every time your document is compiled. However, if you specify this argument as cache = TRUE
, then the code is only run the first time the document is compiled while the following times it simply stores and presents the results of the computations when the document was first compiled.a
(that we saw earlier). This is clearly not the best example since this operation runs extremely quickly and there is no visible loss in document compilation time. However, we will use it just to highlight how the cache
argument works. Therefore, if we want to avoid running this operation each time the document is compiled, then we just embed our R code as follows:computeA
and the reason for this will become apparent further on. Once we have done this we can compile the document that will run this operation and store its result. Now, if we compile the document again (independently from whether we made changes to the rest of the document or not) this operation will not be run and the result of the previous (first) compiling will be presented. However, if changes are made to the R code which has been “cached”, then the code will be run again and this time its new result will be stored for all the following compilings until it is changed again.a
from the previous chunk:## 4
since a <- 2
(from the previous chunk). What happens however if we modify the value of a
in the previous chunk? In this case, the previous chunk will be recomputed but the value of d
(in the following chunk) will not be updated since it has stored the value of 4 and it is not recomputed since this chunk has not been modified. To avoid this, a solution is to specify the chunks of code that the “cached” code depends on. This is why we initially gave a name to the first chunk of code (“computeA”) so as to refer to it in following chunks of “cached” code that depend on it. To refer to this code you need to use the option dependson
as follows:a
changes in the first chunk, the value of d
will also change but will be stored until either the computeA
chunk or the latter chunk is modified.RMarkdown::render()
or click the Knit
button at the top of the RStudio scripts pane to save the output in your working directory.pandoc
. Pandoc is a free software that understands and converts useful markdown syntax, such as the code mentioned above, into a readable and clean format.