Skip to content

Commit

Permalink
styler
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkenwg committed Sep 18, 2024
1 parent 7ec4605 commit 600f53b
Show file tree
Hide file tree
Showing 24 changed files with 480 additions and 324 deletions.
2 changes: 1 addition & 1 deletion R/cleanup.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ batch_cleanup <- function(path, force = FALSE,
)
names(clean) <- files
nfailed <- sum(!clean)
if(nfailed > 0 && !silent) {
if (nfailed > 0 && !silent) {
wrn("Clean up of %n file%s failed", n = nfailed)
}
invisible(clean)
Expand Down
2 changes: 1 addition & 1 deletion R/completed.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @return A flag specifying whether batch processing is complete.
#' @seealso [batch_process()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down
8 changes: 5 additions & 3 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' that will be processed when [batch_run()] is called.
#' @seealso [batch_process()] and [batch_run()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down Expand Up @@ -65,7 +65,9 @@ batch_config <- function(fun, path, regexp = ".*", recurse = FALSE, ...) {
}
dots <- list(...)
cleanup_log_file(path)
save_config(path, regexp, recurse, fun = fun, dots = dots, time =
sys_time_utc() + 1e-05) # 1e-05 required to ensure time check
save_config(path, regexp, recurse,
fun = fun, dots = dots, time =
sys_time_utc() + 1e-05
) # 1e-05 required to ensure time check
invisible(files)
}
2 changes: 1 addition & 1 deletion R/files.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @return A character vector of the names of the remaining files.
#' @seealso [batch_process()] and [batch_run()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down
19 changes: 9 additions & 10 deletions R/internal.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
report_file <- function(x) {

time <- x$time
if(!is.na(time)) {
if (!is.na(time)) {
time <- time_to_character(time)
time <- paste0(" [", time, "]")
}

switch(x$type,
SUCCESS = cli_alert_success(c(col_white(x$file), col_blue(time))),
FAILURE = cli_alert_danger(c(col_white(x$file), col_blue(time))),
REMAING = cli_alert_warning(col_white(x$file)))
REMAING = cli_alert_warning(col_white(x$file))

Check warning on line 11 in R/internal.R

View check run for this annotation

Codecov / codecov/patch

R/internal.R#L11

Added line #L11 was not covered by tests
)
}

