diff --git a/.github/workflows/style-and-lint.yaml b/.github/workflows/style-and-lint.yaml index c3485dc0..e07073c4 100644 --- a/.github/workflows/style-and-lint.yaml +++ b/.github/workflows/style-and-lint.yaml @@ -2,7 +2,7 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: pull_request: - branches: [main, master] + branches: [not-main, not-master] name: style-and-lint diff --git a/.github/workflows/style-with-air.yaml b/.github/workflows/style-with-air.yaml new file mode 100644 index 00000000..b27bd086 --- /dev/null +++ b/.github/workflows/style-with-air.yaml @@ -0,0 +1,53 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + pull_request: + branches: [main, master] + +name: style-with-air + +permissions: write-all + +jobs: + style_pkg: + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ github.token }} + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/pr-fetch@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@main + + - uses: cachix/cachix-action@v15 + with: + name: rstats-on-nix + + - name: Build dev env + run: nix-build + + - name: Run Air + run: nix-shell -I nixpkgs='https://github.com/rstats-on-nix/nixpkgs/archive/2025-02-28.tar.gz' -p air-formatter --run "air format ." + + - name: config bot user and check for changes + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git diff-index --quiet HEAD || echo "has_changes=true" >> $GITHUB_ENV + + - name: commit if changes + if: env.has_changes == 'true' + run: | + git add \*.R + git add \*.Rmd + git commit -m 'Style via air' + + - uses: r-lib/actions/pr-push@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/R/available_r.R b/R/available_r.R index 99a4e39b..325fb899 100644 --- a/R/available_r.R +++ b/R/available_r.R @@ -22,8 +22,13 @@ available_df <- function() { available_r <- function() { r_versions <- unique(available_df()$`R.version`) c( - "bleeding-edge", "frozen-edge", "r-devel", "bioc-devel", - "r-devel-bioc-devel", "latest-upstream", r_versions + "bleeding-edge", + "frozen-edge", + "r-devel", + "bioc-devel", + "r-devel-bioc-devel", + "latest-upstream", + r_versions ) } diff --git a/R/ga_cachix.R b/R/ga_cachix.R index 8ed785f8..b72045c3 100644 --- a/R/ga_cachix.R +++ b/R/ga_cachix.R @@ -48,7 +48,6 @@ ga_cachix <- function(cache_name, path_default) { message("GitHub Actions workflow file saved to: ", path) } - # The sed command for Darwin is of the form "sed -i '' s/foo/bar" # while on Linux it's "sed -i s/foo/bar" darwin_specific_quotes <- if (Sys.info()["sysname"] == "Darwin") { @@ -59,15 +58,23 @@ ga_cachix <- function(cache_name, path_default) { system( paste0( - "sed -i ", darwin_specific_quotes, "'s/CACHE_NAME/", cache_name, "/g' ", + "sed -i ", + darwin_specific_quotes, + "'s/CACHE_NAME/", + cache_name, + "/g' ", paste0(path, "/cachix_dev_env.yaml") ) ) system( paste0( - "sed -i ", darwin_specific_quotes, "'s/PATH_TO_DEFAULT_NIX/", - path_default, "/g' ", file.path(path, "cachix_dev_env.yaml") + "sed -i ", + darwin_specific_quotes, + "'s/PATH_TO_DEFAULT_NIX/", + path_default, + "/g' ", + file.path(path, "cachix_dev_env.yaml") ) ) diff --git a/R/get_latest.R b/R/get_latest.R index 8b3f0e8e..caacbd2a 100644 --- a/R/get_latest.R +++ b/R/get_latest.R @@ -17,14 +17,18 @@ get_latest <- function(r_version) { if (nchar(r_version) == 40) { return(r_version) } else if ( - !(r_version %in% c( - "bioc-devel", - "r-devel-bioc-devel", - "r-devel", - "frozen-edge", - "bleeding-edge", - "latest-upstream" - )) && all(r_version > Filter(function(x) ("latest-upstream" != x), available_r())) + !(r_version %in% + c( + "bioc-devel", + "r-devel-bioc-devel", + "r-devel", + "frozen-edge", + "bleeding-edge", + "latest-upstream" + )) && + all( + r_version > Filter(function(x) ("latest-upstream" != x), available_r()) + ) ) { stop( "The provided R version is too recent,\nand not yet included in `nixpkgs`.\n", @@ -36,10 +40,15 @@ get_latest <- function(r_version) { "'bleeding-edge' and 'frozen-edge'." ) } else if ( - !(r_version %in% c( - "r-devel-bioc-devel", "r-devel", "bioc-devel", - "bleeding-edge", "frozen-edge", available_r() - )) + !(r_version %in% + c( + "r-devel-bioc-devel", + "r-devel", + "bioc-devel", + "bleeding-edge", + "frozen-edge", + available_r() + )) ) { stop( "The provided R version is too recent,\nand not yet included in `nixpkgs`.\n", @@ -74,7 +83,8 @@ get_right_commit <- function(r_version) { api_url <- "https://api.github.com/repos/rstats-on-nix/nixpkgs/commits?sha=r-daily" } else if ( r_version %in% Filter(function(x) `!=`(x, "latest-upstream"), available_r()) - ) { # all but latest-upstream + ) { + # all but latest-upstream # If a user provides an R version, use most recent date for that version return(get_date_from_version(r_version)) } else { @@ -101,17 +111,18 @@ get_right_commit <- function(r_version) { #' Fetch if available and stop with propagating the curl error. Also show URL #' for context #' @noRd -try_get_request <- function(url, - handle, - extra_diagnostics = NULL) { +try_get_request <- function(url, handle, extra_diagnostics = NULL) { tryCatch( { req <- curl::curl_fetch_memory(url, handle) }, error = function(e) { - stop("Request `curl::curl_fetch_memory(", - paste0("url = ", "'", url, "'", ")` "), "failed:\n ", - e$message[1], extra_diagnostics, + stop( + "Request `curl::curl_fetch_memory(", + paste0("url = ", "'", url, "'", ")` "), + "failed:\n ", + e$message[1], + extra_diagnostics, call. = FALSE ) } diff --git a/R/make_launcher.R b/R/make_launcher.R index 66dcc7c5..9124d3a3 100644 --- a/R/make_launcher.R +++ b/R/make_launcher.R @@ -22,14 +22,19 @@ make_launcher <- function(editor, project_path) { editor <- trimws(editor) - stopifnot("'editor' argument must be a single word" = grepl("^[A-Za-z]+$", editor)) + stopifnot( + "'editor' argument must be a single word" = grepl("^[A-Za-z]+$", editor) + ) if (isFALSE(dir.exists(project_path))) { dir.create(path = project_path, recursive = TRUE) project_path <- normalizePath(path = project_path) } - script_text <- sprintf("#!/usr/bin/env nix-shell\n#!nix-shell default.nix -i bash\n%s", editor) + script_text <- sprintf( + "#!/usr/bin/env nix-shell\n#!nix-shell default.nix -i bash\n%s", + editor + ) script_path <- file.path(project_path, paste0("start-", editor, ".sh")) writeLines(script_text, script_path) diff --git a/R/make_nixpkgs_url.R b/R/make_nixpkgs_url.R index 8b529a42..edbdf249 100644 --- a/R/make_nixpkgs_url.R +++ b/R/make_nixpkgs_url.R @@ -19,7 +19,11 @@ make_nixpkgs_url <- function(r_ver = NULL, date = NULL) { list( "url" = paste0( - "https://github.com/", github_repo, "archive/", latest_commit, ".tar.gz" + "https://github.com/", + github_repo, + "archive/", + latest_commit, + ".tar.gz" ), "latest_commit" = latest_commit, "r_ver" = r_ver @@ -27,7 +31,9 @@ make_nixpkgs_url <- function(r_ver = NULL, date = NULL) { } else { list( "url" = paste0( - "https://github.com/rstats-on-nix/nixpkgs/archive/", date, ".tar.gz" + "https://github.com/rstats-on-nix/nixpkgs/archive/", + date, + ".tar.gz" ), "latest_commit" = date, "r_ver" = get_version_from_date(date) diff --git a/R/nix_build_helpers.R b/R/nix_build_helpers.R index 1226e29a..2eef5c32 100644 --- a/R/nix_build_helpers.R +++ b/R/nix_build_helpers.R @@ -62,12 +62,15 @@ fix_ld_library_path <- function() { #' #' @noRd -poll_sys_proc_blocking <- function(cmd, proc, - what = c("nix-build", "expr", "nix-hash"), - message_type = - c("simple", "quiet", "verbose")) { +poll_sys_proc_blocking <- function( + cmd, + proc, + what = c("nix-build", "expr", "nix-hash"), + message_type = c("simple", "quiet", "verbose") +) { what <- match.arg(what, choices = c("nix-build", "expr", "nix-hash")) - message_type <- match.arg(message_type, + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) is_quiet <- message_type == "quiet" @@ -97,13 +100,15 @@ poll_sys_proc_blocking <- function(cmd, proc, #' with an error code. The waiting is implemented to not create race conditions #' #' @noRd -poll_sys_proc_nonblocking <- function(cmd, - proc, - what = c("nix-build", "expr", "nix-hash"), - message_type = - c("simple", "quiet", "verbose")) { +poll_sys_proc_nonblocking <- function( + cmd, + proc, + what = c("nix-build", "expr", "nix-hash"), + message_type = c("simple", "quiet", "verbose") +) { what <- match.arg(what, choices = c("nix-build", "expr", "nix-hash")) - message_type <- match.arg(message_type, + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) is_quiet <- message_type == "quiet" @@ -119,7 +124,8 @@ poll_sys_proc_nonblocking <- function(cmd, tools::pskill(pid = proc) stop( "`nix_build()` likely interrupted by SIGINT (ctrl+c)\n", - "Stop process with PID ", proc + "Stop process with PID ", + proc ) } @@ -151,7 +157,8 @@ nix_build_installed <- function() { nix_build_exit_msg <- function(x) { x_char <- as.character(x) - err_msg <- switch(x_char, + err_msg <- switch( + x_char, "100" = "generic build failure (100).", "101" = "build timeout (101).", "102" = "hash mismatch (102).", diff --git a/R/renv_helpers.R b/R/renv_helpers.R index 096c0811..4a3f619f 100644 --- a/R/renv_helpers.R +++ b/R/renv_helpers.R @@ -40,7 +40,9 @@ read_renv_lock <- function(renv_lock_path = "renv.lock") { #' renv_remote_pkgs(read_renv_lock()$Packages) #' } renv_remote_pkgs <- function( - renv_lock_remote_pkgs, host = NULL) { + renv_lock_remote_pkgs, + host = NULL +) { # , "bitbucket", "git", "local", "svn", "url", "version", "cran", "bioc" supported_pkg_hosts <- c("api.github.com", "gitlab.com") if (!(is.null(host) || (host %in% supported_pkg_hosts))) { @@ -61,28 +63,38 @@ renv_remote_pkgs <- function( host <- renv_lock_pkg_info$RemoteHost } else { stop( - renv_lock_pkg_info$Package, " has unsupported remote host: ", - renv_lock_pkg_info$RemoteHost, "\nSupported hosts are: ", + renv_lock_pkg_info$Package, + " has unsupported remote host: ", + renv_lock_pkg_info$RemoteHost, + "\nSupported hosts are: ", paste0(supported_pkg_hosts, collapse = ", ") ) } } else { if (host != renv_lock_pkg_info$RemoteHost) { stop( - "Remote host (", renv_lock_pkg_info$RemoteHost, ") of ", renv_lock_pkg_info$Package, - " does not match the provided host (", host, ")" + "Remote host (", + renv_lock_pkg_info$RemoteHost, + ") of ", + renv_lock_pkg_info$Package, + " does not match the provided host (", + host, + ")" ) } } pkg_info <- vector(mode = "list", length = 3) names(pkg_info) <- c("package_name", "repo_url", "commit") - switch(host, + switch( + host, "api.github.com" = { pkg_info[[1]] <- renv_lock_pkg_info$Package pkg_info[[2]] <- paste0( # RemoteHost is listed as api.github.com for some reason - "https://github.com/", renv_lock_pkg_info$RemoteUser, "/", + "https://github.com/", + renv_lock_pkg_info$RemoteUser, + "/", renv_lock_pkg_info$RemoteRepo ) pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha @@ -90,8 +102,11 @@ renv_remote_pkgs <- function( "gitlab.com" = { pkg_info[[1]] <- renv_lock_pkg_info$Package pkg_info[[2]] <- paste0( - "https://", renv_lock_pkg_info$RemoteHost, "/", - renv_lock_pkg_info$RemoteUser, "/", + "https://", + renv_lock_pkg_info$RemoteHost, + "/", + renv_lock_pkg_info$RemoteUser, + "/", renv_lock_pkg_info$RemoteRepo ) pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha @@ -159,12 +174,13 @@ renv_remote_pkgs <- function( #' } #' renv2nix <- function( - renv_lock_path = "renv.lock", - project_path, - return_rix_call = FALSE, - method = c("fast", "accurate"), - override_r_ver = NULL, - ...) { + renv_lock_path = "renv.lock", + project_path, + return_rix_call = FALSE, + method = c("fast", "accurate"), + override_r_ver = NULL, + ... +) { method <- match.arg(method, c("fast", "accurate")) renv_lock <- read_renv_lock(renv_lock_path = renv_lock_path) if (method == "fast") { @@ -175,13 +191,18 @@ renv2nix <- function( for (i in seq_along(renv_lock$Packages)) { if (renv_lock$Packages[[i]]$Source %in% c("Repository", "Bioconductor")) { repo_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]] - } else if (renv_lock$Packages[[i]]$RemoteHost %in% c("api.github.com", "gitlab.com")) { + } else if ( + renv_lock$Packages[[i]]$RemoteHost %in% + c("api.github.com", "gitlab.com") + ) { remote_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]] } else { # unsupported_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]] warning( - renv_lock$Packages[[i]]$Package, " has the unsupported remote host ", - renv_lock$Packages[[i]]$RemoteHost, " and will not be included in the Nix expression.", + renv_lock$Packages[[i]]$Package, + " has the unsupported remote host ", + renv_lock$Packages[[i]]$RemoteHost, + " and will not be included in the Nix expression.", "\n Consider manually specifying the git remote or a local package install." ) } diff --git a/R/rix_init.R b/R/rix_init.R index 6616abe5..1798193e 100644 --- a/R/rix_init.R +++ b/R/rix_init.R @@ -93,23 +93,31 @@ #' message_type = c("simple") #' ) #' } -rix_init <- function(project_path, - rprofile_action = c( - "create_missing", "create_backup", - "overwrite", "append" - ), - message_type = c("simple", "quiet", "verbose")) { - message_type <- match.arg(message_type, +rix_init <- function( + project_path, + rprofile_action = c( + "create_missing", + "create_backup", + "overwrite", + "append" + ), + message_type = c("simple", "quiet", "verbose") +) { + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) is_quiet <- message_type == "quiet" - rprofile_action <- match.arg(rprofile_action, + rprofile_action <- match.arg( + rprofile_action, choices = c("create_missing", "create_backup", "overwrite", "append") ) stopifnot( - "`project_path` needs to be character of length 1" = - is.character(project_path) && length(project_path) == 1L + "`project_path` needs to be character of length 1" = is.character( + project_path + ) && + length(project_path) == 1L ) if (isFALSE(is_quiet) && identical(Sys.getenv("TESTTHAT"), "false")) { @@ -128,7 +136,8 @@ rix_init <- function(project_path, project_path <- normalizePath(path = project_path) if (isFALSE(is_quiet) && identical(Sys.getenv("TESTTHAT"), "false")) { cat( - "==> Existing isolated nix-R project folder:\n", project_path, + "==> Existing isolated nix-R project folder:\n", + project_path, "\n" ) } @@ -175,17 +184,23 @@ rix_init <- function(project_path, timestamp <- format(Sys.time(), "%Y-%m-%dT%H:%M:%S%z") rprofile_backup <- paste0(rprofile_file, "_backup_", timestamp) - switch(rprofile_action, + switch( + rprofile_action, create_missing = { if (isTRUE(rprofile_exists)) { if (isFALSE(is_quiet)) { cat( "\n* Keep existing `.Rprofile`. in `project_path`:\n", - paste0(project_path, "/"), "\n" + paste0(project_path, "/"), + "\n" ) } } else { - write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "wb") + write_rprofile( + rprofile_text, + rprofile_file = rprofile_file, + mode = "wb" + ) if (isFALSE(is_quiet)) { message_rprofile(action_string = "Added", project_path = project_path) } @@ -195,10 +210,15 @@ rix_init <- function(project_path, create_backup = { if (isTRUE(rprofile_exists)) { file.copy(from = rprofile_file, to = rprofile_backup) - write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "wb") + write_rprofile( + rprofile_text, + rprofile_file = rprofile_file, + mode = "wb" + ) if (isFALSE(is_quiet) && identical(Sys.getenv("TESTTHAT"), "false")) { cat( - "\n==> Backed up existing `.Rprofile` in file:\n", rprofile_backup, + "\n==> Backed up existing `.Rprofile` in file:\n", + rprofile_backup, "\n" ) message_rprofile( @@ -207,7 +227,10 @@ rix_init <- function(project_path, ) } - if (message_type == "verbose" && identical(Sys.getenv("TESTTHAT"), "false")) { + if ( + message_type == "verbose" && + identical(Sys.getenv("TESTTHAT"), "false") + ) { cat("\n* Current lines of local `.Rprofile` are\n:") cat(readLines(con = rprofile_file), sep = "\n") } @@ -225,18 +248,21 @@ rix_init <- function(project_path, write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "wb") if (isTRUE(rprofile_exists)) { message_rprofile( - action_string = "Overwrote", project_path = project_path + action_string = "Overwrote", + project_path = project_path ) } else { message_rprofile( - action_string = "Added", project_path = project_path + action_string = "Added", + project_path = project_path ) } }, append = { write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "a+") message_rprofile( - action_string = "Appended", project_path = project_path + action_string = "Appended", + project_path = project_path ) } ) @@ -273,10 +299,10 @@ get_rprofile_text <- function(rprofile_deparsed) { #' @param action_string string #' @param project_path string with project path #' @noRd -message_rprofile <- function(action_string = "Added", - project_path = ".") { +message_rprofile <- function(action_string = "Added", project_path = ".") { msg <- paste0( - "\n==> ", action_string, + "\n==> ", + action_string, " `.Rprofile` file and code lines for new R sessions launched from:\n", project_path, "\n\n* Added the location of the Nix store to `PATH` ", @@ -284,7 +310,7 @@ message_rprofile <- function(action_string = "Added", "/nix/var/nix/profiles/default/bin" ) - if(identical(Sys.getenv("TESTTHAT"), "false")){ + if (identical(Sys.getenv("TESTTHAT"), "false")) { cat(msg) } } @@ -299,9 +325,11 @@ message_rprofile <- function(action_string = "Added", #' are separated by `":"`. #' @noRd # nolint start: object_name_linter -set_message_session_PATH <- function(message_type = - c("simple", "quiet", "verbose")) { - message_type <- match.arg(message_type, +set_message_session_PATH <- function( + message_type = c("simple", "quiet", "verbose") +) { + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) if (message_type == "verbose" && identical(Sys.getenv("TESTTHAT"), "false")) { @@ -322,7 +350,6 @@ set_message_session_PATH <- function(message_type = } # nolint end: object_name_linter - #' Report whether the current R session is running in Nix and RStudio, or not. #' @param is_nix_r logical scalar. `TRUE` means in a Nix R environment #' @param is_rstudio `TRUE` means source R session is inside RStudio @@ -331,17 +358,19 @@ set_message_session_PATH <- function(message_type = #' create identical messages, while `"quiet"` omits diagnostics messages #' @return NULL #' @noRd -message_r_session_nix_rstudio <- function(is_nix_r, - is_rstudio, - message_type = - c("simple", "quiet", "verbose")) { +message_r_session_nix_rstudio <- function( + is_nix_r, + is_rstudio, + message_type = c("simple", "quiet", "verbose") +) { stopifnot( - "`is_nix_r` needs to be TRUE or FALSE" = - is.logical(is_nix_r) && length(is_nix_r) == 1L, - "`is_rstudio` needs to be TRUE or FALSE" = - is.logical(is_rstudio) && length(is_rstudio) == 1L + "`is_nix_r` needs to be TRUE or FALSE" = is.logical(is_nix_r) && + length(is_nix_r) == 1L, + "`is_rstudio` needs to be TRUE or FALSE" = is.logical(is_rstudio) && + length(is_rstudio) == 1L ) - message_type <- match.arg(message_type, + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) @@ -362,10 +391,7 @@ message_r_session_nix_rstudio <- function(is_nix_r, # derive compound message msg <- paste0(nix_r_msg, " and ", rstudio_msg) - switch(message_type, - simple = cat(msg), - verbose = cat(msg) - ) + switch(message_type, simple = cat(msg), verbose = cat(msg)) } @@ -437,7 +463,8 @@ nix_rprofile <- function() { if (isFALSE(has_nix_path)) { Sys.setenv( PATH = paste( - old_path, nix_path, + old_path, + nix_path, sep = ":" ) ) @@ -486,12 +513,25 @@ nix_rprofile <- function() { rm(current_paths, userlib_paths, user_dir, new_paths) } # source vscode-R init.R file for vscode-R - if (isTRUE(is_code) && interactive() && isFALSE(is_rstudio) && isFALSE(is_positron)) { - vscode_r_init <- file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R") + if ( + isTRUE(is_code) && + interactive() && + isFALSE(is_rstudio) && + isFALSE(is_positron) + ) { + vscode_r_init <- file.path( + Sys.getenv( + if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME" + ), + ".vscode-R", + "init.R" + ) if (file.exists(vscode_r_init)) { source(vscode_r_init) } else { - message("No .vscode-R/init.R file found. If you want to use VSCode-R, you need to source it in your .Rprofile or start vscode from within nix-shell") + message( + "No .vscode-R/init.R file found. If you want to use VSCode-R, you need to source it in your .Rprofile or start vscode from within nix-shell" + ) } } rm(is_rstudio, is_nix_r, is_code, is_positron) diff --git a/R/setup_cachix.R b/R/setup_cachix.R index 019d9520..ce00d22d 100644 --- a/R/setup_cachix.R +++ b/R/setup_cachix.R @@ -27,9 +27,11 @@ setup_cachix <- function(nix_conf_path = "~/.config/nix") { nix_conf_file <- file.path(nix_conf_path, "nix.conf") if (!nix_build_installed()) { - stop("Nix is not installed in this system, + stop( + "Nix is not installed in this system, there is no need to configure the cache. -Install Nix first.") +Install Nix first." + ) } if (nix_conf_exists(nix_conf_file)) { diff --git a/R/tar_nix_ga.R b/R/tar_nix_ga.R index 0e8f4c0c..06c3cf65 100644 --- a/R/tar_nix_ga.R +++ b/R/tar_nix_ga.R @@ -41,5 +41,6 @@ tar_nix_ga <- function() { message("GitHub Actions workflow file saved to: ", path) } - if (identical(Sys.getenv("TESTTHAT"), "true")) paste0(path, "/run-pipeline.yaml") + if (identical(Sys.getenv("TESTTHAT"), "true")) + paste0(path, "/run-pipeline.yaml") } diff --git a/R/with_nix.R b/R/with_nix.R index 5195af0c..a8100adb 100644 --- a/R/with_nix.R +++ b/R/with_nix.R @@ -129,25 +129,34 @@ #' ## session, which will be exported to the nix-R session. #' ## Other option: running system commands through `nix-shell` environment. #' } -with_nix <- function(expr, - program = c("R", "shell"), - project_path = ".", - message_type = c("simple", "quiet", "verbose")) { +with_nix <- function( + expr, + program = c("R", "shell"), + project_path = ".", + message_type = c("simple", "quiet", "verbose") +) { nix_file <- file.path(project_path, "default.nix") # nolint start: line_length_linter stopifnot( - "`project_path` must be character of length 1." = - is.character(project_path) && length(project_path) == 1L, - "`project_path` has no `default.nix` file. Use one that contains `default.nix`" = - file.exists(nix_file), + "`project_path` must be character of length 1." = is.character( + project_path + ) && + length(project_path) == 1L, + "`project_path` has no `default.nix` file. Use one that contains `default.nix`" = file.exists( + nix_file + ), "`message_type` must be character." = is.character(message_type), - "`expr` needs to be a call or function for `program = R`, and character of length 1 for `program = shell`" = - is.function(expr) || is.call(expr) || (is.character(expr) && length(expr) == 1L) + "`expr` needs to be a call or function for `program = R`, and character of length 1 for `program = shell`" = is.function( + expr + ) || + is.call(expr) || + (is.character(expr) && length(expr) == 1L) ) # nolint end program <- match.arg(program, choices = c("R", "shell")) - message_type <- match.arg(message_type, + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) @@ -219,7 +228,8 @@ with_nix <- function(expr, if (isFALSE(is_quiet)) { cat( "\n* using environment defined by Nix expression in file:\n", - normalizePath(file.path(project_path, "default.nix")), "\n" + normalizePath(file.path(project_path, "default.nix")), + "\n" ) } @@ -265,9 +275,14 @@ with_nix <- function(expr, rnix_file <- file.path(temp_dir, "with_nix_r.R") rnix_quoted <- quote_rnix( - expr, program, + expr, + program, message_type = message_type, - args_vec, globals, pkgs, temp_dir, rnix_file + args_vec, + globals, + pkgs, + temp_dir, + rnix_file ) rnix_deparsed <- deparse_chr1(expr = rnix_quoted, collapse = "\n") @@ -294,10 +309,13 @@ with_nix <- function(expr, proc <- sys::exec_background(cmd = "nix-shell", cmd_rnix_deparsed) poll_sys_proc_nonblocking( - cmd = cmd_rnix_deparsed, proc, what = "expr", + cmd = cmd_rnix_deparsed, + proc, + what = "expr", message_type ) - } else if (program == "shell") { # end of `if (program == "R")` + } else if (program == "shell") { + # end of `if (program == "R")` shell_cmd <- c( file.path(project_path, "default.nix"), "--pure", @@ -313,7 +331,10 @@ with_nix <- function(expr, out <- readRDS(file = file.path(temp_dir, "_out.Rds")) } else if (program == "shell") { status <- poll_sys_proc_blocking( - cmd = shell_cmd, proc, what = "expr", message_type + cmd = shell_cmd, + proc, + what = "expr", + message_type ) out <- sys::as_text(proc$stdout) } diff --git a/R/with_nix_helpers.R b/R/with_nix_helpers.R index 37c4e18f..2b96eb5d 100644 --- a/R/with_nix_helpers.R +++ b/R/with_nix_helpers.R @@ -117,11 +117,13 @@ where <- function(name, env = parent.frame()) { #' @param expr an **R** expression #' @param args_vec character vector with arguments #' @noRd -recurse_find_check_globals <- function(expr, - args_vec, - message_type = - c("simple", "quiet", "verbose")) { - message_type <- match.arg(message_type, +recurse_find_check_globals <- function( + expr, + args_vec, + message_type = c("simple", "quiet", "verbose") +) { + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) is_quiet <- message_type == "quiet" @@ -282,9 +284,10 @@ classify_globals <- function(globals_expr, args_vec) { globs_empty <- NULL } globs_other <- vec_envs_check[ - !names(vec_envs_check) %in% names( - c(globs_pkg, globs_globalenv, globs_empty, globs_base) - ) + !names(vec_envs_check) %in% + names( + c(globs_pkg, globs_globalenv, globs_empty, globs_base) + ) ] if (length(globs_other) == 0L) { globs_other <- NULL @@ -292,12 +295,14 @@ classify_globals <- function(globals_expr, args_vec) { } is_globalenv_funs <- vapply( - names(globs_globalenv), function(x) is.function(get(x)), + names(globs_globalenv), + function(x) is.function(get(x)), FUN.VALUE = logical(1L) ) is_otherenv_funs <- vapply( - names(globs_other), function(x) is.function(get(x)), + names(globs_other), + function(x) is.function(get(x)), FUN.VALUE = logical(1L) ) @@ -355,10 +360,13 @@ classify_globals <- function(globals_expr, args_vec) { #' (default), `"quiet"` for no messaging, or `"verbose"` to report which object #' categories are saved under which `.Rds` file and path. #' @noRd -serialize_globals <- function(globals_expr, - temp_dir, - message_type = c("simple", "quiet", "verbose")) { - message_type <- match.arg(message_type, +serialize_globals <- function( + globals_expr, + temp_dir, + message_type = c("simple", "quiet", "verbose") +) { + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) funs <- globals_expr$globalenv_fun @@ -367,7 +375,9 @@ serialize_globals <- function(globals_expr, cat( "==> serializing global functions under `.Rds` in temporary folder at", - paste0(normalizePath(temp_dir), "...\n"), paste(names(funs)), "\n" + paste0(normalizePath(temp_dir), "...\n"), + paste(names(funs)), + "\n" ) } globalenv_funs <- lapply( @@ -382,7 +392,8 @@ serialize_globals <- function(globals_expr, if (message_type == "verbose") { cat( "==> serializing non-function object(s), e.g., other environments", - paste(names(others)), " ...\n" + paste(names(others)), + " ...\n" ) } globalenv_others <- lapply( @@ -396,7 +407,8 @@ serialize_globals <- function(globals_expr, if (!is.null(env_funs)) { cat( "==> Serializing function(s) from other environment(s):", - paste(names(env_funs)), "\n" + paste(names(env_funs)), + "\n" ) env_funs <- lapply( names(env_funs), @@ -409,7 +421,8 @@ serialize_globals <- function(globals_expr, if (!is.null(env_others)) { cat( "==> Serializing non-function object(s) from custom environment(s)::", - paste(names(env_others)), "\n" + paste(names(env_others)), + "\n" ) env_others <- lapply( names(env_others), @@ -436,10 +449,13 @@ serialize_globals <- function(globals_expr, #' `"quiet"`, or `"verbose"`. #' @return character vector with name of R packages. #' @noRd -serialize_pkgs <- function(globals_expr, - temp_dir, - message_type = c("simple", "verbose", "quiet")) { - message_type <- match.arg(message_type, +serialize_pkgs <- function( + globals_expr, + temp_dir, + message_type = c("simple", "verbose", "quiet") +) { + message_type <- match.arg( + message_type, choices = c("simple", "quiet", "verbose") ) is_quiet <- message_type == "quiet" @@ -447,7 +463,8 @@ serialize_pkgs <- function(globals_expr, if (!is.null(pkgs) && isFALSE(is_quiet)) { cat( "=> Serializing package(s) required to run `expr`:\n", - paste(pkgs), "\n" + paste(pkgs), + "\n" ) } saveRDS( @@ -476,14 +493,16 @@ serialize_pkgs <- function(globals_expr, #' `Rscript` in `nix-shell` #' @return A language object #' @noRd -quote_rnix <- function(expr, - program, - message_type, - args_vec, - globals, - pkgs, - temp_dir, - rnix_file) { +quote_rnix <- function( + expr, + program, + message_type, + args_vec, + globals, + pkgs, + temp_dir, + rnix_file +) { expr_quoted <- bquote({ message_type <- .(message_type) is_quiet <- message_type == "quiet" @@ -506,7 +525,11 @@ quote_rnix <- function(expr, .libPaths(new_paths) r_version_num <- paste0(R.version$major, ".", R.version$minor) if (isFALSE(is_quiet)) { - cat("\n* evaluating `expr` in Nix shell with R version", r_version_num, "\n\n") + cat( + "\n* evaluating `expr` in Nix shell with R version", + r_version_num, + "\n\n" + ) } # assign `args_vec` as in c(...) form. args_vec <- .(with_assign_vecnames_call(vec = args_vec)) @@ -521,8 +544,14 @@ quote_rnix <- function(expr, if (message_type == "verbose") { cat( paste0( - " ==> reading file ", "'", obj, ".Rds", "'", - " for argument named `", obj, "` ...\n" + " ==> reading file ", + "'", + obj, + ".Rds", + "'", + " for argument named `", + obj, + "` ...\n" ) ) } @@ -539,8 +568,14 @@ quote_rnix <- function(expr, if (message_type == "verbose") { cat( paste0( - " ==> reading file ", "'", obj, ".Rds", "'", - " for global object named `", obj, "`\n" + " ==> reading file ", + "'", + obj, + ".Rds", + "'", + " for global object named `", + obj, + "`\n" ) ) } diff --git a/create_dev_env.R b/create_dev_env.R index f1b066ef..53435b3e 100644 --- a/create_dev_env.R +++ b/create_dev_env.R @@ -3,9 +3,19 @@ library(rix) rix( date = "2025-01-14", r_pkgs = c( - "devtools", "diffviewer", "fledge", "lintr", "styler", - "codetools", "jsonlite", "httr", "sys", "testthat", "knitr", - "rmarkdown", "rhub" + "devtools", + "diffviewer", + "fledge", + "lintr", + "styler", + "codetools", + "jsonlite", + "httr", + "sys", + "testthat", + "knitr", + "rmarkdown", + "rhub" ), system_pkgs = c("R", "glibcLocalesUtf8", "pandoc", "nix"), tex_pkgs = c("inconsolata"), diff --git a/data-raw/DATASET.R b/data-raw/DATASET.R index 78f79c0e..9e82000b 100644 --- a/data-raw/DATASET.R +++ b/data-raw/DATASET.R @@ -31,7 +31,6 @@ library(rix) # commit that contains all the fixes that were merged during this release. This # ensures that a maximum of packages are in a working state. - # data("r_nix_revs") # This was added already, so it's commented now diff --git a/tests/testthat/test-available_r.R b/tests/testthat/test-available_r.R index 2da9c55f..c5dba5da 100644 --- a/tests/testthat/test-available_r.R +++ b/tests/testthat/test-available_r.R @@ -2,14 +2,39 @@ testthat::test_that("available_r lists all available r versions", { testthat::expect_equal( available_r(), c( - "bleeding-edge", "frozen-edge", "r-devel", "bioc-devel", - "r-devel-bioc-devel", "latest-upstream", - "3.5.3", "3.6.0", "3.6.1", "3.6.2", "3.6.3", - "4.0.0", "4.0.1", "4.0.2", "4.0.3", "4.0.4", "4.0.5", - "4.1.0", "4.1.1", "4.1.2", "4.1.3", - "4.2.0", "4.2.1", "4.2.2", "4.2.3", - "4.3.0", "4.3.1", "4.3.2", "4.3.3", - "4.4.0", "4.4.1", "4.4.2", "4.4.3" + "bleeding-edge", + "frozen-edge", + "r-devel", + "bioc-devel", + "r-devel-bioc-devel", + "latest-upstream", + "3.5.3", + "3.6.0", + "3.6.1", + "3.6.2", + "3.6.3", + "4.0.0", + "4.0.1", + "4.0.2", + "4.0.3", + "4.0.4", + "4.0.5", + "4.1.0", + "4.1.1", + "4.1.2", + "4.1.3", + "4.2.0", + "4.2.1", + "4.2.2", + "4.2.3", + "4.3.0", + "4.3.1", + "4.3.2", + "4.3.3", + "4.4.0", + "4.4.1", + "4.4.2", + "4.4.3" ) ) }) diff --git a/tests/testthat/test-rix_init.R b/tests/testthat/test-rix_init.R index a7631b8c..4d0c9971 100644 --- a/tests/testthat/test-rix_init.R +++ b/tests/testthat/test-rix_init.R @@ -105,7 +105,8 @@ testthat::test_that("Snapshot test of rix_init(), append", { rprofile_file <- paste0(path_env_nix, "/.Rprofile") rprofile_con <- file(rprofile_file, open = "a+", encoding = "native.enc") - writeLines(enc2utf8("This is in the original Rprofile"), + writeLines( + enc2utf8("This is in the original Rprofile"), rprofile_con, useBytes = TRUE ) diff --git a/tests/testthat/test-setup_cachix.R b/tests/testthat/test-setup_cachix.R index 7fe34274..6be98561 100644 --- a/tests/testthat/test-setup_cachix.R +++ b/tests/testthat/test-setup_cachix.R @@ -1,21 +1,25 @@ testthat::test_that("setup_cachix(): error if already configured", { skip_if_not(nix_shell_available()) testthat::expect_error( - setup_cachix(nix_conf_path = "testdata/nix_conf_samples/nix_already/"), "already configured" + setup_cachix(nix_conf_path = "testdata/nix_conf_samples/nix_already/"), + "already configured" ) }) testthat::test_that("setup_cachix(): error if no nix", { skip_if_not(!nix_shell_available()) testthat::expect_error( - setup_cachix(nix_conf_path = "testdata/nix_conf_samples/nix_already/"), "no need" + setup_cachix(nix_conf_path = "testdata/nix_conf_samples/nix_already/"), + "no need" ) }) testthat::test_that("setup_cachix(): configure if not already", { skip_if_not(nix_shell_available()) testthat::expect_snapshot_file( - path = setup_cachix(nix_conf_path = "testdata/nix_conf_samples/nix_not_yet/"), + path = setup_cachix( + nix_conf_path = "testdata/nix_conf_samples/nix_not_yet/" + ), name = "nix.conf" ) diff --git a/tests/testthat/test-with_nix.R b/tests/testthat/test-with_nix.R index 2439fd8c..118e85a3 100644 --- a/tests/testthat/test-with_nix.R +++ b/tests/testthat/test-with_nix.R @@ -10,7 +10,6 @@ testthat::test_that("Testing `with_nix()` if Nix is installed", { # R version 3.5.3 on Nixpkgs does not build for aarch64-darwin (ARM macOS) skip_if(Sys.info()["sysname"] == "Darwin") - path_subshell <- tempdir() # Suppress the warning related to generating an expression @@ -83,7 +82,6 @@ testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", { inherits(out_subshell, "data.frame") ) - on.exit( unlink(path_subshell, recursive = TRUE, force = TRUE), add = TRUE