Skip to content

Commit

Permalink
turned vignettes static to avoid compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Jan 8, 2025
1 parent ab08015 commit 1a906b6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,19 @@ knitr::opts_chunk$set(
)
```

```{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, include = F}
path_default_nix <- paste0(
tempdir(), "repo",
paste0(sample(letters, 5), collapse = "")
)
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/",
commit = "1c860959310b80e67c41f7bbdc3e84cef00df18e"
),
ide = "other",
project_path = path_default_nix,
overwrite = TRUE
)
```

```{r, eval = F}
path_default_nix <- tempdir()
Expand All @@ -73,12 +47,59 @@ rix(

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")
```

```{r, include = FALSE}
unlink(path_default_nix, recursive = TRUE, force = TRUE)
let
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/2023-02-13.tar.gz") {};
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages)
rmarkdown
tarchetypes
targets;
};
housing = (pkgs.rPackages.buildRPackage {
name = "housing";
src = pkgs.fetchgit {
url = "https://github.com/rap4all/housing/";
rev = "1c860959310b80e67c41f7bbdc3e84cef00df18e";
sha256 = "sha256-s4KGtfKQ7hL0sfDhGb4BpBpspfefBN6hf+XlslqyEn4=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages)
dplyr
ggplot2
janitor
purrr
readxl
rlang
rvest
stringr
tidyr;
};
});
system_packages = builtins.attrValues {
inherit (pkgs)
glibcLocales
nix
R;
};
in
pkgs.mkShell {
LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
LANG = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LC_TIME = "en_US.UTF-8";
LC_MONETARY = "en_US.UTF-8";
LC_PAPER = "en_US.UTF-8";
LC_MEASUREMENT = "en_US.UTF-8";
buildInputs = [ housing rpkgs system_packages ];
}
```

The environment that gets built from this `default.nix` file contains R version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ before to R versions 4.2.0 and later.
First, we write a `default.nix` file containing Nix expressions that pin R
version 4.1.3 from Nixpkgs.

```{r parsermd-chunk-3}
```{r parsermd-chunk-3, eval = F}
library("rix")
path_env_1 <- file.path(".", "_env_1_R-4-1-3")
Expand All @@ -98,12 +98,31 @@ rix(
The following expression is written to default.nix in the subfolder
`./_env_1_R-4-1-3/`.

```{r parsermd-chunk-4, echo = FALSE}
cat(readLines(file.path(path_env_1, "default.nix")), sep = "\n")
```

```{r, include = FALSE}
unlink(path_env_1, recursive = TRUE, force = TRUE)
```{r parsermd-chunk-4}
let
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/2022-04-19.tar.gz") {};
system_packages = builtins.attrValues {
inherit (pkgs)
glibcLocales
nix
R;
};
in
pkgs.mkShell {
LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
LANG = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LC_TIME = "en_US.UTF-8";
LC_MONETARY = "en_US.UTF-8";
LC_PAPER = "en_US.UTF-8";
LC_MEASUREMENT = "en_US.UTF-8";
buildInputs = [ system_packages ];
}
```

This also includes a custom `.Rprofile` file that ensure that this subshell
Expand All @@ -116,7 +135,6 @@ in the folder `./_env_1_R-4-1-3`. Since you are sure you are using an R version
higher 4.2.0 available on your system, you can check what that
`as.vector.data.frame()` S3 method returns a list.


```{r parsermd-chunk-5}
df <- data.frame(a = 1:3, b = 4:6)
as.vector(x = df, mode = "list")
Expand Down Expand Up @@ -245,21 +263,8 @@ rix(
list.files(path_env_1_2)
```

```{r, include = F}
library("rix")
path_env_1_2 <- file.path(tempdir(), "_env_1_2_R-4-2-0")
rix(
r_ver = "4.2.0",
overwrite = TRUE,
project_path = path_env_1_2,
shell_hook = "R"
)
```

```{r, echo = F}
list.files(path_env_1_2)
"default.nix"
```

Now, initiate a new R session as development environment using `nix-shell`. Open
Expand Down

0 comments on commit 1a906b6

Please sign in to comment.