Skip to content

Commit

Permalink
reading chp file tree fig update
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorcampbell committed Dec 20, 2023
1 parent 8e365c2 commit d4ac466
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Binary file removed img/reading/filesystem.jpeg
Binary file not shown.
Binary file added img/reading/filesystem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions source/reading.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,27 @@ with respect to the computer's filesystem base (or *root*) folder, regardless of

Suppose our computer's filesystem looks like the picture in Figure
\@ref(fig:file-system-for-export-to-intro-datascience). We are working in a
file titled `worksheet_02.ipynb`, and our current working directory is `worksheet_02`;
file titled `project3.ipynb`, and our current working directory is `project3`;
typically, as is the case here, the working directory is the directory containing the file you are currently
working on.

```{r file-system-for-export-to-intro-datascience, echo = FALSE, message = FALSE, warning = FALSE, fig.align = "center", fig.cap = "Example file system.", fig.retina = 2, out.width="100%"}
knitr::include_graphics("img/reading/filesystem.jpeg")
knitr::include_graphics("img/reading/filesystem.png")
```

Let's say we wanted to open the `happiness_report.csv` file. We have two options to indicate
where the file is: using a relative path, or using an absolute path.
The absolute path of the file always starts with a slash `/`—representing the root folder on the computer—and
proceeds by listing out the sequence of folders you would have to enter to reach the file, each separated by another slash `/`.
So in this case, `happiness_report.csv` would be reached by starting at the root, and entering the `home` folder,
then the `dsci-100` folder, then the `worksheet_02` folder, and then finally the `data` folder. So its absolute
path would be `/home/dsci-100/worksheet_02/data/happiness_report.csv`. We can load the file using its absolute path
then the `dsci-100` folder, then the `project3` folder, and then finally the `data` folder. So its absolute
path would be `/home/dsci-100/project3/data/happiness_report.csv`. We can load the file using its absolute path
as a string passed to the `read_csv` function.
```{r eval = FALSE}
happy_data <- read_csv("/home/dsci-100/worksheet_02/data/happiness_report.csv")
happy_data <- read_csv("/home/dsci-100/project3/data/happiness_report.csv")
```
If we instead wanted to use a relative path, we would need to list out the sequence of steps needed to get from our current
working directory to the file, with slashes `/` separating each step. Since we are currently in the `worksheet_02` folder,
working directory to the file, with slashes `/` separating each step. Since we are currently in the `project3` folder,
we just need to enter the `data` folder to reach our desired file. Hence the relative path is `data/happiness_report.csv`,
and we can load the file using its relative path as a string passed to `read_csv`.
```{r eval = FALSE}
Expand All @@ -119,13 +119,13 @@ happy_data <- read_csv("data/happiness_report.csv")
Note that there is no forward slash at the beginning of a relative path; if we accidentally typed `"/data/happiness_report.csv"`,
R would look for a folder named `data` in the root folder of the computer&mdash;but that doesn't exist!

Aside from specifying places to go in a path using folder names (like `data` and `worksheet_02`), we can also specify two additional
Aside from specifying places to go in a path using folder names (like `data` and `project3`), we can also specify two additional
special places: the *current directory* \index{path!current} and the *previous directory*. \index{path!previous}
We indicate the current working directory with a single dot `.`, and \index{aaaaaacurdirsymb@\texttt{.}|see{path}}
the previous directory with two dots `..`. \index{aaaaaprevdirsymb@\texttt{..}|see{path}} So for instance, if we wanted to reach the `bike_share.csv` file from the `worksheet_02` folder, we could
use the relative path `../tutorial_01/bike_share.csv`. We can even combine these two; for example, we could reach the `bike_share.csv` file using
the (very silly) path `../tutorial_01/../tutorial_01/./bike_share.csv` with quite a few redundant directions: it says to go back a folder, then open `tutorial_01`,
then go back a folder again, then open `tutorial_01` again, then stay in the current directory, then finally get to `bike_share.csv`. Whew, what a long trip!
the previous directory with two dots `..`. \index{aaaaaprevdirsymb@\texttt{..}|see{path}} So for instance, if we wanted to reach the `bike_share.csv` file from the `project3` folder, we could
use the relative path `../project2/bike_share.csv`. We can even combine these two; for example, we could reach the `bike_share.csv` file using
the (very silly) path `../project2/../project2/./bike_share.csv` with quite a few redundant directions: it says to go back a folder, then open `project2`,
then go back a folder again, then open `project2` again, then stay in the current directory, then finally get to `bike_share.csv`. Whew, what a long trip!

So which kind of path should you use: relative, or absolute? Generally speaking, you should use relative paths.
Using a relative path helps ensure that your code can be run
Expand All @@ -136,17 +136,17 @@ all of the folders between the computer's root, represented by `/`, and the file
across different computers. For example, suppose Fatima and Jayden are working on a
project together on the `happiness_report.csv` data. Fatima's file is stored at

`/home/Fatima/project/data/happiness_report.csv`,
`/home/Fatima/project3/data/happiness_report.csv`,

while Jayden's is stored at

`/home/Jayden/project/data/happiness_report.csv`.
`/home/Jayden/project3/data/happiness_report.csv`.

Even though Fatima and Jayden stored their files in the same place on their
computers (in their home folders), the absolute paths are different due to
their different usernames. If Jayden has code that loads the
`happiness_report.csv` data using an absolute path, the code won't work on
Fatima's computer. But the relative path from inside the `project` folder
Fatima's computer. But the relative path from inside the `project3` folder
(`data/happiness_report.csv`) is the same on both computers; any code that uses
relative paths will work on both! In the additional resources section,
we include a link to a short video on the
Expand Down

0 comments on commit d4ac466

Please sign in to comment.