library(tidyverse)
model_01 <- lm(Petal.Length ~ Petal.Width, data = iris)Creating Reproducible APA-Style Manuscripts
Introduction
You already know Quarto, so you’re familiar with writing documents that combine text and code… Today we’re introducing RMarkdown and papaja, which work in a very similar way and are designed to create fully reproducible APA-style manuscripts directly from R.
This worksheet contains all the tasks and hints you’ll need to explore these tools and start turning your own work into polished, reproducible, goRgeous papers 😍
RMarkdown Basics
Open the ‘worksheet’ RMarkdown document in the papaja workshop Posit project and ‘knit’ it.
RMarkdown files have the file extension .Rmd!
Change the YAML of your RMarkdown document. Add:
a different title
the author below the title (can be your own name or an alter ego 👀)
the current date
an abstract paragraph to the YAML (you can write gibberish or copy and paste from your favourite or most recently read paper)
a table of contents (toc)
& add a theme
You’ll need to tweak a few things in your YAML header here:
If you wanted, you could dynamically insert today’s date using inline code (more on this later!), this guide might help in the meantime
For adding a theme (and styling your document), have a look here
You can browse available themes here, though not all of them shown are available
Be extra careful with the indentation in the YAML — spacing really matters, and even a small misalignment can break the whole document!
Now let’s add some formatted text to the body of the document! You can write gibberish, copy text from a paper online, or from your own work. In the Source mode, try to:
Create some headings and subheadings of different levels
Add a paragraph and make some words bold or italic
Add a footnote
Knit your document!
Have a look at the RMarkdown Cheat Sheet if you get stuck!
As an additional top tip, you should knit your document regularly to help troubleshoot code errors and to also keep a copy of your work.
Onto the juicy part, let’s add some code and analyses to the document!
Add two code chunks that runs the below code
Give each chunk a descriptive name
Set the code chunk options for the plot to be hidden so that we can see the output, but not the code
You can use as many code chunks as your heart desires but I find it most easy to work with when I split them into key sections and steps.
When labeling code chunks, avoid spaces, underscores and duplicate names as these can cause errors.
For the full list of code chunk options, see the cheatsheet.
Papaja Principles
Create a new
papajadocument in the same Posit project and fill in the YAML with made up information, details from your favourite paper, or something you’ve worked on (or a mix of all three!).Knit the file and inspect the beautiful output. Notice the additional files that have now appeared in the files pane.
If ever you’re not quite sure what an option does, change it slightly, knit the file, and see what changes in the output - just don’t change too much all at once!
In the new
papajadocument you just created, copy the linear model code from earlier (repeated below) into a new code chunk in the Method subsection.Using basic inline code, report the sample size using
nrow()on theirisdataset replacing the XXs in the example below as a guide of what to write.Using the rest of the code below, report the full linear model result for the
Petal.Widthpredictor.
‘The sample consisted of XX flowers.’
‘Petal width was a significant predictor of petal length (XX).’
There’s something sneaky going on here… in apa_print(), Petal.Width has been quietly changed to Petal_Width. Always check the output to make sure you’re selecting the correct thing!
Not applicable here, but not all analyses work with apa_print(), if you want to check if yours will work, use methods(apa_print).
If the analysis isn’t supported by apa_print(), you can extract numbers manually from the object containing your results. str() is very useful for finding which numbers you’re looking for.
Add a code chunk, give it a descriptive label, and insert an APA style table of the linear model results.
Add another code chunk, with a different label, and add an APA theme to the below plot. Make sure to add a figure caption in the code chunk options too:
fig.cap="Scatterplot of petal length and petal width."Have a go at cross-referencing them in text.
I strongly recommend knitting your file every time you add a table or a figure to help spot errors before they become even more difficult to pinpoint.
If there’s errors with cross-referencing, you’ll see ‘??’ in place of where the number should be in the knitted HTML file.
Move all
library()commands to the r setup chunk.Reference any of the papers you’ve used today, and a few others you’ve read recently or can find online. Try a variety of methods to add them, and insert them into a sentence like you would in a normal paper.
Knit the document and have a look at all the nicely formatted references!
By moving all the library() commands to the r setup chunk, papaja will automatically cite all the R packages we’ve used too.
- Change the output type to a Word doc and knit the file.
We’ll need to go back to the YAML to change this output to docx
For spelling and grammar checks I find it very useful knitting to Word but make sure to only make further changes to the .Rmd file and not the Word doc because R will overwrite any manual changes you’ve made every time you knit!
That’s all for today! As you keep going, try to experiment, explore, and adapt these tools to fit your own work. There’s a huge range of resources and tools online, so make the most of them as you learn. The aim isn’t perfection - it’s building a workflow that’s clear, efficient, reproducible and works well for you.
Good luck and happy coding! ✨