Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brancengregory committed May 24, 2024
0 parents commit 05771cb
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### File generated by `rix::rix_init()` ###
# 1. Currently, system RStudio does not inherit environmental variables
# defined in `$HOME/.zshrc`, `$HOME/.bashrc` and alike. This is workaround to
# make the path of the nix store and hence basic nix commands available
# in an RStudio session
# 2. For nix-R session, remove `R_LIBS_USER`, system's R user library.`.
# This guarantees no user libraries from the system are loaded and only
# R packages in the Nix store are used. This makes Nix-R behave in pure manner
# at run-time.
{
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
is_nixr <- nzchar(Sys.getenv("NIX_STORE"))
if (isFALSE(is_nixr) && isTRUE(is_rstudio)) {
cat("{rix} detected RStudio R session")
old_path <- Sys.getenv("PATH")
nix_path <- "/nix/var/nix/profiles/default/bin"
has_nix_path <- any(grepl(nix_path, old_path))
if (isFALSE(has_nix_path)) {
Sys.setenv(PATH = paste(old_path, nix_path, sep = ":"))
}
rm(old_path, nix_path)
}
if (isTRUE(is_nixr)) {
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths)
new_paths <- current_paths[-user_dir]
.libPaths(new_paths)
rm(current_paths, userlib_paths, user_dir, new_paths)
}
rm(is_rstudio, is_nixr)
}
117 changes: 117 additions & 0 deletions .github/workflows/run-pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Inspired by the template in the {targets} package
# https://github.com/ropensci/targets/blob/3b16f37ba5cdca710adbb81c2301f6b1d7432cfb/inst/templates/github_actions.yaml

# This file is licensed under the MIT
# Copyright © 2023 Bruno Rodrigues and Philipp Baumann
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

name: run-pipeline

on:
push:
branches:
- main
- master

jobs:
targets:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:

- uses: actions/checkout@v3

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
with:
logger: pretty
log-directives: nix_installer=trace
backtrace: full

- name: Nix cache
uses: DeterminateSystems/magic-nix-cache-action@main

- name: Build development environment
run: |
nix-build
- name: Check if previous runs exists
id: runs-exist
run: git ls-remote --exit-code --heads origin targets-runs
continue-on-error: true

- name: Checkout previous run
if: steps.runs-exist.outcome == 'success'
uses: actions/checkout@v2
with:
ref: targets-runs
fetch-depth: 1
path: .targets-runs

- name: Restore output files from the previous run
if: steps.runs-exist.outcome == 'success'
run: |
nix-shell default.nix --run "Rscript -e 'for (dest in scan(\".targets-runs/.targets-files\", what = character())) {
source <- file.path(\".targets-runs\", dest)
if (!file.exists(dirname(dest))) dir.create(dirname(dest), recursive = TRUE)
if (file.exists(source)) file.rename(source, dest)
}'"
- name: Run model
run: |
nix-shell default.nix --run "Rscript -e 'targets::tar_make()'"
- name: Identify files that the targets pipeline produced
run: git ls-files -mo --exclude=renv > .targets-files

- name: Create the runs branch if it does not already exist
if: steps.runs-exist.outcome != 'success'
run: git checkout --orphan targets-runs

- name: Put the worktree in the runs branch if the latter already exists
if: steps.runs-exist.outcome == 'success'
run: |
rm -r .git
mv .targets-runs/.git .
rm -r .targets-runs
- name: Upload latest run
run: |
git config --local user.name "GitHub Actions"
git config --local user.email "[email protected]"
rm -r .gitignore .github/workflows
git add --all -- ':!renv'
for file in $(git ls-files -mo --exclude=renv)
do
git add --force $file
done
git commit -am "Run pipeline"
git push origin targets-runs
- name: Prepare failure artifact
if: failure()
run: rm -rf .git .github .targets-files .targets-runs

- name: Post failure artifact
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: .
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.Rproj.user
13 changes: 13 additions & 0 deletions R/main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library(here)
library(readr)

get_data <- function(file) {
read_csv(file)
}

plot_data <- function(data) {
data |>
group_by(Species) |>
ggplot(aes(x = `Sepal.Width`, y = `Sepal.Length`, color = Species)) +
geom_point()
}
17 changes: 17 additions & 0 deletions _rix.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library(rix)

rix(
r_ver = "latest",
r_pkgs = c(
"targets",
"dplyr",
"ggplot2",
"readr",
"here"
),
system_pkgs = NULL,
git_pkgs = NULL,
project_path = ".",
overwrite = TRUE,
print = TRUE
)
17 changes: 17 additions & 0 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library(targets)

source("R/main.R")

tar_option_set(
package = c(
"dplyr",
"ggplot2",
"readr"
)
)

list(
tar_target(file, "data/data.csv", format = "file"),
tar_target(data, get_data(filed)),
tar_target(plot, plot_data(data))
)
6 changes: 6 additions & 0 deletions data/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(readr)
library(here)

data <- data(iris)

write_csv(data, here("data/data.csv"))
Empty file added data/data.csv
Empty file.
35 changes: 35 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file was generated by the {rix} R package v0.7.0 on 2024-05-24
# with following call:
# >rix(r_ver = "https://github.com/NixOS/nixpkgs/archive/3305b2b25e4ae4baee872346eae133cf6f611783.tar.gz",
# > r_pkgs = c("targets",
# > "dplyr",
# > "ggplot2"),
# > system_pkgs = NULL,
# > git_pkgs = NULL,
# > project_path = ".",
# > overwrite = TRUE,
# > print = TRUE)
# It uses nixpkgs' revision 3305b2b25e4ae4baee872346eae133cf6f611783 for reproducibility purposes
# which will install R version latest
# Report any issues to https://github.com/b-rodrigues/rix
let
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/3305b2b25e4ae4baee872346eae133cf6f611783.tar.gz") {};
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages) targets dplyr ggplot2;
};
system_packages = builtins.attrValues {
inherit (pkgs) R glibcLocales nix;
};
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 = [ rpkgs system_packages ];

}
13 changes: 13 additions & 0 deletions demo-nix-targets.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
Encoding: UTF-8

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

0 comments on commit 05771cb

Please sign in to comment.