Positron Setup

TipLearning Objectives

After completing this session, you will be able to:

  • Define critical Positron vocabulary (e.g., “absolute file path”, “working directory”)
  • Describe tenets of effective project organization
  • Create a practice Positron folder that is well-organized
  • Explain how file paths operate within a working directory

2 Effective Project Organization

When starting a new research project, one of the first things I do is make a folder for it and link that to Positron (just like we have here!). The next step is to then populate that project folder with relevant sub-folders. The goal is to organize your project so that it is a compendium of your research. This means that the project has all of the digital parts needed to replicate your analysis, including code, figures, manuscript drafts, and data.

Some common directories are:

  • data: where we store our data (often contains subdirectories for raw and processed data, as well as metadata)
  • docs: summaries or reports of analysis or other relevant project information
  • plots or figs: generated plots, graphs, and figures
  • scripts: has all scripts where you clean and wrangle data and run your analysis.
  • tools: contains scripts with your custom functions

Directory organization will vary from project to project, but the ultimate goal is to create a well-organized project for both reproducibility and collaboration.

training_oharac
 |–  README.md
 |–  data
 |      |–  raw
 |      L   processed
 |–  plots
 |–  scripts
 |      |–  01_harmonize.r
 |      |–  02_wrangle.r
 |      L   03_analyze.r
 |–  docs
 |      L   2026-04_results.pdf
 L   tools
        L   calc-diversity.r

TipProject Sub-Directories

For this week we are going to create 3 folders (a.k.a. “directories”) in our training_{username} folder.

  1. In your file manager ( “Finder” | “File Explorer”), make three new folders: data, plots, scripts
  2. Go to the “Explorer” tab of Positron (top left; has an icon like this: )
  3. Confirm that your new folders are visible to Positron

Note that if you prefer, you can make new folders directly from the “Explorer” tab of Positron (by clicking the small button of a folder with a plus sign)

3 Using File Paths in an Positron Folder

Now that we have your project folder created and linked with Positron, let’s learn how to move in a project. We do this using “paths”.

In computing, there are two types of path: “absolute” and “relative”.

An absolute path always starts with the root of your file system ( something like /Users | something like C:\) and locates files from there. No two users will have the same absolute file path, so using an absolute file path is a recipe for a non-reproducible workflow!

Example:
/Users/lyon/Documents/iowa-state/ms-chap-01/data/raw-butterflies-2017.xlsx

A relative path starts from some location in your file system that is below the root (i.e., it is the file path relative to some folder in your computer). Relative paths are combined with the path to that location to locate files on your system. Some coding languages refer to the location where the relative path starts as our “working directory”. Others working on the same project on their local computer will have the same relative file structure, so using relative file paths enables (but does not guarantee) a reproducible workflow!

Example:
data/raw-butterflies-2017.xlsx

Positron automatically sets the working directory to the folder you linked! This means that you can reference files from within the project without worrying about where the project directory itself is.

For example, if I want to read in a file from the data directory within my training project, I can simply type read.csv("data/samples.csv") as opposed to read.csv("/Users/lyon/Documents/delta/training_oharac/data/samples.csv").

This is a time-saver for you when you work alone (because you’re not typing the absolute path every time) but when working in teams it is the key to the same code files being run-able by everyone in the project! For example, a collaborator with the same relative path can use read.csv("data/samples.csv") without editing the code while they would need to make several edits to the absolute path for it to work for them.

3.1 File Paths Across Operating Systems

If your team includes both Mac/Linux and Windows users, even relative paths could potentially fail because the two operating systems use a different direction of slash to separate pieces of the file path ( data/samples.csv | data\samples.csv). To complicate things, R has a special use for the backslash \. Fortunately, easy solutions exist to ensure cross platform compatibility:

  • R for Windows understands both types of slashes, so you can always just use the forward slash /.
  • The file.path() function in R is useful for building and automating file paths, and automatically detects the computer’s operating system and insert the correct slash type for that computer.

3.2 Avoid Manually Setting your Working Directory!

Note that once you start working in Positron folders you should basically never need to run the setwd() command. If you are in the habit of doing this, stop and take a look at where and why you do it. Could leveraging the working directory eliminate this need? Almost definitely!

Similarly, think about how you work with absolute paths. Could you leverage the working directory of your R project to replace these with relative paths and make your code more portable? Probably!

Artwork by Allison Horst. A cartoon of a cracked glass cube looking frustrated with casts on its arm and leg, with bandaids on it, containing “setwd,” looks on at a metal riveted cube labeled “R Proj” holding a skateboard looking sympathetic, and a smaller cube with a helmet on labeled “here” doing a trick on a skateboard.