Skip to content

Commit 71fd2f6

Browse files
Remove unlockBinding by using active bindings
1 parent 550cfd6 commit 71fd2f6

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

R/mplusModel.R

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,29 @@ mplusModel_r6 <- R6::R6Class(
3434
pvt_variables = NULL, # names of columns in data to be written to the .dat file
3535
pvt_is_montecarlo = FALSE, # whether this is a monte carlo model (in which case the data section is irrelevant)
3636

37-
# private method to populate fields using the result of readModels
37+
# read-only outputs (backing storage)
38+
pvt_input = NULL,
39+
pvt_warnings = NULL,
40+
pvt_parameters = NULL,
41+
pvt_summaries = NULL,
42+
pvt_savedata = NULL,
43+
3844
populate_output = function(o) {
3945
private$pvt_output_loaded <- TRUE
40-
for (ff in c("parameters", "input", "warnings", "summaries", "savedata")) {
41-
#for (ff in names(o)) {
42-
unlockBinding(ff, self)
43-
self[[ff]] <- o[[ff]]
44-
lockBinding(ff, self)
45-
}
46+
private$pvt_parameters <- o$parameters
47+
private$pvt_input <- o$input
48+
private$pvt_warnings <- o$warnings
49+
private$pvt_summaries <- o$summaries
50+
private$pvt_savedata <- o$savedata
4651
},
4752

48-
# private method to clear all fields after the object is invalidated (e.g., by changing the inp_file or data)
4953
clear_output = function() {
5054
private$pvt_output_loaded <- FALSE
51-
for (ff in c("parameters", "input", "warnings", "summaries", "savedata")) {
52-
#for (ff in names(o)) {
53-
unlockBinding(ff, self)
54-
self[[ff]] <- NULL
55-
lockBinding(ff, self)
56-
}
55+
private$pvt_parameters <- NULL
56+
private$pvt_input <- NULL
57+
private$pvt_warnings <- NULL
58+
private$pvt_summaries <- NULL
59+
private$pvt_savedata <- NULL
5760
},
5861

5962
detect_variables = function() {
@@ -180,26 +183,36 @@ mplusModel_r6 <- R6::R6Class(
180183
private$pvt_is_montecarlo <- any(grepl("^\\s*montecarlo\\s*:", value, ignore.case = TRUE, perl=TRUE))
181184
}
182185
}
186+
},
187+
188+
# ---- read-only output fields (active bindings) ----
189+
#' @field input Parsed Mplus input sections (read-only)
190+
input = function(value) {
191+
if (missing(value)) private$pvt_input
192+
else stop("`input` is read-only.")
193+
},
194+
#' @field warnings Mplus warnings (read-only)
195+
warnings = function(value) {
196+
if (missing(value)) private$pvt_warnings
197+
else stop("`warnings` is read-only.")
198+
},
199+
#' @field parameters Parameter estimates (read-only)
200+
parameters = function(value) {
201+
if (missing(value)) private$pvt_parameters
202+
else stop("`parameters` is read-only.")
203+
},
204+
#' @field summaries Model summaries/statistics (read-only)
205+
summaries = function(value) {
206+
if (missing(value)) private$pvt_summaries
207+
else stop("`summaries` is read-only.")
208+
},
209+
#' @field savedata Data produced by SAVEDATA (read-only)
210+
savedata = function(value) {
211+
if (missing(value)) private$pvt_savedata
212+
else stop("`savedata` is read-only.")
183213
}
184214
),
185215
public=list(
186-
187-
### READ-ONLY FIELDS (set by populate_output)
188-
#' @field input Mplus input syntax parsed into a list by major section
189-
input = NULL,
190-
191-
#' @field warnings Syntax and estimation warnings as a list
192-
warnings = NULL,
193-
194-
#' @field parameters a list containing the parameter estimates for the model
195-
parameters = NULL,
196-
197-
#' @field summaries a list containing the model summary information and statistics
198-
summaries = NULL,
199-
200-
#' @field savedata a list containing the data output by the SAVEDATA command
201-
savedata = NULL,
202-
203216
#' @description generate an mplusModel_r6 object
204217
#' @param syntax a character vector of Mplus input syntax for this model
205218
#' @param data a data.frame to be used for estimating the model
@@ -233,9 +246,7 @@ mplusModel_r6 <- R6::R6Class(
233246
private$pvt_dat_file <- self$input$data$file
234247

235248
# if file cannot be loaded as is because of a relative path problem, look in the model directory
236-
if (!file.exists(dfile)) {
237-
dfile <- file.path(private$pvt_model_dir, dfile)
238-
}
249+
if (!file.exists(dfile)) dfile <- file.path(private$pvt_model_dir, dfile)
239250
data <- tryCatch(data.table::fread(dfile, header = FALSE, na.strings=c("*", "."), strip.white=TRUE, data.table = FALSE),
240251
error=function(e) { warning("Could not load data file: ", dfile); return(NULL) })
241252

man/mplusModel_r6.Rd

Lines changed: 10 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)