Skip to content

Commit

Permalink
Adds ephemeral files
Browse files Browse the repository at this point in the history
  • Loading branch information
eliocamp committed Oct 8, 2024
1 parent 249f5c4 commit 47a9570
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Added options management. `cdo_options_use()` will use options for an operation. `cdo_option_set()` will set global options to use for all operations. `cdo_options_clear()` will clear the global options.
- The new `cdo_operator()` allows the user to define an arbitrary operator in case the some operator is missing in the package. Operators are run with the `cdo()` function.
- Added operators with zero outputs.
- By default, temporay files output are deleted when not accessible from the R session.
22 changes: 20 additions & 2 deletions R/cdo.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ get_output_length <- function(x) {
#'
#' @param operation a CDO operation
#' @param output an output file or base string for output files. Defaults to
#' temporary files.
#' temporary files that will be deleted when its bond variable is garbage collected.
#' @param options character vector with CDO options.
#' @param verbose whether to print the command being executed.
#'
Expand Down Expand Up @@ -186,14 +186,32 @@ cdo_execute <- function(operation,
return(operation$output)
}


ephemeral_files <- R6::R6Class("ephemeral_files", public = list(
files = NA,
initialize = function(files) {
self$files <- files
return(self)
},
print = function() {
cat("Will be deleted when garbage collected\n")
},

finalize = function() {
file.remove(self$files)
})
)

temp_output <- function(operation) {
if (operation$operator$n_output == Inf) {
n <- 1
} else {
n <- operation$operator$n_output
}

replicate(n, tempfile())
files <- replicate(n, tempfile())
attr(files, "ephemeral") <- ephemeral_files$new(files)
files
}


Expand Down
22 changes: 20 additions & 2 deletions pkg_build/extra-R/cdo.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ get_output_length <- function(x) {
#'
#' @param operation a CDO operation
#' @param output an output file or base string for output files. Defaults to
#' temporary files.
#' temporary files that will be deleted when its bond variable is garbage collected.
#' @param options character vector with CDO options.
#' @param verbose whether to print the command being executed.
#'
Expand Down Expand Up @@ -186,14 +186,32 @@ cdo_execute <- function(operation,
return(operation$output)
}


ephemeral_files <- R6::R6Class("ephemeral_files", public = list(
files = NA,
initialize = function(files) {
self$files <- files
return(self)
},
print = function() {
cat("Will be deleted when garbage collected\n")
},

finalize = function() {
file.remove(self$files)
})
)

temp_output <- function(operation) {
if (operation$operator$n_output == Inf) {
n <- 1
} else {
n <- operation$operator$n_output
}

replicate(n, tempfile())
files <- replicate(n, tempfile())
attr(files, "ephemeral") <- ephemeral_files$new(files)
files
}


Expand Down

0 comments on commit 47a9570

Please sign in to comment.