Skip to main content

6  Importing Data and Project Paths

6.1 Why This Module Matters

Data analysis usually starts by reading a file. If the file path only works on your computer, the analysis is fragile. If the file path works inside the project, the analysis is much easier to share, grade, revise, and trust.

flowchart TD
  A[Book root] --> B[book/chapters]
  A --> C[book/assets]
  A --> D[materials/week_03/data]
  B --> E[Chapter source]
  D --> F[CSV or input data]

This module teaches a simple rule:

Code should find files from the project, not from your personal computer.

That means we avoid paths like C:/Users/Chris/Downloads/data.csv or /Users/name/Desktop/data.csv. Those paths describe one person’s machine. They do not describe the project.

6.2 Learning Goals

By the end of this module, you should be able to:

  • explain why project-relative paths matter
  • import a CSV file with read_csv()
  • build a file path with here()
  • inspect imported data before analyzing it
  • identify common import problems
  • avoid using setwd() in course assignments

6.3 Vocabulary

Term Meaning in this course Example
CSV A plain-text table where values are separated by commas weather_raw.csv
import Read data from a file into R read_csv()
path The location of a file data/weather_raw.csv
project-relative path A path that starts from the project folder here("data", "weather_raw.csv")
absolute path A path that starts from the whole computer /Users/name/Downloads/file.csv
working directory The folder R uses as its current starting place project folder
reproducible Able to run again on another computer or later date project-based file paths
glimpse A quick view of column names and types glimpse(weather_raw)

6.4 Before You Start

Download the Module 03 practice project (.zip), extract it, and open the extracted module_03_practice folder in Positron. Open scripts/week_03_follow_along.R and keep the CSV files in data/. The week_03 filenames are retained for existing links; this is Module 03.

The canonical Module 03 files are:

  • materials/week_03/week_03.Rproj
  • materials/week_03/scripts/week_03_follow_along.R
  • materials/week_03/data/weather_raw.csv
  • materials/week_03/data/stream_gauge_messy.csv

The data files are in the project. That location is part of the lesson. Your code should point to the project files, not to a copy in Downloads or on the Desktop.

6.5 The Main Idea

A project is like a file cabinet. The project folder contains the scripts, data, rendered outputs, and supporting files for one piece of work.

When you use project-relative paths, your code says:

Start in this project, then go to the data folder, then open this file.

That instruction can work for you, your instructor, a TA, or GitHub Actions because it does not depend on your personal computer name.

6.6 Build The Workflow

6.6.1 Load Packages

Module 03 uses readr for importing CSV files and here for project-relative paths.

library(readr)
library(here)
library(dplyr)

If the full course script loads tidyverse, that also loads readr and dplyr.

6.6.2 Import A CSV File

Use read_csv() to read a CSV file.

weather_raw <- read_csv(here("data", "weather_raw.csv"))

Read this code as:

  • use here() to build a path inside the project
  • go into the data folder
  • find weather_raw.csv
  • read the CSV file
  • store the result as weather_raw

The object name weather_raw is a reminder that the data have just been imported. They have not been cleaned or checked yet.

6.6.3 Inspect The Imported Data

After importing, inspect the object.

glimpse(weather_raw)
summary(weather_raw)
nrow(weather_raw)
names(weather_raw)

Pause here even if the import worked. Opening a file only means R found it; you still need to check the column types, row counts, and values before trusting the data.

6.6.4 Import A Messier File

The stream gauge file has five metadata lines before its column headers. Open it as text, count those lines, and use skip = 5 to start at the header. Leave the raw file unchanged.

gauge_raw <- read_csv(here("data", "stream_gauge_messy.csv"), skip = 5)
glimpse(gauge_raw)

Use the name gauge_raw because this is the imported form. Later, you may create a cleaned object with a different name.

6.6.5 Make A Small Cleaning Step

If a column name is hard to use or unclear, rename it.

gauge_clean <- gauge_raw |>
  rename(discharge = discharge_cfs)

