Skip to content

Commit

Permalink
docs: fix precourse tasks so fs is loaded with here and to delete dir…
Browse files Browse the repository at this point in the history
… folder to fix zipping problems

Closes #48, closes #34
  • Loading branch information
lwjohnst86 committed May 4, 2024
1 parent ed8d067 commit 27ff494
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
5 changes: 4 additions & 1 deletion R/pre-course-script.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
library(here)
library(fs)

# Download
mmash_link <- "https://physionet.org/static/published-projects/mmash/multilevel-monitoring-of-activity-and-sleep-in-healthy-people-1.0.0.zip"
# download.file(mmash_link, destfile = here("data-raw/mmash-data.zip"))

# Remove leftover folder so unzipping is always clean
dir_delete(here("data-raw/mmash"))

# Unzip
unzip(here("data-raw/mmash-data.zip"),
exdir = here("data-raw"),
Expand All @@ -15,7 +19,6 @@ unzip(here("data-raw/MMASH.zip"),
)

# Remove/tidy up left over files
library(fs)
file_delete(here(c(
"data-raw/MMASH.zip",
"data-raw/SHA256SUMS.txt",
Expand Down
47 changes: 36 additions & 11 deletions preamble/pre-course.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,40 @@ A quick comment about whether you should save your *raw* data in
projects to it and use their individual `data-raw/` R scripts as the
record for how you processed the raw data.

Unzip the zip files by using the `unzip()` function and writing it in
`data-raw/mmash.R` below the `download.file()` function. The main
argument for `unzip()` is the zip file and the next important one is
called `exdir` that tells `unzip()` which folder you want to extract the
files to. The argument `junkpaths` is used here to tell `unzip()` to
extract everything to the `data-raw/` folder (no idea why it's called
"junkpaths"). This code should be written and executed in the
`data-raw/mmash.R` script.
We will eventually want to unzip the file, but before we begin, we want
to include some code to always delete the unzipped output folder. We
want to do this because sometimes unzipping can cause issues and because
we want the script to always run everything cleanly from beginning to
end whenever we `source()` it. In order to delete the folder, we'll use
the `{fs}` package to handle filesystem actions. Load the `{fs}` package
with `library()` right below the other `library()` function at the top
of the script:

```{r}
#| filename: "data-raw/mmash.R"
library(here)
library(fs)
```

Next, we'll use the `dir_delete()` to tell `{fs}` to always delete the
`data-raw/mmash/` output folder that we will create shortly. Put this
below the `dowload.file()` code:

```{r}
#| filename: "data-raw/mmash.R"
#| eval: false
# Remove leftover folder so unzipping is always clean
dir_delete(here("data-raw/mmash"))
```

Now we can unzip the zip files by using the `unzip()` function and
writing it in `data-raw/mmash.R` below the `download.file()` function.
The main argument for `unzip()` is the zip file and the next important
one is called `exdir` that tells `unzip()` which folder you want to
extract the files to. The argument `junkpaths` is used here to tell
`unzip()` to extract everything to the `data-raw/` folder (no idea why
it's called "junkpaths"). This code should be written and executed in
the `data-raw/mmash.R` script.

```{r admin-mmash-dir-exists}
#| include: false
Expand Down Expand Up @@ -558,8 +584,8 @@ If your files and folders in the `data-raw/` folder do not look like
this, start over by deleting all the files *except for* the `mmash.R`
and `mmash-data.zip` files. Then re-run the code from beginning to end.

To tidy up the files, first, use the `file_delete()` function from fs to
delete all the files we originally extracted (`LICENSE.txt`,
To tidy up the files, first, use the `file_delete()` function from
`{fs}` to delete all the files we originally extracted (`LICENSE.txt`,
`SHA256SUMS.txt`, and `MMASH.zip`). Then use `file_move()` to rename the
new folder `data-raw/DataPaper/` to something more explicit like
`data-raw/mmash/`. Add these lines of code to the `data-raw/mmash.R`
Expand All @@ -568,7 +594,6 @@ script and run them:
```{r delete-rename-files}
#| filename: "data-raw/mmash.R"
#| eval: !expr skip
library(fs)
file_delete(here(c(
"data-raw/MMASH.zip",
"data-raw/SHA256SUMS.txt",
Expand Down
3 changes: 2 additions & 1 deletion sessions/dplyr-joins.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ some code to delete the created `data-raw/mmash/` folder so this script
can run cleanly each time. Place the code right below where the
`download.file()` code is.

<!-- TODO: Remove this after course ends, since the same info is in pre-course now -->

```{r data-raw-mmash-comment-out}
#| filename: "data-raw/mmash.R"
#| eval: false
Expand Down Expand Up @@ -726,4 +728,3 @@ next session.
rm(actigraph_df, rr_df)
save.image(here::here("_temp/dplyr-joins.RData"))
```

14 changes: 9 additions & 5 deletions sessions/functionals.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,22 @@ user_info_df <- import_multiple_files("user_info.csv", import_user_info)
saliva_df <- import_multiple_files("saliva.csv", import_saliva)
```

<!-- TODO: Delete the text about the library(fs), since that will be fixed after the course -->

To test that things work, we'll create an HTML document from our Quarto
document by using the "Render" / "Knit" button at the top of the pane or
with {{< var keybind.render >}}. Once it creates the file, it should
either pop up or open in the Viewer pane on the side. If it works, then
we can move on and open up the `data-raw/mmash.R` script. Inside the
script, copy and paste these two lines of code to the bottom of the
script. Afterwards, go the top of the script and right below the
`library(here)` code, add these two lines of code, so it looks like
this:
we can move on and open up the `data-raw/mmash.R` script. Before
continuing, we'll move the `library(fs)` line to right below the
`library(here)`. Then, inside `data-raw/mmash.R`, copy and paste the two
lines of code in the code chunk above to the bottom of the script.
Afterwards, go the top of the script and right below the `library(fs)`
code, add these two lines of code, so it looks like this:

``` {.r filename="data-raw/mmash.R"}
library(here)
library(fs)
library(tidyverse)
source(here("R/functions.R"))
```
Expand Down

0 comments on commit 27ff494

Please sign in to comment.