Skip to content

Commit

Permalink
basic functionality is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Jul 15, 2023
1 parent 9ecfd7a commit f80ed72
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 45 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
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,"%>%")
106 changes: 106 additions & 0 deletions R/find_rev.R
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")))
}

6 changes: 6 additions & 0 deletions dev/0-dev_history.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ usethis::use_git()

**From now, you will need to "inflate" your package at least once to be able to use the following commands. Let's go to your flat template, and come back here later if/when needed.**

```{r}
fusen::inflate(flat_file = "dev/build_envs.Rmd",
vignette_name = "dev-build_envs",
overwrite = TRUE)
```

```{r development-inflate, eval=FALSE}
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
Expand Down
147 changes: 147 additions & 0 deletions dev/build_envs.Rmd
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")))
}
```
13 changes: 13 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
build_envs.Rmd:
path: dev/build_envs.Rmd
state: active
R: R/find_rev.R
tests: tests/testthat/test-find_rev.R
vignettes: vignettes/dev-build_envs.Rmd
inflate:
flat_file: dev/build_envs.Rmd
vignette_name: dev-build_envs
open_vignette: true
check: true
document: true
overwrite: 'yes'
data_doc.Rmd:
path: dev/data_doc.Rmd
state: active
Expand Down
45 changes: 0 additions & 45 deletions dev/flat_minimal_package.Rmd

This file was deleted.

17 changes: 17 additions & 0 deletions man/available_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/find_rev.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f80ed72

Please sign in to comment.