SLIDE 1 See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/313221458
All in one – Reporting with RStudio (slides)
Presentation · December 2016
CITATIONS READS
209
1 author: Some of the authors of this publication are also working on these related projects: Multigenerational Demography View project Hidden Markov modelling for complex social sequence data View project Satu Helske University of Turku
16 PUBLICATIONS 97 CITATIONS
SEE PROFILE
All content following this page was uploaded by Satu Helske on 02 February 2017.
The user has requested enhancement of the downloaded file.
SLIDE 2
All in one
Reporting with RStudio Satu Helske 8 December 2016
SLIDE 3 Learning R
◮ Online courses
◮ Lynda ◮ DataCamp ◮ Code School
◮ Books
◮ Getting used to R, RStudio, and R Markdown by Chester Ismay ◮ R for Data Science by Garrett Grolemund and Hadley Wickham ◮ R for Stata Users by Robert A. Muenchen and Joseph M. Hilbe
SLIDE 4
Rstudio
◮ Open source graphical interface for R and related tools ◮ Code completion, syntax highlighting, code diagnostics ◮ Autosave, command history, plotting history, environment
browser, integrated searchable help
◮ R projects for easy return to and switching between jobs ◮ https://www.rstudio.com/
SLIDE 5
Reporting with knitr
SLIDE 6
What?
◮ Mix text, R code, R output, R graphics, mathematics ◮ Notes, reports, exercises, slides, research papers ◮ PDF, Word, html etc.
SLIDE 7 Why?
◮ Reproducible research
◮ Change in data → Change in report ◮ Sharing work with others
◮ Easier to adjust font sizes in figures ◮ No copy-paste errors
SLIDE 8 How?
◮ knitr with R Markdown
◮ Simple, fast ◮ Limited functionalities ◮ Rmd file
◮ knitr with LaTeX
◮ Precice control ◮ Requires LaTeX skills ◮ Rnw file
SLIDE 9
R Markdown
SLIDE 10
R Markdown
◮ Simple formatting syntax ◮ Fully reproducible documents ◮ Closely integrated to Rstudio ◮ Based on Markdown language, knitr, and pandoc ◮ http://rmarkdown.rstudio.com/
SLIDE 11
Workflow
SLIDE 12
Open R Markdown file
SLIDE 13
Write
SLIDE 14
Add R code
SLIDE 15
Render
SLIDE 16
Markdown syntax
SLIDE 17 YAML header
subtitle: RStudio for reporting author: Satu Helske date: 8 December 2016
- utput: ioslides_presentation
SLIDE 18
Headers
# Header 1 ## Header 2 ### Header 3
SLIDE 19
Typesetting examples
◮ _italics_ or *italics* produces italics ◮ __bold__ or **bold** produces bold ◮ superscriptˆ2ˆ produces superscript2 ◮ subscripts~2~ produces subscripts2
SLIDE 20 Lists
+ Item A * Item B
produces
◮ Item 1
◮ Item A ◮ Item B
◮ Item 2
(-, +, and * are equivalents)
SLIDE 21 Tables
Type | Freq | %
Apples | 7 | 44 Oranges | 9 | 56 produces Type Freq % Apples 7 44 Oranges 9 56
SLIDE 22
Mathematics
Mathematics like in LaTex: y_i = \beta_0 + \beta_1 x_i + \epsilon_i produces yi = β0 + β1xi + ǫi
SLIDE 23
Embedding R code
SLIDE 24 Code chunks
◮ Start with three backticks followed by {r}
◮ Optional own chunk name (unique, no spaces or periods) ◮ Optional options (R expressions)
◮ R code in between ◮ End with three backticks
SLIDE 25 Some options for code chunks
◮ eval: run R code (TRUE/FALSE) ◮ echo: show R code in output ◮ warning, error, message: show warnings/errors/messages in
◮ cache: save objects
◮ chunk only evaluated 1st time, later objects are loaded
◮ fig.width, fig.height, fig.asp: settings for figure size ◮ fig.cap: caption for figure
SLIDE 26 Dynamic tables
library(knitr) regression_table <- lm(dist ~ speed, data = cars) kable(summary(regression_table)$coefficients, digits = c(2, 2, 2, 3)) Estimate
t value Pr(>|t|) (Intercept)
6.76
0.012 speed 3.93 0.42 9.46 0.000
SLIDE 27 Figures
plot(cars)
5 10 15 20 25 20 40 60 80 100 120 speed dist
SLIDE 28
Dynamic code within text
The sample size was `r nrow(cars)`. The average distance that was travelled was `r mean(cars$dist)`. produces The sample size was 50. The average distance that was travelled was 42.98.
SLIDE 29
Writing papers
SLIDE 30 Bookdown
◮ R package built on top of R Markdown ◮ Book or “book” from several R Markdown documents ◮ Numbering, cross-referencing, appendices ◮ Output options (YAML)
◮ bookdown::html_document2 ◮ bookdown::word_document2 ◮ bookdown::pdf_book
◮ https://bookdown.org/yihui/bookdown/
SLIDE 31 Abstract
Option abstract in YAML
- title: All in one - RStudio for reporting
author: Satu Helske
abstract: "This is my abstract."
SLIDE 32 Numbers and cross-references
◮ Use bookdown outputs (e.g., bookdown::pdf_document2) ◮ Figures
◮ Name chunk (e.g., my-plot), fig.cap in R chunk options ◮ Cross-reference in text: \@ref(fig:my-plot)
◮ Tables
◮ Name chunk (e.g., my-table) ◮ kable function in knitr; add caption argument ◮ Cross-reference: \@ref(tab:my-table)
◮ Sections
◮ Add {#my-section} after section header ◮ Cross-reference: \@ref(my-section)
SLIDE 33 Bibliographies
◮ BibTeX bibliographies (.bib) recommended
@Book{Author2015, author = {Author, Alice and Writer, William}, title = {Book of something}, publisher = {Publisher P}, year = {2015}, address = {City, Country} }
◮ Add bibliography + options in YAML, e.g.
- bibliography: my_bibliography.bib
biblio-style: apalike link-citations: true
SLIDE 34
Citing
◮ Citing in text: @Author2015 ◮ Citing in brackets: [@Author2015; @Writer2016] ◮ References printed at the end of the document →
Last line is section header, e.g. # References
SLIDE 35
LaTeX
SLIDE 36
knitr with LaTeX
◮ Normal LaTeX with embedded R code ◮ Inline code: \Sexpr{} ◮ Code chunks:
<<my_cars_plot, fig.height=6, fig.width=8>>= plot(cars) @
SLIDE 37
Open Rnw document
SLIDE 38
Before rendering with knitr
◮ Change RStudio default from Sweave to knitr
SLIDE 39
Rendering
SLIDE 40 Tips
◮ When writing, use html output
◮ Fast to render ◮ No need to worry about pages, margins etc.
◮ Cache time-consuming code chunks with the cache option
◮ Note: cached chunks only change if you change the contents →
Subsequent chunks do not automatically change (unless cache cleared)
◮ For data cleaning, manipulation, and visualization, see
tidyverse
◮ Finding help
◮ Stack overflow ◮ Github project pages
SLIDE 41 Tips for collaboration
◮ Share the Rmd (R Markdown) file
◮ Use commenting (Crtl + Shift + C) to suggest changes or to
show where you have modified the file
◮ Share the Word file
◮ Use the commenting options in Word ◮ Copy and paste changes into the Rmd file
◮ Use version control such as Git
◮ Integrated in RStudio ◮ Automatically keeps track of all changes in all previous versions ◮ Might be a steep learning curve, but for small projects learning
the very basics is usually enough
◮ Learn (enough) Git in 15 minutes: https://try.github.io
SLIDE 42 Useful links
◮ R Markdown cheat sheet by RStudio ◮ knitr in a knutshell by Karl Broman ◮ knitr by Yihui Xie ◮ LaTeX by the LaTeX Project ◮ Citing
◮ BibTex by Martin J. Osborne ◮ Bibliographies and citations by Rstudio ◮ Citing with Mendeley by Rosanna van Hespen
◮ Tables
◮ Precise control: xtables by David B. Dahl, David Scott,
Jonathan Swinton and others
◮ Good-looking tables with minimal effort, e.g., summarizing
multiple models: stargazer by Marek Hlavac
View publication stats View publication stats