R Markdown List



Here is a brief introduction to using R Markdown.Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents and much, much more.R Markdown provides the flexibility of Markdown with the implementation of R input and output. For more details on using R Markdown see http://rmarkdown.rstudio.com.

List

New Reddit-flavored Markdown. Markdown is the format in which content is written on Reddit. This is a guide to Reddit's particular flavor of Markdown, updated for New Reddit, and current as of September 19, 2018. Browse other questions tagged r markdown or ask your own question. The Overflow Blog Level Up: Creative Coding with p5.js – parts 4 and 5. This is a good option to write any new documentation for existing packages in markdown. There is also a new @noMd tag. Use this if you turned on markdown parsing globally, but need to avoid it for a single chunk. This tag is handy if the markdown parser interferes with more complex Rd syntax. Here is an example roxygen chunk that uses markdown. R Markdown: The Definitive Guide Yihui Xie; J.J. Allaire; Garrett Grolemund download Z-Library. Download books for free. The first official book authored by the core R Markdown developers that provides a comprehensive and accurate reference to the R Markdown ecosystem. With R Markdown, you can easily create.

1.1 Basic markdown syntax

1.1.1 Whitespace

Be careful with your spacing.While whitespace largely is ignored, it does at times give markdown signals as to how to proceed.As a habit, try to keep everything left aligned whenever possible, especially as you type a new paragraph.In other words, there is no need to indent basic text in the Rmd document (in fact, it might cause your text to do funny things if you do).

1.1.2 Italics and bold

  • Italics are done like *this* or _this_
  • Bold is done like **this** or __this__
  • Bold and italics is done like ***this***, ___this___, or (the most transparent solution, in my opinion) **_this_**

1.1.3 Inline code

  • Inline code is created with backticks like `this`

1.1.4 Sub and superscript

Sub2 and super2 script is created like this~2~ and this^2^

R Markdown List

1.1.5 Strikethrough

  • Strikethrough is done ~~like this~~

1.1.6 ‘Escaping’ (aka “What if I need an actual asterisk?”)

  • To include an actual *, _ or , add another in front of them: *, _,

1.1.7 Endash (–), emdash (—)

  • – and — with -- and ---

1.1.8 Blockquotes

Do like this:

Put a > in front of the line.

1.1.9 Headings

Section headers are created with #’s of increasing number, i.e.

  • # First-level heading
  • ## Second-level heading
  • ### Etc.

In PDF output, a level-five heading will turn into a paragraph heading, i.e. paragraph{My level-five heading}, which appears as bold text on the same line as the subsequent paragraph.

1.1.10 Lists

Unordered list by starting a line with an * or a -:

  • Item 1
  • Item 2

Ordered lists by starting a line with a number.Notice that you can mislabel the numbers and Markdown will still make the order right in the output:

  1. Item 1
  2. Item 2

To create a sublist, indent the values a bit (at least four spaces or a tab):

  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

R Markdown Tutorial

1.1.11 Line breaks

The official Markdown way to create line breaks is by ending a line with more than two spaces.

Roses are red.Violets are blue.

This appears on the same line in the output, because we didn’t add spaces after red.

Roses are red.
Violets are blue.

This appears with a line break because I added spaces after red.

List

I find this is confusing, so I recommend the alternative way: Ending a line with a backslash will also create a linebreak:

Roses are red.
Violets are blue.

To create a new paragraph, you put a blank line.

Therefore, this line starts its own paragraph.

R Markdown List Levels

