Suggestion Box
Spot an error or have suggestions for improvement on these notes? Let us know!
Week 11 · Importing Data from OSF
0 · Overview
In many psychology studies, data are stored on the Open Science Framework (OSF).
OSF is an online platform for managing, sharing, and version-controlling research materials.
Being able to import data directly from OSF into R ensures that your workflow is fully reproducible—you will always be working with the most current data, and others can easily reproduce your results from the same source.
You will not need to write this code yourself. The full OSF import script is provided for you to download and use in your project.
Download the OSF Import Script: w11_import_from_osf.R
This section explains what the script does conceptually, how to connect R to your OSF account using an API token, and how to identify the project node for your data.
1 · What the Script Does
The provided R script uses the OSF Application Programming Interface (API) to automatically download data files into your project folder.
At a high level, the script does the following:
- Connects to OSF using your personal access token (your authentication key).
- Finds the “Data” component of your OSF project using its node ID.
- Lists all available files in that component.
- Filters the list to find files that match a specific prefix (for example,
"lexical-decision"). - Checks when each file was last modified and keeps only recent ones.
- Downloads the matching files into your local
osf_downloads/folder. - Cleans up temporary objects so your workspace remains organized.
Once you’ve downloaded the data locally, the rest of your pipeline—scoring, filtering, and combining participants—works exactly the same as before.
2 · Why Import from OSF
There are several advantages to pulling data directly from OSF instead of manually downloading files:
- Reproducibility: Anyone rerunning your code can automatically fetch the same data from the shared repository.
- Version control: If you upload new data to OSF, rerunning the script automatically updates your local copies.
- Automation: You can process many participant files without doing any manual steps.
This approach makes your workflow transparent and ensures that your final results always correspond to the current version of the data on OSF.
3 · Setting Up Your OSF Access Token
To allow R to connect securely to your OSF account, you will need a personal access token.
This token works like a password for the OSF API, but can be limited to specific permissions.
Steps to create your token:
- Go to https://osf.io/ and sign in.
- Click your profile icon (top right) → Settings.
- In the left menu, choose Personal Access Tokens.
- Click Create Token.
- Give it a name (for example,
OSF_API) and check all permissions. - Copy the generated token (a long string of letters and numbers).
- Paste it into the script where it says:
TOKEN <- "paste_your_token_here"
Important: Treat your token like a password.
Do not share it publicly or post it in code you upload to GitHub.
4 · Finding Your OSF Node ID
Each OSF project and component has a unique node ID, which appears in the URL when you open it in a browser.
Example URL:
https://osf.io/avm5d/
Here, the node ID is avm5d.
You’ll need to copy that part of the URL and paste it into your script:
NODE <- "avm5d"
If your project has a “Data” component within the main project, make sure you use the node for the Data component, not the parent project node.
5 · Running the Import Script
Once you have entered your TOKEN and NODE values, you can run the full script as provided.
The script will:
- Connect to your OSF project.
- Search for files that match a specific prefix (for example,
"lexical-decision"). - Download those files into a new folder named
osf_downloads. - Report how many files were found and saved.
You’ll see messages in the R console showing progress, such as:
Found 40 matching files.
Saved: osf_downloads/lexical-decision-001.csv
Saved: osf_downloads/lexical-decision-002.csv
...
When it finishes, you’ll have all of your participant data downloaded and ready for analysis.
6 · Integrating with Your Workflow
After downloading, you can treat these files exactly the same as if you had placed them in your local data/raw/ folder.
For example, you could run:
file_list <- list.files("osf_downloads/", pattern = "*.csv", full.names = TRUE)
Then process them using your process_participant() function and combine them as usual.
7 · Troubleshooting Tips
-
If no files are found:
Check that yourPREFIXmatches the file names on OSF exactly (for example,"lexical-decision"vs"LexicalDecision"). -
If the script fails with “Unauthorized”:
Make sure your token is correct and has all permissions. -
If files download but seem old:
Check theCUTOFF_DATEvalue in the script—it filters files modified before a certain date. -
If you want to redownload everything:
Delete theosf_downloadsfolder and rerun the script.
8 · Summary
The OSF import script allows R to connect directly to your OSF project, list available data files, and download only those you specify.
You control which files are fetched using the prefix and date filters.
By including this import step at the top of your workflow, your project becomes fully reproducible: anyone with the same token and node information can re-create your data folder from OSF automatically.