Reproducible research in R

  1. Creating the document

Reproducible Research-Credit Stanford University

This is the first of a series of post that intends to initiate animal production experts to R software and RMarkdown to reproducible research.

A. Creating an R Markdown file

Within R Studio, click File → New File → R Markdown and you’ll get a dialog box like this:

Option panelDialogue box
Creating file
Dialog box

B. Information of your document

1. YAML Metadata

YAML (Ain’t Markup Language): The header that tells R Markdown how to generate your document. Indentation and spacing are very important. More important, we can add specific options to the header of the document as for example:

1.1. Parameters:

  • Declaring Parameters:

More advanced paramterized report use is published by Yihui et al., (2020). Parameters are declared using the params field within the YAML header of the document. We can also use R objects by including !r before R expressions.

  • Using parameters

We can access the parameters within the knitting environment and the R console in RStudio by using r params$ParameterToUse.

  • Knitting with parameters

There are three ways in which a parameterized report can be knitted:

  1. Using the Knit button within RStudio.

  2. r rmarkdown::render() with the params argument.

  3. Using an interactive user interface to input parameter values.

2. R code chunks

```{r myChunk,echo=FALSE, include= FALSE, eval = TRUE}
x <- c(1, 2, 3)
  • myChunk: name your chunk
  • echo = FALSE: don’t show the code itself in the final document
  • include = FALSE: don’t include this code or its output in the final document
  • eval = FALSE: don’t actually run this code at all

In the next post we will wright our first summary report.

2.1. Chunk otpions

The general chunk option is set using the following chunk.

{r}
knitr::opts_chunk$set(echo=TRUE)

However, there is mulipple options that could be used to cutomize your chunk printouts:

Chunk optionOutput result
1. echo = FALSEHide source code
2. results = FALSE or 'hide'Hide text output
3. message = FALSEHide message
4. warning = FALSEHide warning messages
6. include = FALSEHide everything from a chunk
7. collapse = TRUECollapse text output blocks into source blocks
8. comment = ""Remove leading hashes in text output
9. prompt = TRUEMimic the behavior of the R console
10. .classNameAdd attributes to text output blocks
11. style="background: pink;"Change the background color of source blocks to pink

You can organize content using tabs by applying the .tabset class attribute to headers within a document. This will cause all sub-headers of the header with the .tabset attribute to appear within tabs rather than as standalone sections.

2.2. Tables and Figures options

Chunk optionOutput result
1. fig.show = 'hide'Hide plots
2. fig.width = integerSets the figure width
3. fig.height = integerStes the figure height
4. fig.dim = c(6, 4)Figure dimensions, perform the same taks as the last two agruments
5. fig.align = c('left', 'center', or 'right')The alignment of plots
6. par(mar = c())Figure position
7. :-------Align left the table
8. -------:Alig right the table
9. :-------:Center the table
10. knitr::kable(iris[1:5, ], caption = 'A caption')Print the tables in kable format

3. Text

The best reference for text styling is the briefest ever intro to Markdown syntax where you can find exhaustif text formating. In this post we will state the main aspects of text styling that can be used. We will briefly present some basic text formating in this post:

3.1. Inline formatting

  • To mark text as inline code, use a pair of backticks `code`
  • Italic if surrounded by underscores or asterisks, _text_or *text*
  • Bold text is produced using a pair of double asterisks **text**
  • A pair of tildes (~) turn text to a subscript H~3~PO~4~ renders H3PO4)
  • A pair of carets (^) produce a superscript Cu^2+^ renders Cu2+)
  • Hyperlinks are created using the syntax [text](link)
  • Import the images ![alt text or image title](path/to/image)
  • Footnotes are put inside the square brackets after a caret ^[], ^[This is a footnote.].

3.2. Block-level elements

Section headers can be written after a number of pound signs #, which produce a numbered header

  • # First-level header

  • ## Second-level header

  • ### Third-level header

  • Non-numbered headers we add {-}

  • Unordered list items start with *, -, or + and you can nest one list within another list by indenting the sub-list

- one item
- one item
- one item
    - one more item
    - one more item
    - one more item
  • Oredrerd list:
1. the first item
2. the second item
3. the third item
    - one unordered item
    - one unordered item

4. Mixed options

Code results can be inserted directly into the text of a .Rmd file (Inline code) by enclosing the code with "`r `". R Markdown will always display the results of inline code, but not the code applies relevant text formatting to the results. As a result, inline output is indistinguishable from the surrounding text. However, inline expressions do not take knitr options.

logo

Creative Commons Licence
This work is licensed under a Creative Commons Attribution 4.0 International License.

Related