report_files <- function(status) {
Expand All @@ -27,8 +27,8 @@ report_types <- function(status) {

cli_par()
cli_text(col_white("Success: "), col_green(freq[1]))
cli_text(col_white("Failure: "), if(freq[2] == 0) col_green(freq[2]) else col_red(freq[2]))
cli_text(col_white("Remaining: "), if(freq[3] == 0) col_green(freq[3]) else col_red(freq[3]))
cli_text(col_white("Failure: "), if (freq[2] == 0) col_green(freq[2]) else col_red(freq[2]))
cli_text(col_white("Remaining: "), if (freq[3] == 0) col_green(freq[3]) else col_red(freq[3]))
}

save_config <- function(path, regexp, recurse, fun, dots, time) {
Expand All @@ -50,8 +50,8 @@ read_lines_log <- function(path) {

no_log_data <- function() {
tibble(
type = character(0),
time = hms::as_hms(integer(0)),
type = character(0),
time = hms::as_hms(integer(0)),
file = character(0),
message = character(0)
)
Expand Down Expand Up @@ -138,7 +138,6 @@ time_to_character <- function(time) {
}

process_file <- function(file, fun, dots, path, config_time) {

validate_remaining_file(path, file, config_time)

dots <- c(file.path(path, file), dots)
Expand Down Expand Up @@ -171,11 +170,11 @@ process_file <- function(file, fun, dots, path, config_time) {

process_files <- function(remaining, fun, dots, path, config_time,
progress, options) {

success <- future_map(remaining, process_file,
fun = fun, dots = dots,
path = path, config_time = config_time,
.progress = progress, .options = options)
.progress = progress, .options = options
)

success <- unlist(success)
invisible(set_names(success, remaining))
Expand Down
2 changes: 1 addition & 1 deletion R/is-clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @return A flag specifying whether the directory is clean.
#' @seealso [batch_cleanup()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' batch_is_clean(path)
#' write.csv(mtcars, file.path(path, "file1.csv"))
Expand Down
6 changes: 3 additions & 3 deletions R/process.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#' @inheritParams batch_config
#' @inheritParams batch_run
#' @inheritParams batch_cleanup
#' @param report A flag specifying whether to outputs a report of
#' @param report A flag specifying whether to outputs a report of
#' the status of individual files to the console.
#'
#' @seealso [batch_config()], [batch_run()]
#' and [batch_cleanup()]
#' @return An invisible flag indicating whether all the files where
#' successfully processed.
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_process(function(x) TRUE, path, regexp = "[.]csv$", ask = FALSE)
Expand All @@ -33,7 +33,7 @@ batch_process <- function(fun, path, regexp = ".*", recurse = FALSE,
path = path, progress = progress,
seeds = seeds, options = options, ask = ask
)
if(report) batch_report(path)
if (report) batch_report(path)
batch_cleanup(path, force = force)
invisible(all(success))
}
4 changes: 2 additions & 2 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @return A named list of the configuration values.
#' @seealso [batch_process()] and [batch_log_read()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down Expand Up @@ -37,7 +37,7 @@ batch_config_read <- function(path) {
#' }
#' @seealso [batch_process()] and [batch_config_read()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down
2 changes: 1 addition & 1 deletion R/reconfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ batch_reconfig_fun <- function(path, fun, ...) {
#' remaining to be processed.
#' @seealso [batch_process()] and [batch_config()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$")
Expand Down
6 changes: 3 additions & 3 deletions R/report.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#' Outputs a report of the status of individual files to the console.
#'
#' @inheritParams batch_config
#' @return An invisible NULL. The function is called for its side-effect of
#' @return An invisible NULL. The function is called for its side-effect of
#' outputting a report of the status of individual files to the console.
#' @seealso [batch_file_status()]
#' @export
#' @examples
#' @examples
#' path <- tempdir()
#' write.csv(mtcars, file.path(path, "file1.csv"))
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$",)
#' batch_config(function(x) TRUE, path, regexp = "[.]csv$", )
#' batch_report(path)
#' batch_run(path, ask = FALSE)
#' batch_report(path)
Expand Down
4 changes: 2 additions & 2 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ batch_run <- function(path,
chk_not_any_na(files)
chk_unique(files)
}
if(!is.null(seeds)) {
if (!is.null(seeds)) {
chk_list(seeds)
chk_named(seeds)
chk_unique(names(seeds))
Expand Down Expand Up @@ -94,7 +94,7 @@ batch_run <- function(path,
return(invisible(.named_logical0))
}

if(is.null(seeds)) {
if (is.null(seeds)) {
seeds <- batch_seeds(remaining)
} else {
chk_superset(names(seeds), remaining)
Expand Down
10 changes: 5 additions & 5 deletions R/seeds.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rinteger <- function(n = 1L) {
chk_whole_number(n)
chk_gte(n, 0L)
if(n == 0) integer(0)
if (n == 0) integer(0)
mx <- 2147483647L
as.integer(runif(n, -mx, mx))
}
Expand All @@ -13,8 +13,8 @@ get_random_seed <- function() {
set_random_seed <- function(seed, advance = FALSE) {
env <- globalenv()
env$.Random.seed <- seed
if(advance) {
fun <- if(is.null(seed)) suppressWarnings else identity
if (advance) {
fun <- if (is.null(seed)) suppressWarnings else identity
fun(runif(1))
}
invisible(env$.Random.seed)
Expand Down Expand Up @@ -42,10 +42,10 @@ get_lecyer_cmrg_seed <- function() {
# inspired by furrr:::generate_seed_streams
batch_seeds <- function(files = batch_files_remaining()) {
chk_s3_class(files, "character")

oseed <- get_random_seed()
on.exit(set_random_seed(oseed, advance = TRUE))

seed <- get_lecyer_cmrg_seed()
seeds <- vector("list", length = length(files))
for (i in seq_len(length(files))) {
Expand Down
6 changes: 4 additions & 2 deletions R/status.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ batch_file_status <- function(path) {
log <- log[!duplicated(log$file, fromLast = TRUE), ]
remaining <- batch_files_remaining(path, failed = FALSE)
nremaing <- length(remaining)
nremaing <- tibble(type = rep("REMAING", nremaing),
nremaing <- tibble(
type = rep("REMAING", nremaing),
time = hms(rep(NA, nremaing)),
file = remaining,
message = rep(NA_character_, nremaing))
message = rep(NA_character_, nremaing)
)
log <- rbind(log, nremaing)
log <- log[order(log$file), ]
log
Expand Down
38 changes: 21 additions & 17 deletions tests/testthat/test-cleanup.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ test_that("batch_cleanup", {
path <- withr::local_tempdir()

write.csv(data.frame(x = 1), file.path(path, "file1.csv"))

expect_identical(list.files(path), "file1.csv")

expect_identical(
batch_config(function(x) TRUE,
path = path,
Expand All @@ -27,7 +27,7 @@ test_that("batch_cleanup with all failed", {
write.csv(data.frame(x = 1), file.path(path, "file1.csv"))

expect_identical(list.files(path), "file1.csv")

expect_identical(
batch_config(function(x) FALSE,
path = path,
Expand All @@ -37,8 +37,10 @@ test_that("batch_cleanup with all failed", {
)
expect_lte(file_time(path, "file1.csv"), batch_config_read(path)$time)
expect_identical(batch_run(path, ask = FALSE), c(file1.csv = FALSE))
expect_warning(expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$")
expect_warning(
expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$"
)
expect_identical(batch_cleanup(path, force = TRUE), c(. = TRUE))
expect_identical(list.files(path, pattern = "^file\\d[.]csv$"), "file1.csv")
expect_identical(
Expand Down Expand Up @@ -69,8 +71,10 @@ test_that("batch_cleanup force remaining", {
)
expect_lte(file_time(path, "file1.csv"), batch_config_read(path)$time)
expect_identical(batch_run(path, ask = FALSE), c(file1.csv = FALSE))
expect_warning(expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$")
expect_warning(
expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$"
)
expect_identical(batch_cleanup(path, force = TRUE, remaining = TRUE), c(. = TRUE))
expect_identical(list.files(path, pattern = "^file\\d[.]csv$"), character(0))

Expand All @@ -92,34 +96,34 @@ test_that("batch_cleanup with nested configuration files", {
path <- withr::local_tempdir()
sub <- withr::local_tempdir(tmpdir = path)
sub_sub <- withr::local_tempdir(tmpdir = sub)

write.csv(data.frame(x = 1), file.path(path, "file1.csv"))
write.csv(data.frame(x = 1), file.path(sub, "file1.csv"))
write.csv(data.frame(x = 1), file.path(sub_sub, "file1.csv"))

expect_identical(
batch_config(function(x) TRUE,
path = path,
regexp = "^file\\d[.]csv$"
path = path,
regexp = "^file\\d[.]csv$"
),
"file1.csv"
)
expect_identical(
batch_config(function(x) TRUE,
path = sub,
regexp = "^file\\d[.]csv$"
path = sub,
regexp = "^file\\d[.]csv$"
),
"file1.csv"
)
expect_identical(
batch_config(function(x) TRUE,
path = sub_sub,
regexp = "^file\\d[.]csv$"
path = sub_sub,
regexp = "^file\\d[.]csv$"
),
"file1.csv"
)


expect_identical(batch_run(sub, ask = FALSE), c(file1.csv = TRUE))
expect_identical(batch_run(path, ask = FALSE), c(file1.csv = TRUE))
expect_identical(batch_run(sub_sub, ask = FALSE), c(file1.csv = TRUE))
Expand All @@ -129,4 +133,4 @@ test_that("batch_cleanup with nested configuration files", {
expect_identical(batch_cleanup(sub), structure(logical(0), .Names = character(0)))
expect_identical(batch_cleanup(path), structure(logical(0), .Names = character(0)))
expect_identical(batch_cleanup(sub_sub), structure(logical(0), .Names = character(0)))
})
})
2 changes: 1 addition & 1 deletion tests/testthat/test-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("batch_config returns matching files", {

test_that("batch_config with no files", {
path <- withr::local_tempdir()

expect_error(
batch_config(function(x) TRUE, path = path, regexp = "^file\\d[.]csv$"),
"^Directory '.*' does not contain any files matching '.*'[.]$"
Expand Down
14 changes: 8 additions & 6 deletions tests/testthat/test-demo.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ test_that("demo", {
batch_run(path, ask = FALSE),
c(file2.txt = TRUE, file3.txt = FALSE, file4.txt = FALSE)
)

log <- batch_log_read(path)
log <- log[order(log$file),]
log <- log[order(log$file), ]

expect_identical(log$type, c("SUCCESS", "FAILURE", "FAILURE"))
expect_identical(log$message, c(NA, NA, "Uh, Houston, we've had a problem."))

expect_identical(
readLines(file.path(path, "file.txt")),
"the contents of file.txt"
Expand Down Expand Up @@ -74,8 +74,10 @@ test_that("demo", {
expect_identical(batch_run(path, ask = FALSE), c(file.txt = TRUE))
expect_identical(batch_run(path, ask = FALSE), c(x = TRUE)[-1])

expect_warning(expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$")
expect_warning(
expect_identical(batch_cleanup(path), c(. = FALSE)),
"^Clean up of 1 file failed[.]$"
)
expect_identical(batch_run(path, ask = FALSE, failed = NA), c(file3.txt = TRUE, file4.txt = TRUE))
expect_identical(batch_cleanup(path), c("." = TRUE))
})
Loading

0 comments on commit 600f53b

Please sign in to comment.