1.1.12 Hyperlinks

  • This is a hyperlink created by writing the text you want turned into a clickable link in [square brackets followed by a](https://hyperlink-in-parentheses)

1.1.13 Footnotes

  • Are createdmy footnote text'>1 by writing either ^[my footnote text] for supplying the footnote content inline, or something like [^a-random-footnote-label] and supplying the text elsewhere in the format shown below:This is a random test.'>2

[^a-random-footnote-label]: This is a random test.

1.1.15 Math

The syntax for writing math is stolen from LaTeX. To write a math expression that will be shown inline, enclose it in dollar signs.- This: $A = pi*r^{2}$ Becomes: (A = pi*r^{2})

To write a math expression that will be shown in a block, enclose it in two dollar signs.
This: $$A = pi*r^{2}$$

Becomes:[A = pi*r^{2}]

To create numbered equations, put them in an ‘equation’ environment and give them a label with the syntax (#eq:label), like this:

List

Becomes:[begin{equation}fleft(kright)=binom{n}{k}p^kleft(1-pright)^{n-k}tag{1.1}end{equation}]

For more (e.g. how to theorems), see e.g. the documentation on bookdown.org

1.2 Executable code chunks

List In R Markdown

The magic of R Markdown is that we can add executable code within our document to make it dynamic.

We do this either as code chunks (generally used for loading libraries and data, performing calculations, and adding images, plots, and tables), or inline code (generally used for dynamically reporting results within our text).

The syntax of a code chunk is shown in Figure 1.1.

Common chunk options include (see e.g. bookdown.org):

  • echo: whether or not to display code in knitted output
  • eval: whether or to to run the code in the chunk when knitting
  • include: whether to include anything from the from a code chunk in the output document
  • fig.cap: figure caption
  • fig.scap: short figure caption, which will be used in the ‘List of Figures’ in the PDF front matter

IMPORTANT: Do not use underscoores in your chunk labels - if you do, you are likely to get an error in PDF output saying something like “! Package caption Error: caption outside float.”

1.2.1 Setup chunks - setup, images, plots

An R Markdown document usually begins with a chunk that is used to load libraries, and to set default chunk options with knitr::opts_chunk$set.

In your thesis, this will probably happen in index.Rmd and/or as opening chunks in each of your chapters.

1.2.2 Including images

Code chunks are also used for including images, with include_graphics from the knitr package, as in Figure 1.2

Figure 1.2: Oxford logo

Useful chunk options for figures include:

  • out.width (use with a percentage) for setting the image size
  • if you’ve got an image that gets waaay to big in your output, it will be constrained to the page width by setting out.width = '100%'

Figure rotation

Markdown

You can use the chunk option out.extra to rotate images.

The syntax is different for LaTeX and HTML, so for ease we might start by assigning the right string to a variable that depends on the format you’re outputting to:

Then you can reference that variable as the value of out.extra to rotate images, as in Figure 1.3.

Figure 1.3: Oxford logo, rotated

1.2.3 Including plots

Similarly, code chunks are used for including dynamically generated plots.You use ordinary code in R or other languages - Figure 1.4 shows a plot of the cars dataset of stopping distances for cars at various speeds (this dataset is built in to R).

Figure 1.4: A ggplot of car stuff

Under the hood, plots are included in your document in the same way as images - when you build the book or knit a chapter, the plot is automatically generated from your code, saved as an image, then included into the output document.

1.2.4 Including tables

Tables are usually included with the kable function from the knitr package.

Table 1.1 shows the first rows of that cars data - read in your own data, then use this approach to automatically generate tables.

Table 1.1: A knitr kable table
speeddist
42
410
74
722
816
910
  • Gotcha: when using kable, captions are set inside the kable function
  • The kable package is often used with the kableExtra package

1.2.5 Control positioning

One thing that may be annoying is the way R Markdown handles “floats” like tables and figures.In your PDF output, LaTeX will try to find the best place to put your object based on the text around it and until you’re really, truly done writing you should just leave it where it lies.

In general, you should allow LaTeX to do this, but if you really really need a figure to be positioned where you put in the document, then you can make LaTeX attempt to do this with the chunk option fig.pos='H', as in Figure 1.5:

Figure 1.5: An Oxford logo that LaTeX will try to place at this position in the text

As anyone who has tried to manually play around with the placement of figures in a Word document knows, this can have lots of side effects with extra spacing on other pages, etc.Therefore, it is not generally a good idea to do this - only do it when you really need to ensure that an image follows directly under text where you refer to it (in this document, I needed to do this for Figure 3.1 in section 3.1.4).For more details, read the relevant section of the [R Markdown Cookbook]https://bookdown.org/yihui/rmarkdown-cookbook/figure-placement.html).

1.3 Executable inline code

‘Inline code’ simply means inclusion of code inside text.The syntax for doing this is `r R_CODE`For example, `r 4 + 4` will output 8 in your text.

You will usually use this in parts of your thesis where you report results - read in data or results in a code chunk, store things you want to report in a variable, then insert the value of that variable in your text.For example, we might assign the number of rows in the cars dataset to a variable:

We might then write:
“In the cars dataset, we have `r num_car_observations` observations.”

Which would output:
“In the cars dataset, we have 50 observations.”

1.4 Executable code in other languages than R

If you want to use other languages than R, such as Python, Julia C++, or SQL, see the relevant section of the R Markdown Cookbook

In general, I find that a single R Markdown file quickly becomes unwieldy. I recommend breaking the document up into multiple “child” documents and sourcing these child documents in a parent document. My child documents generally represent major subsections of the document.

I prefer to store the parent R Markdown file in a folder labeled “markdown” (rproject/markdown) and the child R Markdown files in a sub-directory of my “markdown” folder called “sections” (rproject/markdown/sections). In the parent file, the child files are sourced within the code chunk header using child = ‘sections/example.Rmd’. After sourcing all the child chunks, the parent file can be knit (compiled) like a normal R markdown document. The child documents cannot be run in the parent file.

11.1 Extract and Run R-Code from R Markdown Files

The parent file is great for organizing sections of your document, but the child documents cannot be executed within R Studio like a normal code chunk. Without the ability to easily execute the R code within the child documents it can become very difficult to develop new child documents because new child documents often depend on upstream code execution.

Imagine you have a parent document that sources child sections which import your data and clean your data. You now want to visualize your data; accordingly, you begin to develop a visualization child document, which depends on information from the upstream child sections. It would be inefficient and inappropriate to perform all the steps in the upstream child sections within the visualization section. Therefore, you need an effective way to execute the upstream child sections while you continue to develop the visualization section. The inefficient way of doing this is to open each child Rmd file in R Studio and execute them manually in the correct sequence. This becomes tedious after you have three or more documents (imagine doing this for 10+ child sections). The most efficient way that I have found to run upstream child sections is to extract the R-code chunks from each Rmd file, save them in a “raw_scripts” folder, and then source/execute the scripts within a regular R script file (.R).

11.1.1 R Code

In this section we establish the file path to the folder that contains all the child documents. The names of the child documents are extracted and stored as a vector. The grepl() function is used to retain only the Rmd files stored in the vector.

Next, a file path is specified for the R-scripts that will be extracted from the R Markdown documents; I place these files within a “raw_script/extracted” folder. The map() function from the purrr package is used to loop through each file in the vector (r.files.vec). Within the map() loop, the purl() function from knitr is used to extract the R-code from the R Markdown documents and save the code to the specified folder.

Finally, create a vector of file names (source.vec) stored in the “raw_script/extracted” folder. You will want to type these out manually (do not use list.files() functions) because in this format you can easily comment out certain scripts and only run the scripts of interest. map() is then used to loop through each specified file in source.vec. Keep in mind that the order of the file names specified in source.vec will determine the order that these files are executed in the map() function; therefore, order the files in source.vec from furthest upstream to furthest downstream. Each iteration of the loop, executes (sources) the specified R-script.

Once all the R-scripts extracted from the upstream child R Markdown files have been executed, you can begin or continue work on a new child R Markdown document. I keep all the above code in a single R-script and execute the entire script each time I use this file to make sure all of the files are up-to-date.

Lists R Markdown

11.2 Your Turn

R Markdown List

  1. Create an R Markdown document for each of the sections, designated by Header level 1s, we have created thus far.
    • save the documents within your project root (where your .Rproj file lives)
  2. Copy the Header and the relevant content below the header. Paste the content into the appropriate R Markdown document.
  3. Create a parent R Markdown document.
    • create a code chunk for each section.
    • add child = ‘insert-file-name-here.Rmd’ to the header of the appropriate code chunk
  4. Knit the document