Skip to content

Commit b814f09

Browse files
Merge pull request #226 from michaelhallquist/codex/fix-data-file-loading-for-mplusmodel
Handle missing data paths for mplusModel
2 parents ade99b6 + 1462e90 commit b814f09

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

R/mplusModel.R

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,40 @@ mplusModel_r6 <- R6::R6Class(
273273
if (is.null(data) && private$pvt_output_loaded && !is.null(self$input$data$file)) {
274274
dfile <- self$input$data$file
275275
private$pvt_dat_file <- self$input$data$file
276-
277-
# if file cannot be loaded as is because of a relative path problem, look in the model directory
278-
if (!file.exists(dfile)) dfile <- file.path(private$pvt_model_dir, dfile)
279-
data <- tryCatch(data.table::fread(dfile, header = FALSE, na.strings=c("*", "."), strip.white=TRUE, data.table = FALSE),
280-
error=function(e) { warning("Could not load data file: ", dfile); return(NULL) })
281-
276+
277+
# If the data file cannot be loaded as-is, attempt to locate it in the
278+
# directory of the input/output files. This handles cases where the
279+
# data file was specified with an absolute path on another machine but
280+
# the .dat, .inp and .out files all reside in the same folder.
281+
if (!file.exists(dfile)) {
282+
# first, try using the provided path relative to the model directory
283+
rel_dfile <- file.path(private$pvt_model_dir, dfile)
284+
if (file.exists(rel_dfile)) {
285+
dfile <- rel_dfile
286+
} else {
287+
# next, try just the basename of the data file in the model directory
288+
base_dfile <- file.path(private$pvt_model_dir, basename(dfile))
289+
if (file.exists(base_dfile)) {
290+
dfile <- base_dfile
291+
private$pvt_dat_file <- basename(dfile)
292+
}
293+
}
294+
}
295+
296+
data <- tryCatch(
297+
data.table::fread(
298+
dfile,
299+
header = FALSE,
300+
na.strings = c("*", "."),
301+
strip.white = TRUE,
302+
data.table = FALSE
303+
),
304+
error = function(e) {
305+
warning("Could not load data file: ", dfile)
306+
return(NULL)
307+
}
308+
)
309+
282310
# set the names of the data if read succeeds
283311
if (!is.null(data)) names(data) <- strsplit(expandCmd(self$input$variable$names), "\\s+")[[1]]
284312
}

tests/testthat/test-mplusModel.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ test_that("mplusModel reads existing output", {
5353
expect_equal(nrow(m$data), 500)
5454
})
5555

56+
test_that("mplusModel falls back to basename for missing data file", {
57+
tmp <- tempdir()
58+
file.copy(testthat::test_path("submitModels", "ex3.1.dat"), tmp, overwrite = TRUE)
59+
file.copy(testthat::test_path("submitModels", "ex3.1.inp"), tmp, overwrite = TRUE)
60+
file.copy(testthat::test_path("ex3.1.out"), tmp, overwrite = TRUE)
61+
62+
bad_path <- normalizePath(file.path(tempdir(), "nonexistent", "path", "ex3.1.dat"),
63+
winslash = "/", mustWork = FALSE)
64+
inp_lines <- readLines(file.path(tmp, "ex3.1.inp"))
65+
inp_lines <- gsub("ex3.1.dat", bad_path, inp_lines, fixed = TRUE)
66+
writeLines(inp_lines, file.path(tmp, "ex3.1.inp"))
67+
out_lines <- readLines(file.path(tmp, "ex3.1.out"))
68+
out_lines <- gsub("ex3.1.dat", bad_path, out_lines, fixed = TRUE)
69+
writeLines(out_lines, file.path(tmp, "ex3.1.out"))
70+
71+
mplus_fake <- tempfile(); file.create(mplus_fake)
72+
m <- mplusModel(inp_file = file.path(tmp, "ex3.1.inp"), read = TRUE, Mplus_command = mplus_fake)
73+
expect_equal(nrow(m$data), 500)
74+
expect_equal(m$dat_file, file.path(tmp, "ex3.1.dat"))
75+
})
76+
5677
test_that("mplusModel exposes readModels sections", {
5778
tmp <- tempdir()
5879
file.copy(testthat::test_path("submitModels","ex3.1.inp"), tmp, overwrite = TRUE)

0 commit comments

Comments
 (0)