- Sorting out your RStudio working environment.
- Getting you ready to write reproducible reports in RMarkdown.
What is RMarkdown?
.pdf, .html, .docx, .docCode > Download ZIP > unzip directory on your machine.stats-iii.Rproj
xxx/slides.Rmd: slides in RMarkdown formatxxx/exercises: R scripts that we will work withdata/: my scripts will read in data from herermarkdowntinytex# Is tidyverse installed?
'tidyverse' %in% rownames(installed.packages())
# Should return TRUE but if not run
install.packages('tidyverse')
install.packages('tinytex')
tinytex::install_tinytex()
# then restart RStudio
# Then, try tinytex:::is_tinytex() # should return TRUE
rmarkdown will render pdf documents:writeLines('Hello $x^2$', 'test.Rmd')
rmarkdown::render('test.Rmd', output_format = 'pdf_document')
writeLines creates an .Rmd file named test.Rmd.rmarkdown::render renders .Rmd as pdf named test.pdf.rmarkdown/examples/example.RmdKnit > Knit to PDF to compile .Rmd to .pdf. Wow!!!File > New File > R Markdownoutput: pdf_document: use pdfs for writing manuscriptsfa-linear-regression.Rmd.Rproj file.CTRL+ALT+I or CMD+ALT+I (i.e. the letter “i”)packages and load libraries needed:library(tidyverse)
loaddata and load Blomkvist et al. (2017) data:blomkvist <- read_csv("data/blomkvist.csv") %>%
select(id, age, smoker, sex, rt = rt_hand_d) %>%
drop_na()
{r setup, echo=FALSE}setup is a label of this chunk (optional; useful for cross-referencing of figures and tables).echo = FALSE: don’t display chunk in output; echo = TRUE: display chunk.knitr::opts_chunk$set(message = FALSE, # don't return messages
warning = FALSE, # don't return warnings
comment = NA, # don't comment output
echo = TRUE, # display chunk (is default)
eval = TRUE, # evaluate chunk (is default)
out.width = '45%', # figure width
fig.align='center') # figure alignment
# This is a section header ## This is a subsection header # This is another section header ## This is another subsection header ### This is a subsubsection header
myscatterplotecho = F cause we only need the figure.fig.cap = "A scatterplot." in the chunk configurations.library(psyntur) scatterplot(x = age, y = rt, data = blomkvist)
ggplot2ggplot(blomkvist, aes(x = age, y = rt)) +
geom_point() +
theme_classic()
out.width = 50%.\ref{fig:myscatterplot} in the text."A scatterplot of age and reaction time can be found in Figure \ref{fig:myscatterplot}."
header-includes:
- \usepackage{booktabs}
booktabs to improve type setting.library(psyntur) (smoker_age <- describe(data = blomkvist, by = smoker, mean = mean(age), sd = sd(age)))
# A tibble: 3 × 3 smoker mean sd <chr> <dbl> <dbl> 1 former 65.2 16.9 2 no 53.0 21.3 3 yes 50.6 17.5
library(kableExtra)
smoker_age %>%
kable(format = 'latex',
booktabs = TRUE,
digits = 2,
align = 'c', # centre value in each column
caption = 'Descriptives of age by smoker.') %>%
kable_styling(position = 'center') # centre position of table
smoker” and cross-reference the table in the text using Table \ref{tab:smoker}.bibliography: refs.bib biblio-style: apalike
refs.bib (save in same working directory as your .Rmd file).bib entry for Blomkvist et al. (2017) from Google Scholar and paste it into refs.bib:
cite and BibTeX.bib entry into refs.bibblomkvist2017reference@blomkvist2017reference or [@blomkvist2017reference].# References”# Fit the model and get the summary model <- lm(rt ~ sex, data = blomkvist) model_summary <- summary(model)
# Extract R^2 r2 <- model_summary$r.sq
The $R^2$ for this model is `r round(r2, 2)`.
Renders “The \(R^2\) for this model is 0.03.”
# Extract F statistic f_stat <- model_summary$fstatistic p_value <- pf(f_stat[1], f_stat[2], f_stat[3], lower.tail = FALSE)
The model summary can be summarised like so: $F(`r round(f_stat[2])`, `r round(f_stat[3])`) = `r round(f_stat[1],2)`$, $p `r format.pval(p_value, eps = 0.01)`$.
Renders “The model summary can be summarised like so: \(F(1, 263) = 7.48\), \(p <0.01\).”
p <- c(0.05, 0.02, 0.011, 0.005, 0.001) format.pval(p, eps = 0.01)
[1] "0.05" "0.02" "0.01" "<0.01" "<0.01"
'$' symbols for inline mode.$\beta$ renders \(\beta\).$\beta_0$ is \(\beta_0\) and using '{}' for more than one symbol as in $\beta_{01}$ which is \(\beta_{01}\)'^' as in $\sigma^2$ which is \(\sigma^2\).$x + y$, $x - y$$\cdot$ or $\times$ to get \(\cdot\) or \(\times\), respectively, as in \(3 \cdot 2\)$/$ or $\div$ to get \(/\) or \(\div\), respectively, or $\frac{1}{2}$ for \(\frac{1}{2}\)$\pm$ renders to \(\pm\)install.packages("rmdformats")
or
install.packages("remotes")
remotes::install_github("juba/rmdformats")
File > New File > R Markdown (e.g. readthedown or robobook for documents)rmdshower::shower_presentations and ioslides_presentation for slidespapajakableExtra check herermarkdown website.Blomkvist, Andreas W., Fredrik Eika, Martin T. Rahbek, Karin D. Eikhof, Mette D. Hansen, Malene Søndergaard, Jesper Ryg, Stig Andersen, and Martin G. Jørgensen. 2017. “Reference Data on Reaction Time and Aging Using the Nintendo Wii Balance Board: A Cross-Sectional Study of 354 Subjects from 20 to 99 Years of Age.” PLoS One 12 (12): e0189598.
Knuth, Donald Ervin. 1984. “Literate Programming.” The Computer Journal 27 (2): 97–111.
Open Science Collaboration. 2015. “Estimating the Reproducibility of Psychological Science.” Science 349 (6251): aac4716.
Roeser, Jens, Sven De Maeyer, Mariëlle Leijten, and Luuk Van Waes. 2021. “Modelling Typing Disfluencies as Finite Mixture Process.” Reading and Writing, 1–26.
Wickham, Hadley, and Garrett Grolemund. 2016. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media, Inc.
Xie, Yihui. 2017. Dynamic Documents with R and Knitr. Chapman; Hall/CRC.