-
Notifications
You must be signed in to change notification settings - Fork 10
/
z-advanced-topic-reproducible-analytical-pipelines-with-nix.Rmd
103 lines (81 loc) · 3.63 KB
/
z-advanced-topic-reproducible-analytical-pipelines-with-nix.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
title: "z - Advanced topic: Reproducible Analytical Pipelines with Nix"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{z-advanced-topic-reproducible-analytical-pipelines-with-nix}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r, include=FALSE}
library(rix)
```
## Introduction
Isolated environments are great to run pipelines in a safe and reproducible
manner. This vignette details how to build a reproducible analytical pipeline
using an environment built with Nix that contains the right version of R and
packages.
## An example of a reproducible analytical pipeline using Nix
Suppose that you've used `{targets}` to build a pipeline for a project and that
you did so using a tailor-made Nix environment. Here is the call to `rix()` that
you could have used to build that environment:
```{r parsermd-chunk-1}
path_default_nix <- tempdir()
rix(r_ver = "4.2.2",
r_pkgs = c("targets", "tarchetypes", "rmarkdown"),
system_pkgs = NULL,
git_pkgs = list(package_name = "housing",
repo_url = "https://github.com/rap4all/housing/",
branch_name = "fusen",
commit = "1c860959310b80e67c41f7bbdc3e84cef00df18e"),
ide = "other",
project_path = path_default_nix,
overwrite = TRUE)
```
This call to `rix()` generates the following `default.nix` file:
```{r parsermd-chunk-2, echo = F}
cat(readLines(paste0(path_default_nix, "/default.nix")), sep = "\n")
```
The environment that gets built from this `default.nix` file contains R version
4.2.2, the `{targets}` and `{tarchetypes}` packages, as well as the `{housing}`
packages, which is a package that is hosted on GitHub only with some data and
useful functions for the project. Because it is on Github, it gets installed
using the `buildRPackage` function from Nix. You can use this environment to
work on you project, or to launch a `{targets}` pipeline. [This Github
repository](https://github.com/b-rodrigues/nix_targets_pipeline/tree/master)
contains the finalized project.
On your local machine, you could execute the pipeline in the environment by
running this in a terminal:
```
cd /absolute/path/to/housing/ && nix-shell default.nix --run "Rscript -e 'targets::tar_make()'"
```
If you wish to run the pipeline whenever you drop into the Nix shell, you could
add a *Shell-hook* to the generated `default.nix` file:
```{r parsermd-chunk-3, eval = FALSE}
rix(r_ver = "4.2.2",
r_pkgs = c("targets", "tarchetypes", "rmarkdown"),
system_pkgs = NULL,
git_pkgs = list(package_name = "housing",
repo_url = "https://github.com/rap4all/housing/",
branch_name = "fusen",
commit = "1c860959310b80e67c41f7bbdc3e84cef00df18e"),
ide = "other",
shell_hook = "Rscript -e 'targets::tar_make()'",
project_path = path_default_nix,
overwrite = TRUE)
```
Now, each time you drop into the Nix shell for that project using `nix-shell`,
the pipeline gets automatically executed. `{rix}` also features a function
called `tar_nix_ga()` that adds a GitHub Actions workflow file to make the
pipeline run automatically on GitHub Actions. The GitHub repository linked above
has such a file, so each time changes get pushed, the pipeline runs on Github
Actions and the results are automatically pushed to a branch called
`targets-runs`. See the workflow file
[here](https://github.com/b-rodrigues/nix_targets_pipeline/blob/master/.github/workflows/run-pipeline.yaml).
This feature is very heavily inspired and adapted from the
`targets::github_actions()` function.