Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Reproducible research with R
Bjarki Þór Elvarsson and Einar Hjörleifsson
Marine Research Institute
Bjarki&Einar (MRI) R-ICES 1 / 19
Reproducible research
Overview
1 Reproducible research
Bjarki&Einar (MRI) R-ICES 2 / 19
Reproducible research
Reproducible research
How did you do that?
What data did you use?
What scripts were used to do what?
Bjarki&Einar (MRI) R-ICES 3 / 19
Reproducible research
Typical workflow
Prepare data
Perform analysis
Generate report
Realise that something waswrong in the data or analysis
Do it all over again
Bjarki&Einar (MRI) R-ICES 4 / 19
Reproducible research
What is reproducible research?
"The final product of research is not only the paper itself, but also the fullcomputation environment used to produce the results in the paper such asthe code and data necessary for reproduction of the results and buildingupon the research." (Xie, 2014).
Bjarki&Einar (MRI) R-ICES 5 / 19
Reproducible research
Rmarkdown
A convenient tool to generate reproducible document.
MarkdownRemove HTML tag for higher readibility.Inline HTML is avaliable.
R markdownMarkdown + embedded R code chunksRmd -> md -> docx, pdf,html
Why R MarkdownConsolidate your code and document into single file.Easy for version control.
Bjarki&Einar (MRI) R-ICES 6 / 19
Reproducible research
Markdown
Bjarki&Einar (MRI) R-ICES 7 / 19
Reproducible research
Markdown quick refefence
Bjarki&Einar (MRI) R-ICES 8 / 19
Reproducible research
Markdown commands
Text formatting
# 1st level header *a* italics
## 2nd level header **b** bold
### 3rd level header
List: Hyperlinks
* item 1 [Hafro](www.hafro.is)
* item 2
+ subitem
Bjarki&Einar (MRI) R-ICES 9 / 19
Reproducible research
Markdown equations
Markdown also supports LaTeX equation so
$x^2 + \sum_i a_i y_i^n$
becomesx2 +
∑i
aiyni
Further reading: https://en.wikibooks.org/wiki/LaTeX/Mathematics
Bjarki&Einar (MRI) R-ICES 10 / 19
Reproducible research
Embedding R
Bjarki&Einar (MRI) R-ICES 11 / 19
Reproducible research
Chunks
Input from R in Markdown is evaluated in chunks:```{r}
<insert R code for Markdown>
```
Chunks have a plethora of options available by default:Allows you to display or hide codeSimilarly display or hide its outputFigure dimensions can be set (if created by in the chunk)
Bjarki&Einar (MRI) R-ICES 12 / 19
Reproducible research
R code chunks
Bjarki&Einar (MRI) R-ICES 13 / 19
Reproducible research
Helpful chunk output options
eval = TRUE : Evaluate all or part of the current chunkecho = TRUE : Show all or part of the source coderesults = ’asis’ : Writes raw output from R to the output documentwithout markup. Helpful for creating tables with xtable. markup is thedefault.include = TRUE : Code chunk will be included in output. If you don’twant a chunk in the output but still evaluated set this to FALSE
Bjarki&Einar (MRI) R-ICES 14 / 19
Reproducible research
Figure settings
fig.width controls the figure width (in inches)fig.height controls the figure height (in inches)fig.cap is the figure captionfig.align sets the alignmentdev allows the user to specify the file type of the plot (png, pdf, bmp,etc..)
Bjarki&Einar (MRI) R-ICES 15 / 19
Reproducible research
Creating tables in RMarkdown
Tables in Markdown are fairly easy:
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Ce
But one can also use R command to create tables. Note: Set results=’asis’to write raw results from R into the output document
library(knitr)
kable(tab,caption = 'This is a table')
Bjarki&Einar (MRI) R-ICES 16 / 19
Reproducible research
Output options
Html, both slides and normal webpages
Word, requires either Word or Libreoffice to display
Pdf, requires LaTeX:Linux do ’yum install texlive’Windows: install miktex from miktex.orgMacOsX: install MacTeX from tug.org/mactex
Custom output
Bjarki&Einar (MRI) R-ICES 17 / 19
Reproducible research
Other notable features
Bibtex style citations
Theming available based on reference word documents, css files andlatex templates
Allows the creation of interactive webpages using shiny
Support for presentations (beamer, ioslides, slidy)
Bjarki&Einar (MRI) R-ICES 18 / 19
Reproducible research
Class exercise
Play around with Einar’s ggplot2 document
Bjarki&Einar (MRI) R-ICES 19 / 19