Skip to content

Commit e952622

Browse files
authored
Merge pull request #44 from carpentries-incubator/ep3-minoredits
small edits to episode 3 library loading
2 parents b62351d + 48b89c2 commit e952622

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

episodes/03-reproducible-dev-environment.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,15 @@ renv::install("my_package")
249249
Let's install the packages we need for this script. At this time, we need `jsonlite`, `lubridate` and `ggplot2`.
250250

251251
```r
252-
renv::install("jsonlite", "lubridate", "ggplot2"),
252+
renv::install("jsonlite", "lubridate", "ggplot2")
253253
```
254254

255-
I can also install packages in any of the usual ways, i.e., `install.packages()` or `pak::pkg_install("ggplot2")`, but you'll have to complete an additional step to update the `lock` file enumerating packages and dependencies. A call to `renv::snapshot()` should suffice.
255+
We can also install packages in any of the usual ways, i.e., `install.packages()` or `pak::pkg_install("ggplot2")` and `renv` will intercept and install it in the environment.
256+
Then we need to run the `snapshot()` function to update the `renv.lock` file with all the packages installed.
257+
258+
```r
259+
renv::snapshot()
260+
```
256261

257262
Now we can open the `renv.lock` file and see that it stores a lot of machine-readable information in plain text. However, you could also <kbd>COMMAND</kbd>+<kbd>F</kbd> (MacOS) or <kbd>CTRL</kbd>+<kbd>F</kbd> (Windows) to double check that the packages installed are now listed.
258263

@@ -266,19 +271,26 @@ Let's delete the packages we just installed and then restore them using the exis
266271
remove.packages(c("jsonlite", "lubridate", "ggplot2"))
267272
```
268273

274+
269275
If you attempt to load these packages now, your get an error
270276

271277
```r
272-
library("jsonlite)
278+
library("jsonlite")
273279
```
274280

281+
::::::::::::::: instructor
282+
283+
If the call to load `jsonlite` succeeds they might need to restart the R session because they loaded it previously and it is cached.
284+
285+
:::::::::::::::::::::::::::
286+
275287
```output
276288
Error in library(jsonlite) : there is no package called ‘jsonlite’
277289
```
278290

279291
To restore the packages from the `renv.lock`,
280292
```r
281-
renv::restore("renv.lock")
293+
renv::restore()
282294
```
283295

284296
If you attempt to load these packages now, it will work!
@@ -289,11 +301,11 @@ library("jsonlite)
289301
290302
### Ignoring files
291303
292-
Note that you only need to share the small `renv.lock` file with your collaborators - and not the entire `venv_spacewalks` directory with packages contained in your virtual environment.
293-
We need to tell git to ignore that directory, so it is not tracked and shared - we do this by adding `venv_spacewalks` to the `.gitignore` in the root directory of our project.
304+
Note that you only need to share the small `renv.lock` file with your collaborators - and not the entire `renv` directory with packages contained in your virtual environment.
305+
We need to tell git to ignore that directory, so it is not tracked and shared - we do this by adding `renv` to the `.gitignore` in the root directory of our project.
294306
295307
```bash
296-
(venv_spacewalks) $ echo "venv_spacewalks/" >> .gitignore
308+
$ echo "renv/" >> .gitignore
297309
```
298310
If you are a MacOS user, remember the `.DS_Store` hidden file which is also not necessary to share with our project?
299311
We can tell git to ignore it by adding it on a new line in `.gitignore` as pattern `**/.DS_Store` (so it will be ignored in any sub-folder of our project).
@@ -310,13 +322,23 @@ echo "**/.DS_Store" >> .gitignore
310322
Let's add and commit our updated `.gitignore` to our repository.
311323
312324
```bash
313-
(venv_spacewalks) $ git add .gitignore
314-
(venv_spacewalks) $ git commit -m "Ignore venv folder and DS_Store file"
325+
$ git add .gitignore
326+
$ git commit -m "Ignore renv folder and DS_Store file"
315327
```
316328
317329
The same method can be applied to ignore various other files that you do not want git to track.
318330
319331
332+
### Adding the lock file to the repo
333+
334+
We should also add our lock file to the repository so anyone who uses the repo going forward can rebuild our environment.
335+
336+
```bash
337+
$ git add renv.lock
338+
$ git commit -m "Adding renv.lock file"
339+
```
340+
341+
320342
:::::::::::::::::::::: callout
321343
322344
### Another challenge in (long-term) reproducibility
@@ -359,7 +381,7 @@ We are now setup to run our code from the newly created R project
359381
360382
```bash
361383
362-
(venv_spacewalks) $ **Rscript eva_data_analysis.R**
384+
$ Rscript eva_data_analysis.R
363385
364386
```
365387

0 commit comments

Comments
 (0)