# Working with file paths

Paths can be absolute (`/Users/blabber/Documents/data.csv`) or relative (`data.csv`). When you use relative paths, the program needs to know what directory they are relative to. We call that directory (`/Users/blabber/Documents/`) the working directory.

## Knit Directory

When you knit an R-Markdown file in RStudio, you can set the "Knit Directory" - the working directory that will be used when knitting the document. Among other things, every relative path will be considered to be relative to that folder. So, when you write `read_csv('data/penguins.csv')`, R will look for \``data/penguins.csv`\` in that directory.

It can be set to the "Project Directory" (commonly used in BLab), to the "Document Directory" (commonly used outside of BLab) or "Current Working Directory" (at times, useful as a temporary setting). Instead of relying on everyone having "Knit Directory" set to the same option in RStudio, we should define paths in a way that is agnostic to that setting.

### `here` and `fs::path`

One way to do this is to use package `here` and function `fs::path`. We will use `here` to find folders within our project folder and we will use `fs::path` to construct path to files *in relation to these folders*. Like this:

```r
data_folder <- fs::path(here::here('data'))
ant_info <- read_csv(data_folder / 'ant_info.csv')
```

{% hint style="info" %}
Do not use `library(here)` because it prints out the absolute path to the project folder which will be user-specific and will produce unnecessary changes to the knit notebooks.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.bergelsonlab.com/programming-info/quick-programming-computing-how-tos/paths.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
