Skip to content

Commit

Permalink
Use withr's 'with_dir' instead of writing a custom 'with_wd' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Eagles committed Oct 13, 2023
1 parent e7a3d81 commit 222d00a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 98 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Suggests:
knitcitations,
rmarkdown,
sessioninfo,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
VignetteBuilder: knitr
Imports:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export(job_loop)
export(job_report)
export(job_single)
export(partition_info)
export(with_wd)
import(dplyr)
import(glue)
import(purrr)
Expand Down
29 changes: 13 additions & 16 deletions R/array_submit.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@
#' ## Choose a script name
#' job_name <- paste0("array_submit_example_", Sys.Date())
#'
#' ## Create an array job on the temporary directory
#' with_wd(tempdir(), {
#' ## Create an array job script to use for this example
#' job_single(
#' name = job_name,
#' create_shell = TRUE,
#' task_num = 100
#' )
#' ## Create an array job script to use for this example
#' job_single(
#' name = job_name,
#' create_shell = TRUE,
#' task_num = 100
#' )
#'
#' ## Now we can submit the job for a set of task IDs (or omit 'task_ids'
#' ## to automatically grab those same failed task IDs)
#' array_submit(
#' job_bash = paste0(job_name, ".sh"),
#' task_ids = c(1, 6, 8:20, 67),
#' submit = FALSE
#' )
#' })
#' ## Now we can submit the job for a set of task IDs (or omit 'task_ids'
#' ## to automatically grab those same failed task IDs)
#' array_submit(
#' job_bash = paste0(job_name, ".sh"),
#' task_ids = c(1, 6, 8:20, 67),
#' submit = FALSE
#' )
#'
array_submit <- function(job_bash, task_ids = NULL, submit = FALSE, restore = TRUE, verbose = FALSE) {
## Check that the script is in the working directory
Expand Down
28 changes: 0 additions & 28 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
#' Temporarily evaluate an expression in a directory
#'
#' Temporarily evaluate an expression in a directory, then set the directory
#' back to the original.
#'
#' @param dir a directory to perform an expression within.
#' @param expr expression to evaluate.
#'
#' @details See here: http://plantarum.ca/code/setwd-part2/
#' @export
#' @author Tyler Smith, contributed to regionReport by David Robinson
#' https://github.com/dgrtwo
#'
#' @examples
#'
#' ## Create a directory called 'hola' and then check that it exists
#' with_wd(tempdir(), {
#' dir.create("hola", showWarnings = FALSE)
#' file.exists("hola")
#' })
#'
with_wd <- function(dir, expr) {
wd <- getwd()
on.exit(setwd(wd))
setwd(dir)
eval(expr, envir = parent.frame())
}

get_list_indexing <- function(this_list, index) {
if (index == length(this_list)) {
divisor <- 1
Expand Down
29 changes: 13 additions & 16 deletions man/array_submit.Rd

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

33 changes: 0 additions & 33 deletions man/with_wd.Rd

This file was deleted.

4 changes: 3 additions & 1 deletion tests/testthat/test-array_submit.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(withr)

script_name <- "my_array_job"

# Write a basic shell script with 'job_single()'
Expand Down Expand Up @@ -26,7 +28,7 @@ broken_job <- function() {
# then call 'array_submit' with the '...' arguments. Return the contents of
# the original and modified shell script (list(2) of character vectors)
run_test <- function(shell_creation_fun, ...) {
with_wd(
with_dir(
tempdir(),
{
shell_creation_fun()
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-job_loop.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ run_test <- function(delete = TRUE, ...) {
if (delete) unlink(file.path(tempdir(), paste0(job_name, ".sh")))

## Create an array job on the temporary directory
with_wd(tempdir(), {
with_dir(tempdir(), {
## Create an array job script to use for this example
job_loop(
name = job_name,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-job_single.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ run_test <- function(delete = TRUE, ...) {
if (delete) unlink(file.path(tempdir(), paste0(job_name, ".sh")))

## Create an array job on the temporary directory
with_wd(tempdir(), {
with_dir(tempdir(), {
## Create an array job script to use for this example
job_single(
name = job_name,
Expand Down

0 comments on commit 222d00a

Please sign in to comment.