Skip to content

Commit 17e5e19

Browse files
committed
feat(renvironments): add activities and further ideas sections (#85)
1 parent 9948cb3 commit 17e5e19

File tree

1 file changed

+91
-3
lines changed

1 file changed

+91
-3
lines changed

short_courses/r_environments.ipynb

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"* 📦 **Packages:** Dependency management using `renv`.\n",
3232
"* ⚙️ **Other:** Handling system libraries and project settings.\n",
3333
"* 🛠️ **Recreate & troubleshoot:** Restoring environments and common fixes.\n",
34-
"* 🧪 **Activity:** Hands-on practice with a sample repository.\n",
34+
"* 🧪 **Activities:** Have a go at R environment management!\n",
3535
"* 📚 **Further ideas:** Advanced tools and workflows for managing R environments.\n",
3636
"\n",
3737
"<br>\n",
@@ -493,16 +493,104 @@
493493
"\n",
494494
"``````\n",
495495
"\n",
496-
"``````{tab-item} 🧪 Activity\n",
496+
"``````{tab-item} 🧪 Activities\n",
497497
"\n",
498-
"## 🧪 Activity\n",
498+
"## 🧪 Activities\n",
499+
"\n",
500+
"Let's put theory into practice! These activities are designed to help you experience real-world workflows for managing R environments using `renv` and `rig`.\n",
501+
"\n",
502+
"### Activity 1: Create a reproducible R project from scratch\n",
503+
"\n",
504+
"**Goal:** Set up a new R project with `renv`, add a package, capture the environment for reproducibility, and share it with others.\n",
505+
"\n",
506+
"**Suggested package and script:** Use the `ggplot2` package, and create a script named `plot_example.R` with the following code:\n",
507+
"\n",
508+
"```\n",
509+
"library(ggplot2)\n",
510+
"ggplot(mtcars, aes(mpg, wt)) + geom_point()\n",
511+
"```\n",
512+
"\n",
513+
"```{dropdown} View solution\n",
514+
"\n",
515+
"1. **Create a new R project.**\n",
516+
" * In RStudio: Go to \"File\" > \"New Project...\" and choose \"New Directory\" or \"Existing Directory\" as needed.\n",
517+
"2. **Initialize `renv`.**\n",
518+
" * In the R console, run:\n",
519+
" ```\n",
520+
" install.packages(\"renv\")\n",
521+
" renv::init()\n",
522+
" ```\n",
523+
"3. **Install a package.**\n",
524+
" * For example, to add `ggplot2`, add this to a `DESCRIPTION` file and then run\n",
525+
" ```\n",
526+
" renv::install()\n",
527+
" ```\n",
528+
"4. **Write and run a simple script.**\n",
529+
" * Create a new R script (e.g., `plot_example.R`) with:\n",
530+
" ```\n",
531+
" library(ggplot2)\n",
532+
" ggplot(mtcars, aes(mpg, wt)) + geom_point()\n",
533+
" ```\n",
534+
" * Run the script to confirm everything works.\n",
535+
"5. **Snapshot the environment.**\n",
536+
" * Save the current state of your environment:\n",
537+
" ```\n",
538+
" renv::snapshot()\n",
539+
" ```\n",
540+
" * This updates the `renv.lock` file, recording all package versions.\n",
541+
"6. **Share your project.**\n",
542+
" * Your project folder now contains everything needed for someone else to reproduce your analysis - just share the folder (via GitHub, ZIP, etc.).\n",
543+
"\n",
544+
"Review the **📦 Packages** page for more information on these steps.\n",
545+
"\n",
546+
"```\n",
547+
"\n",
548+
"### Activity 2: Reproduce an existing R project\n",
549+
"\n",
550+
"**Goal:** Clone a repository with a pre-configured `renv` environment, switch to the correct R version, restore dependencies, and run a basic R script.\n",
551+
"\n",
552+
"Example repository: [amyheather/renv-reproduce-activity](https://github.com/amyheather/renv-reproduce-activity/)\n",
553+
"\n",
554+
"```{dropdown} View solution\n",
555+
"\n",
556+
"1. **Clone the example repository.**\n",
557+
" * `git clone https://github.com/amyheather/renv-reproduce-activity.git`\n",
558+
" * `cd renv-reproduce-activity`\n",
559+
"2. **Switch to the required R version.**\n",
560+
" * Check the `renv.lock` file or `README` for the required R version.\n",
561+
" * Use `rig` (or your preferred tool) to switch R versions.\n",
562+
"3. **Restore the project environment.**\n",
563+
" * Open the project in RStudio.\n",
564+
" * Run `renv::restore()`.\n",
565+
"4. **Run the example code.**\n",
566+
" * Locate and run the provided R script (`analysis.R`).\n",
567+
"5. **Troubleshoot (if needed).**\n",
568+
" * If you encounter errors, refer to the troubleshooting section in **🛠️ Recreate & troubleshoot**.\n",
569+
"\n",
570+
"```\n",
499571
"\n",
500572
"``````\n",
501573
"\n",
502574
"``````{tab-item} 📚 Further ideas\n",
503575
"\n",
504576
"## 📚 Further ideas\n",
505577
"\n",
578+
"If you are interested in exploring more advanced or alternative approaches to managing reproducible R environments - especially when dealing with complex external dependencies - consider the following tools and resources:\n",
579+
"\n",
580+
"### Docker for R\n",
581+
"\n",
582+
"Using Docker allows you to encapsulate your entire R environment, including system libraries, R versions, and packages, inside a container. This approach can solve many issues related to external dependencies and ensures that your analysis runs identically across different systems.\n",
583+
"\n",
584+
"* [`dockerfiler`](https://github.com/colinfay/dockerfiler): An R package for programmatically creating Dockerfiles, making it easier to build reproducible R environments with custom dependencies.\n",
585+
"* [`rocker`](https://www.rocker-project.org/): A collection of Docker images for R, maintained by the R community, which you can use as a base for your own projects.\n",
586+
"\n",
587+
"### R-universe\n",
588+
"\n",
589+
"[R-universe](https://r-universe.dev/) is an online platform for building, hosting, and distributing R packages—including those not available on CRAN. It can help you:\n",
590+
"\n",
591+
"* Access packages from multiple sources, not just CRAN.\n",
592+
"* Set up custom repositories (collections of R packages) for your team or organisation.\n",
593+
"* Centralise package management (as repositories can include public, private and experimental).\n",
506594
"``````\n",
507595
"\n",
508596
"```````\n",

0 commit comments

Comments
 (0)