Skip to content

Commit

Permalink
Merge pull request #212 from carpentries-incubator/here
Browse files Browse the repository at this point in the history
per #210 using here:here()
  • Loading branch information
rcurty authored Aug 12, 2022
2 parents 9cac5ab + c787bec commit 67493d2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
8 changes: 8 additions & 0 deletions _episodes/04-good-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ In the complete path example you can see that the code is not going to be portab
> {: .solution}
{: .challenge}

> ## Tip: Level up your relative paths
> We just discussed how relative paths are a better practice when coding because we can guarantee our code will work on somebody else's system. However, relative paths can still be quite confusing to deal with, especially when you have many sub-directories in your project. One way to make things a bit easier on ourselves is to make sure the part that's *relative* to what we're referencing is always the same.
> We can employ a package called "here" to do this. `Here` has a function which always references the root (or top-level) directory of your project (i.e. where the .rproj file lives). Conviniently, that function is called `here()`. `here()` gives us a consistent starting path when building relative paths. We'll see how this is used later in the lesson.
- Read the CRAN documentation here: [here](https://cran.r-project.org/web/packages/here/vignettes/here.html)
- [Read more about how the `here` package can be useful for R Markdown files specifically](http://jenrichmond.rbind.io/post/how-to-use-the-here-package/)
{: .callout}

#### **Treat data as read only**
This is probably the most important goal of setting up a project. Data is typically time consuming and/or expensive to collect. Working with them interactively (e.g., in Excel or R) where they can be modified means you are never sure of where the data came from, or how it has been modified since collection. It is therefore a good idea to treat your data as “read-only”. However, in many cases your data will be “dirty”: it will need significant preprocessing to get into a format R (or any other programming language) will find useful. Storing these scripts in a separate folder, and creating a second “read-only” data folder to hold the “cleaned” data sets can prevent confusion between the two sets. You should have separate folders for each: raw data, code, and output data/analyses. You wouldn’t mix your clean laundry with your dirty laundry, right?

Expand Down
15 changes: 8 additions & 7 deletions _episodes/08-code-chunks.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ The logical fix for this would be to adjust the relative file path to read the e
> ## Challenge 8.1 Relative Path Madness
> change the relative path for the externally sourced code from `code/03_HR_analysis.R` to `../../code/03_HR_analysis.R`
>
> What is going wrong here?
> What was going on here? Why did we get an error when running the code this time?
>
> > ## Solution:
> > The issue is that the code we are calling from within the rmd document contains file paths to read and save the data that are relative to the code directory where the `03_HR_analysis.R` script resides so the paths aren’t correct when run from the .rmd file. Yesh! All sorts of relative path chaos - don’t worry if you are confused.
> > The issue is that the code we are calling from within the rmd document contains file paths to read and save the data that are relative to the code directory where the `03_HR_analysis.R` script resides so the paths aren’t correct when run from the .rmd file. Yesh!
> {: .solution}
{: .challenge}


Well, there is a solution for this as well! (As with most every obstacle you run into with R). What we will do is use a handy feature for R Markdown which allows us to change the working directory of our R Markdown document. This means whenever using relative paths in our project they can always* be relative to the root directory, allowing us to standardize our relative file path and allieviate this file connection error. To do this we will learn additional global Knitr options.
Thankfully, there is a solution for this! (As with most every obstacle you run into with R). What we will do is use a handy feature for R Markdown which allows us to change the working directory of our R Markdown document. This means whenever using relative paths in our project they can always* be relative to the root directory, allowing us to standardize our relative file path and allieviate this file connection error. To do this we will learn additional global Knitr options.

*almost always

Expand All @@ -131,15 +131,16 @@ Now, what might be the issue with changing the working directory through the IDE

### Option #2: Change the working directory with a bit of code

The second option requires a bit of code, but will overall be more reproducible (because it's not dependent on your personal RStudio IDE settings)
This is a setting option in our global knitr settings:
The second option requires a bit of code, but will overall be more reproducible (because it's not dependent on your personal RStudio IDE settings). To accomplish this, we will now use the `here` package introduced previously.

This is also a setting option in our global knitr settings:

We will now navigate back up to the top of our `.rmd` document to the setup code chunk. Then, we'll add the following line of code before or after our code for code chunk options (it wouldn't hurt to add a comment explaining what it does either):

~~~
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
knitr::opts_knit$set(root.dir = here::here())
~~~
*notice this code uses one of our pre-installed packages `rprojroot` to
*notice again this code uses a function from one of our pre-installed packages `here`.

![global knitr settings](../fig/08-global-knitr-options.png)

Expand Down
Binary file modified fig/08-global-knitr-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ We will be covering the purpose of using packages and recap different ways to in

1) Open R studio
2) Select from the upper menu `Tools > Install packages...` or click on the `Packages` tab in the bottom-right section and then click on install. Either action will prompt a box dialog.
3) In the `Install Packages` dialog box, copy this command `bookdown, tidyverse, rticles, BayesFactor, patchwork, rprojroot` under the Packages field, make sure the option `install dependencies` is selected, keep other information unchanged, and then click `install`.
3) In the `Install Packages` dialog box, copy this command `bookdown, tidyverse, rticles, BayesFactor, patchwork, here` under the Packages field, make sure the option `install dependencies` is selected, keep other information unchanged, and then click `install`.
4) Don't be alarmed by the stop sign that will blink (and do not click on it otherwise you will cancel the process) or the red text messages. Once the process completes the cursor will be preceeded by a greater-than sign `>`.

*Entering function in the console*
Expand All @@ -42,11 +42,11 @@ We will be covering the purpose of using packages and recap different ways to in
install.packages("rticles")
install.packages("BayesFactor")
install.packages("patchwork")
install.packages("rprojroot")``
install.packages("here")``

or

`install.packages(c("bookdown", "tidyverse", "rticles", "BayesFactor", "patchwork", "rprojroot"))`
`install.packages(c("bookdown", "tidyverse", "rticles", "BayesFactor", "patchwork", "here"))`

2) Don't be alarmed by the stop sign that will blink (and do not click on it otherwise you will cancel the process) or the red text messages. Once the process completes the cursor will be preceeded by a greater-than sign `>`.

Expand Down

0 comments on commit 67493d2

Please sign in to comment.