# Open a file connection
output_file <- file("output.txt", open = "wt")
# Redirect both standard output and error messages to the same file
sink(output_file, split = TRUE) # Capture normal output
sink(output_file, type = "message") # Capture errors and warningsAccessibility Commands Cheat Sheet
An NCRM Case Study in Pedagogy
R Studio Accessible Keyboard Shortcuts
Below are commands for both Mac and Windows that allow you to navigate between the different windows that exist within the R Studio (posit) environment.
Windows/Linux Shortcuts
Quarto-Specific Shortcuts
- Toggle Between Source & Visual Mode:
Control + Shift + F4
- Render Quarto Document:
Control + Shift + K
- Preview Quarto Document:
Control + Shift + P
Terminal Shortcuts
- Open Terminal Pane:
Shift + Alt + T
- Switch to Terminal Pane:
Control + 7
- New Terminal Tab:
Alt + Shift + R
- Close Terminal Tab:
Ctrl + Shift + W
- Cycle Between Terminal Tabs:
Ctrl + Tab
Background Jobs
- Switch to Jobs Pane:
Control + 8
- Cancel Running Job: Once in the Jobs pane, the user can try pressing Tab to navigate through elements and reach the Stop button, then press Enter to activate it. However, this may not always be accessible, depending on the screen reader.
Mac Shortcuts
Quarto-Specific Shortcuts
- Toggle Between Source & Visual Mode:
Command + Shift + F4
- Render Quarto Document:
Command + Shift + K
- Preview Quarto Document:
Command + Shift + P
Terminal Shortcuts
- Open Terminal Pane:
Shift + Option + T
- Switch to Terminal Pane:
Command + 7
- New Terminal Tab:
Option + Shift + R
- Close Terminal Tab:
Command + Shift + W
- Cycle Between Terminal Tabs:
Command + Tab
Background Jobs
- Switch to Jobs Pane:
Command + 8
- Cancel Running Job: Once in the Jobs pane, the user can try pressing Tab to navigate through elements and reach the Stop button, then press Enter to activate it. However, this may not always be accessible, depending on the screen reader.
Opening a New Quarto Document
A very accessible way of working within R is through a quarto document. Quarto documents allow us to have a single document within which we can write our text and also write and execute code. A quarto document can be published as a word document, html file (most accessiblely friendly) or a PDF. There are also other options related to presentations that can be used.
Mac
Open File Menu: Control + Option + F
Navigate to New File: Down Arrow
Select New Quarto Document: Right Arrow, Down Arrow, Enter
There are three options for type of Quarto file. We want to select “Document”. There are dialogue windows to enter your name and also provide a title of the document. You can select the document to write as either HTML, PDF or Word. It is suggested that HTML or Word are best for screen readers.
Quarto documents have two windows. One called Visual which presents the document much like a word document. with the ability to edit the document using a series of dropdown menus. The other window is the Source window which allows you to do all of this but in a way that is friendlier to a screen reader.
Switch to Source Mode: Control + Option + 1
Switch to Visual Mode: Control + Option + 2
Windows
Open File Menu: Ctrl + Alt + F
Navigate to New File: Down Arrow
Select New Quarto Document: Right Arrow, Down Arrow, Enter
There are three options for type of Quarto file. We want to select “Document”. There are dialogue windows to enter your name and also provide a title of the document. You can select the document to write as either HTML, PDF or Word. It is suggested that HTML or Word are best for screen readers.
Quarto documents have two windows. One called Visual which presents the document much like a word document. with the ability to edit the document using a series of dropdown menus. The other window is the Source window which allows you to do all of this but in a way that is friendlier to a screen reader.
Switch to Source Mode: Ctrl + Alt + 1
Switch to Visual Mode: Ctrl + Alt + 2
Opening and Running Code Chunks in a Quarto Document
It is important to run each code chunk as you write them. This will automatically open an output window within the document that works with a screen reader. This will also highlight any error messages allowing you to effectively isolate problems. Waiting until a whole document is written and then rendering can make finding errors significantly more challenging.
Opening a New Code Chunk
To insert a new code chunk in a Quarto document within RStudio, follow these steps:
Navigate to the Quarto Document Editor
- If you are not already focused on the editor, press Ctrl + 1 (Windows/Linux) or Cmd + 1 (Mac) to move to the Source Editor.
Insert a New R Code Chunk Using the Keyboard
Press Ctrl + Alt + I (Windows/Linux) or Cmd + Option + I (Mac).
This will insert a new code chunk
If you are using a screen reader, it may read out “Backtick backtick backtick brace r brace” when the chunk is inserted.
Manually Typing a Code Chunk
- You can also type the chunk manually by entering three backticks (
```), followed by{r}, and then pressing Enter to create the space for your code.
- You can also type the chunk manually by entering three backticks (
Writing code in the Code Chunk
- Once you have opened a code chunk, use the arrow keys to navigate within the chunk and it is here that you can then type in your code.
Running a Code Chunk
After typing your code inside a chunk, you can execute it in one of the following ways:
Run the Current Code Chunk
navigate the cursor inside the chunk and press Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac).
The output will appear below the chunk.
A screen reader will announce the execution, often by reading the output if it’s text-based.
Run the Entire Chunk at Once
Press Ctrl + Shift + Enter (Windows/Linux) or Cmd + Shift + Enter (Mac) to execute the full chunk.
This ensures all lines inside the chunk run together.
Run All Chunks in the Document
- To execute all code chunks in the entire document at once, press Ctrl + Alt + R (Windows/Linux) or Cmd + Option + R (Mac).
Using Sink to Output Code Chunks
So far, we have used commands to execute code chunks. This is great as it allows us to run through elements of code as we go. However, at present, screen reader support is not easy to use within the output of code chunks. As we see later, typically the easiest way to work with R is by rendering it into a whole document. However, waiting until the end of the document to check whether our code works leads to challenges, particularly if there are errors in our code. An easy way to resolve this is to create a new code chunk that captures both standard output and error messages into a text file.
Setting Up Sink for Output Logging
Create a new code chunk
Use Option+Command+I (Mac) or Ctrl+Alt+I (Windows/Linux) to insert a new chunk.
Navigate into the code chunk. The top row of the chunk will have the text
{r}.
Modify the chunk options
To the right-hand side of
r, place a comma and writeinclude = FALSE.This should look like
{r setup, include = FALSE, eval = FALSE}.
Set up the sink function
- Move to a fresh line inside the chunk and enter the following code:
Run each line of code in turn
- Ensure your cursor is on each line and press Ctrl+Enter (Windows/Linux) or Cmd+Enter (Mac) to execute it.
How This Works
sink(output_file, split = TRUE)redirects normal output tooutput.txtwhile still displaying it in the console.sink(output_file, type = "message")ensures that error messages and warnings are also captured.The text file does not automatically open, but you can find it in your working directory.
Viewing the Output
After running a code chunk, reopen
output.txtto see the updated output.This allows you to track results and identify errors without waiting until the document is fully rendered.
Working Directories
In order to get R Studio to understand where files are located and where it should save new objects you create we need to set a working directory.
This section starts by talking you through how to navigate file structures in both Mac and Windows. A first step in setting a working directory is understanding where in your computer you want it to be.
How to Find and Access the Working Directory on a Mac
When using Finder on a Mac, you can locate files and folders within your working directory and retrieve their full filepaths. Here’s how to do it step by step:
Step 1: Access the Sidebar
1. Open Finder.
2. Locate the Sidebar on the left-hand side of the Finder window.
- The Sidebar displays a list of parent directories, such as OneDrive, iCloud, Dropbox, Applications, and other commonly used locations.
- If you are using a screen reader like VoiceOver, interact with the Sidebar:
Press VO+Shift+Down Arrow (VoiceOver command) to interact.
Use the Up Arrow or Down Arrow keys to navigate the list.
Step 2: Select the Parent Directory
1. Navigate through the Sidebar until you find the directory where your file is located (e.g., OneDrive).
2. Press Enter or Return to open the directory.
Step 3: Navigate to Files and Folders
1. Once the parent directory is selected, navigate to the **File and Folder List** on the right side of the Finder window.
With VoiceOver, press VO+Right Arrow to move focus to the next area (the file list).
Interact with the file list using VO+Shift+Down Arrow and navigate using the Arrow Keys to locate the file or folder you need.
Step 4: Get the Full Filepath
To retrieve the full filepath of your working directory:
1. Select the file or folder you want in Finder.
2. Press Command+Option+C to copy the full filepath to your clipboard.
- This shortcut copies the absolute filepath of the selected item.
3. Paste the filepath into any text editor or document using Command+V to view it. We will do this into a Quarto code chunk below.
Notes for VoiceOver Users
- When interacting with Finder, ensure you’re aware of the distinction between areas (Sidebar, File List, Toolbar, etc.) and navigate accordingly using VO+Arrow Keys.
- The Sidebar is your guide to top-level directories, while the File List area lets you drill down into specific files and folders.
How to Find and Access the Working Directory on Windows
In order to get R Studio to understand where your files are located and where it should save files, you need to find your working directory. Here’s how to do it step by step on a Windows machine:
Step 1: Open File Explorer
- Press the Windows key + E to open File Explorer.
- This will bring up a window showing your directories and files.
Step 2: Access the Sidebar
- In File Explorer, the Sidebar is on the left-hand side of the window. It displays parent directories like Desktop, Documents, OneDrive, and This PC, which contains other drives and folders.
- If you are using a screen reader like Narrator or another assistive technology, interact with the Sidebar:
- Press Narrator + F6 to navigate to the Sidebar.
- Use the Up Arrow or Down Arrow keys to move through the list of drives and directories.
Step 3: Navigate to the Desired Directory
- Use the arrow keys to navigate through the Sidebar and locate the directory where your file is stored (e.g., Documents or OneDrive).
- Once you’ve found the directory, press Enter to open it.
Step 4: Get the Full Filepath
To retrieve the full filepath of your working directory:
- Once inside the folder where your file is located, press Alt + D to highlight the address bar at the top of the File Explorer window.
- Copy the full path of the directory by pressing Ctrl + C.
- This will copy the absolute filepath of the current directory to your clipboard.
- Paste the filepath into any text editor or document using Ctrl + V to view it. We will do this into a Quarto code chunk below.
Notes for Narrator Users
- When interacting with File Explorer, make sure you’re aware of the distinction between areas (Sidebar, Address Bar, File List, etc.). Use Tab and Shift + Tab to move through the sections.
- The Sidebar provides access to top-level directories, while the Address Bar and File List let you navigate specific files and folders.
Setting Working Directory
Now that we have learnt how to find the location that we wish to save our files in, we need to tell R where this is located. In R, the working directory is the folder on your computer where R reads and saves files by default. Think of it as R’s “home base” for accessing and storing data files, scripts, and other resources. You can set or check your working directory using commands like setwd() or getwd().
We need to save our data files to the working directory we wish to use. I recommend that you create a folder to save all of your data files and output. For me, this produces the following location. Simply change the code here to represent where your folder is located.
To set our working directory, we need to use the command “setwd” and open brackets and quotations and paste our full filepath we have from copied from above within this. It should look something like this.
We again need to do this within a code chunk so click Option+Command+I to open one and then write your code within it.
# Replace "path/to/your/directory" with the full path to your working folder
setwd("path/to/your/directory")Run the code chunk using shift+control+enter
Rendering a Quarto Document
Rendering a Quarto document processes the .qmd file and converts it into the specified output format, such as HTML, PDF, or Word. During rendering, code chunks are executed, and their results (e.g., text, tables, or plots) are embedded into the final document. The process also applies formatting, including headings, lists, and other markdown elements, to create a polished and structured output.
So far, we have focused on running individual chunks. This is good practice as you write the document because it makes spotting and resolving errors easier, as you’re taken directly to the relevant code chunk. However, we may also want to render the entire document to generate a final version.
For accessibility, HTML is the best option because it works well with screen readers, allows for flexible text resizing, and supports interactive elements. Unlike PDFs, which can have readability issues, or Word documents, which may require additional formatting adjustments, HTML ensures that content remains accessible and adaptable for all users.
Mac
- Render Document: Shift + Command + K
Windows
- Render Document: Shift + Ctrl + K
How to Install a Package in a Quarto Code Chunk
Step 1: Open a New Code Chunk
Press Ctrl + Alt + I (Windows/Linux) or Cmd + Option + I (Mac) to insert a new R code chunk.
- If using a screen reader, it may announce “Backtick backtick backtick brace r brace.”
Step 2: Navigate Inside the Chunk
- Use the Down Arrow key to move into the empty space inside the chunk.
Step 3: Type the Installation Command
Type the following command exactly as shown:
install.packages("tidyverse")
Step 4: Run the Code Chunk
Press Ctrl + Shift + Enter (Windows/Linux) or Cmd + Shift + Enter (Mac) to execute the entire chunk.
This will begin the installation of the tidyverse package.
If using a screen reader, listen for installation progress messages in the Console Window.
Alternative: Run the Command Line by Line
Place your cursor anywhere on the install.packages(“tidyverse”) line.
Press Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac) to run just that line.
- This is useful if you are troubleshooting or testing individual commands.
By following these steps, you can successfully install an R package using a Quarto code chunk without using a mouse.
Running Packages
Before we conduct any analysis, we need to run our pacakges. While we have installed them, we now need to tell the software that we want to load them. We do this with the “library” command. To execute this code within a quarto document we need to open a code box.
We do this by Option+Command+I
Write our commands within the code chunk
Run the code chunk using shift+control+enter
library(tidyverse)
library(BrailleR)
library(DT)
library(haven)
library(jmv)Now we have got our packages loaded, we have one more step to complete before we can get started with loading data and running analyses. We need to set our working directory.
Remember, every time we want to write and run code we need to include it wihin a code chunk.
Appendix Common Keyboard Shortcuts in RStudio
General
- Open File: Ctrl + O (Windows) / Command + O (Mac)
- Save File: Ctrl + S (Windows) / Command + S (Mac)
- Close File: Ctrl + W (Windows) / Command + W (Mac)
Editing
- Undo: Ctrl + Z (Windows) / Command + Z (Mac)
- Redo: Ctrl + Y (Windows) / Command + Shift + Z (Mac)
- Find: Ctrl + F (Windows) / Command + F (Mac)
- Replace: Ctrl + H (Windows) / Command + Option + F (Mac)
Code Execution
- Run Current Line/Selection: Ctrl + Enter (Windows) / Command + Enter (Mac)
- Run All Code: Ctrl + Shift + Enter (Windows) / Command + Shift + Enter (Mac)
Console
- Clear Console: Ctrl + L (Windows) / Command + L (Mac)
- Interrupt R: Esc (Windows/Mac)
Help
- Show Help: F1 (Windows/Mac)
- Search Help: Ctrl + Shift + F1 (Windows) / Command + Shift + F1 (Mac)
For the complete list of keyboard shortcuts, you can visit the Posit R Studio Keyboard Shortcuts page.
Appendix: Alternative Quarto Document formats
Word Document Output
---
title: "My Document"
format: docx
---PDF Document Output
---
title: "My Document"
format: pdf
---HTML Document Output
---
title: "My Document"
format: html
---Combined Document Output
---
title: "My Document"
format:
html: default
pdf: default
docx: default
---Appendix: Overview of Chunk Options in Quarto
In Quarto, code chunk options are specified within curly braces {} immediately following the chunk indicator ({r}). The options are provided as a comma-separated list inside the curly braces.
For instance, if you wanted to set the option eval (this determines whether the code chunk should be evaluated and executed or whether it should just be displayed for use in learning exercises), you would open a code chunk, navigate within where r is and change it to {r, eval=FALSE}.
Code chunk options within a quarto document.
There are a number of different options that allow you to change what happens to code chunks within a quarto document.
The options control various aspects of the chunk’s execution, output, and display. Below is a list of some commonly used options. All of these work on the basis of a TRUE or FALSE logic.
Basic Options:
eval: Determines whether the code chunk should be evaluated.eval=TRUE(default): Evaluates the code.eval=FALSE: Skips the execution of the code (useful for showing code without running it).
echo: Controls whether the code should be displayed in the output.echo=TRUE(default): Displays the code in the output.echo=FALSE: Hides the code in the output (useful when only showing results).
include: Controls whether both the code and the output should be included.include=TRUE(default): Includes both code and output in the document.include=FALSE: Excludes both code and output from the document (useful for hidden code).
results: Controls the display of results.results='markup'(default): Shows results in a readable format (e.g., text, tables).results='hide': Hides the results.results='asis': Displays results as-is (e.g., for LaTeX or Markdown).
Formatting and Output Options:
message: Controls whether messages generated by the code chunk (e.g., warnings or information) are displayed.message=TRUE(default): Shows messages.message=FALSE: Hides messages.
warning: Controls whether warnings generated by the code chunk are displayed.warning=TRUE(default): Displays warnings.warning=FALSE: Hides warnings.
error: Determines whether errors in the code should be shown.error=TRUE(default): Shows errors.error=FALSE: Suppresses error messages.
cache: Caches the output of the code chunk for future runs.cache=TRUE: Caches the results for later use, speeding up future document rendering.cache=FALSE(default): Does not cache results.
Output Formatting and Plot Options:
fig.widthandfig.height: Controls the dimensions of figures (in inches).- Example:
fig.width=5, fig.height=4
- Example:
fig.align: Specifies the alignment of figures in the output.fig.align='left'(default): Aligns figures to the left.fig.align='center': Centers the figure.fig.align='right': Aligns the figure to the right.
dpi: Controls the resolution of plots.- Example:
dpi=300for high-quality images.
- Example:
File Output and Document Generation:
out.widthandout.height: Control the size of the output (useful for images and plots).- Example:
out.width="50%"to display images at 50% width.
- Example:
fig.cap: Adds a caption to the figure.- Example:
fig.cap="Figure 1: My plot"
- Example:
fig.path: Controls the directory where figures are saved.- Example:
fig.path="images/plots_"(saves plots in a specified directory).
- Example:
Code Evaluation and Debugging:
debug: Helps in debugging by printing intermediate outputs.debug=TRUE: Prints intermediate results in the output.debug=FALSE: Default setting, doesn’t print intermediate results.
stop_on_error: Determines whether the rendering should stop when an error occurs.stop_on_error=TRUE(default): Stops rendering if an error occurs.stop_on_error=FALSE: Continues rendering even if an error occurs.
Summary of Key Options:
- Control execution:
eval,include,cache,error - Control output visibility:
echo,message,warning,results - Plot options:
fig.width,fig.height,fig.align,dpi,fig.cap - File and path options:
fig.path,out.width,out.height
These options allow you to fine-tune the appearance and behavior of your Quarto document to suit your needs, whether for presentation, debugging, or publication.
Additional Keyboard Shortcuts for Working with Code Chunks
Navigate Between Code Chunks
Move to the previous chunk: Ctrl + Alt + P (Windows/Linux) or Cmd + Option + P (Mac).
Move to the next chunk: Ctrl + Alt + N (Windows/Linux) or Cmd + Option + N (Mac).
Delete a Code Chunk
- Move to the chunk and manually delete its content, or use the Chunk Menu (
Alt + Shift + K, then arrow down to “Delete”).
- Move to the chunk and manually delete its content, or use the Chunk Menu (
Run Selected Code (Instead of Full Chunk)
- Highlight the specific lines of code you want to run and press Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac).