A family of small, fast-failing validators used throughout ecokit (and
packages depending on it) to enforce argument types and formats —
particularly for HPC/SLURM job configuration (mod_slurm()) and parallel
processing setup (set_parallel()).
Usage
is_integer(x)
validate_n_cores(x)
validate_strategy(x)
validate_slurm_runtime(x, warning = TRUE)
validate_slurm_ram(x)Arguments
- x
Input value
For
is_integer()andvalidate_n_cores(), a numeric value to check.For
validate_strategy(), a character string naming afuturestrategy.For
validate_slurm_runtime()andvalidate_slurm_ram(), a character string with a SLURM time or memory specification.
- warning
Logical, used only by
validate_slurm_runtime(). IfTRUE(default), an invalid runtime string raises an error via stop_ctx. IfFALSE, an invalid runtime string instead raises a base::warning and the function returnsFALSE.
Value
is_integer(): a single logical (TRUE/FALSE) for numeric input; errors ifxisNULL.validate_n_cores(): a single integer, clamped toparallelly::availableCores()ifxexceeds it (with a warning).validate_strategy(),validate_slurm_ram(): the unchanged inputxif valid; otherwise an error.validate_slurm_runtime(): the unchanged inputxif valid; otherwise an error (warning = TRUE, default) orFALSEwith a warning (warning = FALSE).
Details
is_integer(): checks whetherxis a single positive whole-number value (e.g.5,5L,5.0). ReturnsFALSEfor non-scalars,NA, zero, negative, non-integer, or non-numeric values (without erroring), but throws an error ifxisNULL.validate_n_cores(): validates a requested core countx. Must be a positive integer scalar (checked viais_integer(); otherwise errors). Ifxexceedsparallelly::availableCores(), a warning is issued and the number of available cores is used instead.validate_strategy(): validates afutureparallelisation strategy namex. Must be one of"sequential","multisession","multicore", or"cluster".validate_slurm_runtime(): validates a SLURM--timestringx. Accepted formats follow SLURM conventions:"minutes","minutes:seconds","hours:minutes:seconds","days-hours","days-hours:minutes", or"days-hours:minutes:seconds", with hours00-23and minutes/seconds00-59.validate_slurm_ram(): validates a SLURM--memstringx. Must be a positive integer immediately followed by an uppercase unit:K,M,G,T, or their long formsKB,MB,GB,TB.
Examples
# is_integer
is_integer(5)
#> [1] FALSE
is_integer(5L)
#> [1] TRUE
is_integer(5.5)
#> [1] FALSE
is_integer(-1)
#> [1] FALSE
is_integer(NA)
#> [1] FALSE
is_integer(c(1, 2))
#> [1] FALSE
is_integer("5")
#> [1] FALSE
if (FALSE) { # \dontrun{
is_integer(NULL) # Errors: `x` cannot be NULL
} # }
# validate_n_cores
validate_n_cores(2)
#> [1] 2
# Warns and is clamped to the number of available cores
validate_n_cores(1000L)
#> Warning: `x` (1000) exceeds available cores. Using all available cores (4)
#> [1] 4
if (FALSE) { # \dontrun{
# Errors: not a positive integer scalar
validate_n_cores(-1)
validate_n_cores("2")
} # }
# validate_strategy
validate_strategy("multisession")
#> [1] "multisession"
if (FALSE) { # \dontrun{
validate_strategy("parallel") # Errors: not a valid `future` strategy
} # }
# validate_slurm_runtime ----
validate_slurm_runtime("30")
#> [1] "30"
validate_slurm_runtime("2:00:00")
#> [1] "2:00:00"
validate_slurm_runtime("0-12:00:00")
#> [1] "0-12:00:00"
validate_slurm_runtime("5-23:59")
#> [1] "5-23:59"
validate_slurm_runtime("2:00")
#> [1] "2:00"
if (FALSE) { # \dontrun{
validate_slurm_runtime("2:70:00") # Errors: invalid minutes
validate_slurm_runtime("12-24:00:00") # Errors: invalid hours
validate_slurm_runtime("abc") # Errors: not a SLURM time format
} # }
# Returns FALSE with a warning instead of erroring
validate_slurm_runtime("abc", warning = FALSE)
#> Warning: Invalid SLURM runtime format: `abc`
#> [1] FALSE
# validate_slurm_ram ----
validate_slurm_ram("8G")
#> [1] "8G"
validate_slurm_ram("1024M")
#> [1] "1024M"
validate_slurm_ram("32GB")
#> [1] "32GB"
validate_slurm_ram("1T")
#> [1] "1T"
if (FALSE) { # \dontrun{
validate_slurm_ram("500") # Errors: missing unit
validate_slurm_ram("500m") # Errors: lowercase unit not accepted
validate_slurm_ram("4.5G") # Errors: non-integer amount
validate_slurm_ram("100MBs") # Errors: trailing characters
validate_slurm_ram("GB32") # Errors: unit before amount
} # }
