Skip to content

Commit 516857b

Browse files
Implement change detection in run method
1 parent 86f7e2d commit 516857b

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

R/mplusModel.R

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,18 +392,47 @@ mplusModel_r6 <- R6::R6Class(
392392
submitModels(target = self$inp_file, replaceOutfile = replaceOutfile, Mplus_command = self$Mplus_command, ...)
393393
},
394394

395-
#' @description run this model locally using `runModels`
395+
#' @description run this model locally using `runModels`. The method detects
396+
#' changes in the data and input syntax and only rewrites the corresponding
397+
#' files when updates are detected.
396398
#' @param replaceOutfile Currently supports three settings: “always”, which runs all models, regardless of whether an output file for the model exists;
397399
#' “never”, which does not run any model that has an existing output file; and “modifiedDate”, which only runs a model if the modified date for the input
398400
#' file is more recent than the output file modified date (implying there have been updates to the model).
399401
#' @param ... additional arguments passed to `runModels`
400402
run = function(replaceOutfile = "modifiedDate", ...) {
401-
# TODO: only write inp and dat files if things have changed compared to disk
402-
self$write_dat()
403-
self$write_inp()
403+
# Only write input and data files when the current contents differ from those on disk
404+
405+
# check whether data have changed compared to existing .dat file
406+
if (!private$pvt_is_montecarlo && !is.null(self$data)) {
407+
write_dat <- TRUE
408+
cols <- private$pvt_variables
409+
if (is.null(cols)) cols <- names(self$data)
410+
if (file.exists(self$dat_file)) {
411+
new_hash <- digest::digest(self$data[, cols, drop = FALSE], algo = "md5")
412+
existing <- tryCatch(
413+
data.table::fread(self$dat_file, col.names = cols, data.table = FALSE),
414+
error = function(e) NULL
415+
)
416+
if (!is.null(existing)) {
417+
ext_hash <- digest::digest(existing, algo = "md5")
418+
if (isTRUE(new_hash == ext_hash)) write_dat <- FALSE
419+
}
420+
}
421+
if (write_dat) self$write_dat(quiet = TRUE)
422+
}
423+
424+
# check whether the input file has changed
425+
write_inp <- TRUE
426+
if (file.exists(self$inp_file)) {
427+
new_md5 <- digest::digest(self$syntax, algo = "md5", serialize = FALSE)
428+
ext_md5 <- digest::digest(readLines(self$inp_file), algo = "md5", serialize = FALSE)
429+
if (isTRUE(new_md5 == ext_md5)) write_inp <- FALSE
430+
}
431+
if (write_inp) self$write_inp(quiet = TRUE)
432+
404433
runModels(target = self$inp_file, replaceOutfile = replaceOutfile, Mplus_command = self$Mplus_command, ...)
405434
self$read(force = TRUE) # read/re-read after estimation
406-
435+
407436
}
408437
)
409438
)

0 commit comments

Comments
 (0)