-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bruno Rodrigues
committed
Jul 15, 2023
1 parent
9ecfd7a
commit f80ed72
Showing
11 changed files
with
405 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export("%>%") | ||
export(available_r) | ||
export(find_rev) | ||
export(my_fun) | ||
export(rix) | ||
importFrom(magrittr,"%>%") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# WARNING - Generated by {fusen} from dev/build_envs.Rmd: do not edit by hand | ||
|
||
#' find_rev Find the right Nix revision | ||
#' @param r_version Character. R version to look for, for example, "4.2.0" | ||
#' @return A character. The Nix revision to use | ||
#' @export | ||
#' | ||
#' @examples | ||
#' find_rev("4.2.0") | ||
find_rev <- function(r_version){ | ||
|
||
temp <- new.env(parent = emptyenv()) | ||
|
||
data(list = "r_nix_revs", | ||
package = "rix", | ||
envir = temp) | ||
|
||
get("r_nix_revs", envir = temp) | ||
|
||
output <- r_nix_revs$revision[r_nix_revs$version == r_version] | ||
|
||
stopifnot("Error: the provided R version is likely wrong. Please check that you provided a correct R version. You can list available versions using `available_r()`" = !identical(character(0), output)) | ||
|
||
output | ||
} | ||
|
||
|
||
#' available_r List available R versions | ||
#' @return A character vector containing the available R versrions. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' available_r() | ||
available_r <- function(){ | ||
|
||
temp <- new.env(parent = emptyenv()) | ||
|
||
data(list = "r_nix_revs", | ||
package = "rix", | ||
envir = temp) | ||
|
||
get("r_nix_revs", envir = temp) | ||
|
||
r_nix_revs$version | ||
|
||
} | ||
|
||
|
||
#' rix Build a reproducible development environment definition | ||
#' @return Nothing, this function only has the side-effect of writing a file | ||
#' called "default.nix" in the working directory. This file contains the | ||
#' instructions to build a reproducible environment using the Nix package | ||
#' manager. | ||
#' @param r_ver Character. The required R version. Leave this argument empty if | ||
#' you want to use the latest R version available. You can check which R | ||
#' versions are available using `available_r` | ||
#' @param pkgs Vector of characters. List the required packages for your | ||
#' analysis here. | ||
#' @param rstudio Logical, defaults to FALSE. If TRUE, RStudio gets installed in | ||
#' this environment to enable interactive work. | ||
#' @param path Character. Where to write `default.nix`. | ||
#' @details This function will write a `default.nix` in the chosen path. Using | ||
#' the Nix package manager, it is then possible to build a reproducible | ||
#' development environment using the `nix-build` command in the path. This | ||
#' environment will contain the chosen version of R and packages, and will not | ||
#' interfere with any other installed version (via Nix or not) on your | ||
#' machine. Every dependency, including both R package dependencies but also | ||
#' system dependencies like compilers will get installed as well in that | ||
#' environment. If you use RStudio for interactive work, then set the | ||
#' `rstudio` parameter to `TRUE`. If you use another IDE (for example Emacs or | ||
#' Visual Studio Code), you do not need to add it to the `default.nix` file, | ||
#' you can simply use the version that is installed on your computer. To use | ||
#' Visual Studio Code however, you need to install the `{languageserver}` | ||
#' package, so don't forget to add it to the list of packages. Once you built | ||
#' the environment using `nix-build`, you can drop into an interactive session | ||
#' using `nix-shell`. See the "How-to Nix" vignette for more details. | ||
#' @export | ||
rix <- function(r_ver, pkgs, rstudio = FALSE, path = "."){ | ||
nixTemplate <- " | ||
{ pkgs ? import (fetchTarball 'https://github.com/NixOS/nixpkgs/archive/RE_VERSION.tar.gz') {} }: | ||
with pkgs; | ||
let | ||
my-r = rWrapper.override { | ||
packages = with rPackages; [PACKAGE_LIST]; | ||
}; | ||
USE_RSTUDIO my-rstudio = rstudioWrapper.override { | ||
USE_RSTUDIO packages = with rPackages; [PACKAGE_LIST]; | ||
USE_RSTUDIO}; | ||
in | ||
mkShell { | ||
buildInputs = [ | ||
my-r | ||
USE_RSTUDIO my-rstudio | ||
]; | ||
}" | ||
|
||
nixFile <- gsub('RE_VERSION', find_rev(r_ver) , nixTemplate) | ||
packages <- paste(pkgs, collapse = ' ') | ||
nixFile <- gsub('PACKAGE_LIST', packages, nixFile) | ||
nixFile <- gsub('USE_RSTUDIO', ifelse(rstudio, '', '#'), nixFile) | ||
|
||
writeLines(nixFile, normalizePath(paste0(path, "/default.nix"))) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
--- | ||
title: "RIX: functions" | ||
output: html_document | ||
editor_options: | ||
chunk_output_type: console | ||
--- | ||
|
||
```{r development, include=FALSE} | ||
library(testthat) | ||
``` | ||
|
||
<!-- | ||
You need to run the 'description' chunk in the '0-dev_history.Rmd' file before continuing your code there. | ||
--> | ||
|
||
```{r development-load} | ||
# Load already included functions if relevant | ||
pkgload::load_all(export_all = FALSE) | ||
``` | ||
|
||
This function takes an R version as an input and returns the Nix revision that provides it: | ||
|
||
```{r function-find_rev} | ||
#' find_rev Find the right Nix revision | ||
#' @param r_version Character. R version to look for, for example, "4.2.0" | ||
#' @return A character. The Nix revision to use | ||
#' @export | ||
#' | ||
#' @examples | ||
#' find_rev("4.2.0") | ||
find_rev <- function(r_version){ | ||
temp <- new.env(parent = emptyenv()) | ||
data(list = "r_nix_revs", | ||
package = "rix", | ||
envir = temp) | ||
get("r_nix_revs", envir = temp) | ||
output <- r_nix_revs$revision[r_nix_revs$version == r_version] | ||
stopifnot("Error: the provided R version is likely wrong. Please check that you provided a correct R version. You can list available versions using `available_r()`" = !identical(character(0), output)) | ||
output | ||
} | ||
``` | ||
|
||
```{r tests-find_rev} | ||
testthat::expect_equal( | ||
find_rev("4.2.2"), | ||
"8ad5e8132c5dcf977e308e7bf5517cc6cc0bf7d8" | ||
) | ||
``` | ||
|
||
This function return available R versions: | ||
|
||
```{r function-available_r} | ||
#' available_r List available R versions | ||
#' @return A character vector containing the available R versrions. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' available_r() | ||
available_r <- function(){ | ||
temp <- new.env(parent = emptyenv()) | ||
data(list = "r_nix_revs", | ||
package = "rix", | ||
envir = temp) | ||
get("r_nix_revs", envir = temp) | ||
r_nix_revs$version | ||
} | ||
``` | ||
|
||
This next function returns a `default.nix` file that can be used to build a | ||
reproducible environment. This function takes an R version as an input (the | ||
correct Nix revision is found using `find_rev()`), a list of R packages, and | ||
whether the user wants to work with RStudio or not in that environment. If | ||
you use another IDE, you can leave the "rstudio" argument blank: | ||
|
||
```{r function-rix} | ||
#' rix Build a reproducible development environment definition | ||
#' @return Nothing, this function only has the side-effect of writing a file | ||
#' called "default.nix" in the working directory. This file contains the | ||
#' instructions to build a reproducible environment using the Nix package | ||
#' manager. | ||
#' @param r_ver Character. The required R version. Leave this argument empty if | ||
#' you want to use the latest R version available. You can check which R | ||
#' versions are available using `available_r` | ||
#' @param pkgs Vector of characters. List the required packages for your | ||
#' analysis here. | ||
#' @param rstudio Logical, defaults to FALSE. If TRUE, RStudio gets installed in | ||
#' this environment to enable interactive work. | ||
#' @param path Character. Where to write `default.nix`. | ||
#' @details This function will write a `default.nix` in the chosen path. Using | ||
#' the Nix package manager, it is then possible to build a reproducible | ||
#' development environment using the `nix-build` command in the path. This | ||
#' environment will contain the chosen version of R and packages, and will not | ||
#' interfere with any other installed version (via Nix or not) on your | ||
#' machine. Every dependency, including both R package dependencies but also | ||
#' system dependencies like compilers will get installed as well in that | ||
#' environment. If you use RStudio for interactive work, then set the | ||
#' `rstudio` parameter to `TRUE`. If you use another IDE (for example Emacs or | ||
#' Visual Studio Code), you do not need to add it to the `default.nix` file, | ||
#' you can simply use the version that is installed on your computer. To use | ||
#' Visual Studio Code however, you need to install the `{languageserver}` | ||
#' package, so don't forget to add it to the list of packages. Once you built | ||
#' the environment using `nix-build`, you can drop into an interactive session | ||
#' using `nix-shell`. See the "How-to Nix" vignette for more details. | ||
#' @export | ||
rix <- function(r_ver, pkgs, rstudio = FALSE, path = "."){ | ||
nixTemplate <- " | ||
{ pkgs ? import (fetchTarball 'https://github.com/NixOS/nixpkgs/archive/RE_VERSION.tar.gz') {} }: | ||
with pkgs; | ||
let | ||
my-r = rWrapper.override { | ||
packages = with rPackages; [PACKAGE_LIST]; | ||
}; | ||
USE_RSTUDIO my-rstudio = rstudioWrapper.override { | ||
USE_RSTUDIO packages = with rPackages; [PACKAGE_LIST]; | ||
USE_RSTUDIO}; | ||
in | ||
mkShell { | ||
buildInputs = [ | ||
my-r | ||
USE_RSTUDIO my-rstudio | ||
]; | ||
}" | ||
nixFile <- gsub('RE_VERSION', find_rev(r_ver) , nixTemplate) | ||
packages <- paste(pkgs, collapse = ' ') | ||
nixFile <- gsub('PACKAGE_LIST', packages, nixFile) | ||
nixFile <- gsub('USE_RSTUDIO', ifelse(rstudio, '', '#'), nixFile) | ||
writeLines(nixFile, normalizePath(paste0(path, "/default.nix"))) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.