What is R and RStudio?
R is a free, open-source programming language purpose-built for statistical analysis. RStudio is an Integrated Development Environment (IDE) for R — essentially a wrapper that gives you a more user-friendly interface for coding. You should download both.
Downloading R and RStudio
Visit this page for instructions on downloading both R and RStudio.
What is a knitted document, and why make one?
When you write an R Markdown file (.Rmd), you’re combining two things in one document: your write-up (in plain text/Markdown) and your R code (in “chunks”). Knitting is the process of running that file through R — it executes all your code chunks, captures the output (tables, plots, statistics), and weaves it together with your text into a single, polished output file (HTML, PDF, or Word).
This is especially useful for:
- Reproducibility — your analysis and your results write-up live in the same file, so anyone (including future-you) can see exactly which code produced which result. This is dependent on your annotating and explaining your code and method well, of course.
- Clean presentation — you can build a clean results summary in this document because you can choose to hide code that’s not necessary for interpretation (e.g., code loading in libraries/ data cleaning code), and keep code that is necessary for interpretation (e.g., model outputs, model specifications). This allows you to create a tidy, readable report with a table of contents, numbered sections, and formatted output.
- Inline reporting — you can embed R code directly into your sentences (e.g., “the mean age was
`r mean(age)`”), so your reported statistics are always pulled live from your actual data rather than typed in manually and prone to error.
Setting up a working directory
A working directory is the folder you set as the default location for any files you read into R or save out of R. Setting one up properly helps keep your data, scripts, and plots organised in one place. Follow the steps below to get set up.
Step 1: Create a folder on your desktop
This folder will be your working directory. Give it an intuitive name (e.g., “Dissertation Analyses”) so you can find it easily later.
Step 2: Export your data from Qualtrics
- Open your survey on Qualtrics.
- In the top toolbar, click Data & Analysis.
- Above your responses, on the right, click Export & Import.
- Select Export Data… from the dropdown menu.
- Make sure the data table type is set to CSV at the top of the box.
- Click Export values to get your numeric dataset (i.e., the dataset where Likert variables appear as numbers).
- Save this file in the folder you created in Step 1 and name it
num_data. - Go back to Qualtrics and select Export labels to get your text dataset (i.e., the dataset where variables appear as text).
- Save this file in the same folder and name it
text_data.
Step 3: Create an R Project
- Open RStudio.
- Click the second button in the top-left toolbar (a blue box logo with an “R” on it and a green plus sign in the corner).
- Select Existing Directory.
- Click Browse and select the folder you created in Step 1.
Step 4: Create your code script
- Click the first button in the top-left toolbar (a white page logo with a green plus sign in the corner).
- Select R Markdown… from the dropdown menu. Many people code in a basic R Script, but we prefer R Markdown, since it lets you “chunk” your code and produce a nicely knitted HTML file with your results explained and summarised alongside the code.
- Delete the default content on the page and edit the YAML header as needed (details below)
The YAML header sets the “settings” for your knitted document. See an example of how we’ve set things up in the past:
title: "My Dissertation"— sets the title that appears at the top of your knitted documentdate: "r Sys.Date()"— inserts today’s date automatically by running the R commandSys.Date()inline, so you never have to update it manuallyoutput: html_document:— tells R Markdown to knit your file into an HTML page (as opposed to a PDF or Word doc)toc: true— adds a table of contents to the top of your knitted document, generated automatically from your headers (#,##, etc.)toc_depth: 2— limits the table of contents to headers up to level 2 (i.e.,#and##, but not###and beyond), keeping it from getting clutterednumber_sections: true— automatically numbers your headers (1, 1.1, 1.2, 2, etc.), which is handy for referencing specific sections latertheme: readable— sets the visual styling of the knitted page; “readable” is one of several built-in Bootstrap themes (others includecerulean,journal,flatly, etc.) that control fonts, colours, and spacing
Step 5: Add a code chunk
Code chunks are where your actual R code lives. Anything inside a chunk gets run when you knit the document, and — depending on how you set the chunk’s options — its code and/or output can be shown or hidden in the final knitted file.
- Click into your
.Rmdfile below the YAML header, where you want your code to begin. - In the top-left panel’s toolbar, click the green Insert button (looks like a green square with a “C” and a plus sign) and select R from the dropdown.
-
An empty chunk will appear, bounded by three backticks at the top and bottom:
- Write your R code inside the chunk.
- Set the chunk’s options in the curly braces at the top (e.g.
{r, echo = FALSE}) depending on what you want visible in the knitted document:include = FALSE— hides both the code and its outputecho = FALSE— hides the code but shows the outputwarning = FALSE— hides any warnings the code produces, while still showing the output
For example, a chunk that runs your data-cleaning code silently (no code, no output, no warnings) would start with {r, include = FALSE, warning = FALSE}.