Read this code as:

  • start with gauge_raw
  • rename the column discharge_cfs
  • give the renamed column the shorter name discharge
  • store the result as gauge_clean

6.6.6 Check Missing Values

After importing and renaming, check for missing values.

gauge_clean |>
  filter(is.na(discharge))

This asks R to show rows where discharge is missing. Missing values are not automatically bad, but they must be noticed.

6.7 Read The Code

This line is the center of the module:

read_csv(here("data", "weather_raw.csv"))

Read it from the inside out:

  • here("data", "weather_raw.csv") builds the path data/weather_raw.csv from the project root.
  • read_csv(...) reads the file at that path.
  • The result is a data frame.

This is better than:

read_csv("/Users/my_name/Desktop/weather_raw.csv")

The second path may work for one person today. It will not work for another person, GitHub Actions, or even you after moving the file.

6.8 Check Your Work

After importing data, check:

  • Does the object appear in the Environment pane?
  • Does glimpse() show the expected column names?
  • Are numeric columns stored as numbers?
  • Are date columns stored as dates or character strings?
  • How many rows were imported?
  • Are there missing values in important columns?
  • Does the script run after restarting R?

Restarting R is a strong test. It shows whether the script creates the objects it needs instead of depending on leftovers in your session.

6.9 Common Problems

Symptom Likely cause First thing to try
No such file or directory The path does not point to the file Check file name, folder name, and project location
Code works for you but not someone else You used an absolute path Replace it with here("data", "file.csv")
R cannot find here() The package is not loaded Run library(here)
Column names look wrong The file may have been read with the wrong delimiter or header Open the CSV as text and inspect the first lines
Numbers are stored as text The column contains non-numeric values Use glimpse() and inspect the raw values
A script works only after clicking around The workflow depends on manual setup Restart R and run the script from top to bottom

6.10 Try It

Base task:

Import weather_raw.csv using here(). Run glimpse(), summary(), and nrow() on the imported object. Write one sentence that describes what each inspection function tells you.

Extension:

Import stream_gauge_messy.csv. Find at least one row that needs attention before analysis. Leave the full cleanup for later, and write one sentence explaining what you noticed and why it matters.

6.11 Connect To The Assignment

Assignment 03 - Import and Inspect asks you to use a reproducible file workflow on data/stream_gauge_data.csv, a file with five metadata lines above the header. Accept it in Classroom 50 (sign in with your GitHub account, select NRES800), clone it in Positron, and complete assignment.R. Your code should work when the project is opened fresh.

This assignment is done at home and pushed from home. There is no in-class upload. It is due Sunday, Sep 27, 11:59 PM.

Where the written parts go:

  • The provenance note, the row-count check, the red-flag explanation, and the AI note are written as # comments inside assignment.R, directly under the matching section heading. Do not create a separate document.
  • The README gets only a 2-3 sentence description of the repository. It is scored from this assignment on.

Before submitting, ask:

  • Did I use files inside the project?
  • Did I avoid setwd()?
  • Did I avoid Desktop or Downloads paths?
  • Did I inspect the imported data with head() and glimpse()?
  • Does nrow(stream_data) match the number of data rows in the CSV?
  • Did I explain the suspicious value using the actual number?

If your code depends on a file location outside the project, the assignment is not reproducible yet.

After you push, the checks run and feedback arrives as a GitHub Issue in your repository. A red X means “look here,” not a grade: open the check, read the first error, fix, and push again.

6.12 Finish The Professional Workflow

Before you stop:

  • Restart R.
  • Run the script from top to bottom.
  • Check that the console shows the expected data inspection results and the printed summary.
  • Confirm the raw CSV in data/ is unchanged.
  • Commit assignment.R and the README.
  • Push to GitHub.
  • Confirm that GitHub has the latest version and that the check ran.

The point is not only to import data once. The point is to build an import workflow that can run again on your computer, on GitHub, and later in the semester.