Skip to content

Commit a8eb91c

Browse files
authored
fix reading options from jasp files (#53)
1 parent 3ed9d24 commit a8eb91c

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

R/options.R

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ analysisOptionsFromJASPfile <- function(file) {
163163
options <- vector("list", length(analyses))
164164
for (i in seq_along(analyses)) {
165165
analysis <- analyses[[i]]
166-
options[[i]] <- analysis[["options"]]
166+
options[[i]] <- fixOptionsForVariableTypes(analysis[["options"]])
167167
attr(options[[i]], "analysisName") <- analysis[["name"]]
168168
}
169169

@@ -173,6 +173,39 @@ analysisOptionsFromJASPfile <- function(file) {
173173
return(options)
174174
}
175175

176+
fixOptionsForVariableTypes <- function(options) {
177+
178+
# jasp does this internally before passing the options to R.
179+
# however, when reading the options from a file this hasn't been done yet
180+
181+
meta <- options[[".meta"]]
182+
if (is.null(meta))
183+
return(options)
184+
185+
nms2fix <- names(vapply(meta, \(x) isTRUE(x[["hasTypes"]]), logical(1L)))
186+
187+
subOptionNeedsFixing <- function(subOption) {
188+
# all the checks I could think of
189+
is.list(subOption) &&
190+
all(c("types", "value") %in% names(subOption)) &&
191+
length(subOption[["types"]]) == length(subOption[["value"]]) &&
192+
is.character(subOption[["types"]]) &&
193+
is.character(subOption[["value"]])
194+
}
195+
196+
for (nm in nms2fix) {
197+
if (subOptionNeedsFixing(options[[nm]])) {
198+
options[[paste0(nm, ".types")]] <- options[[nm]][["types"]]
199+
options[[nm]] <- options[[nm]][["value"]]
200+
}
201+
}
202+
203+
return(options)
204+
205+
}
206+
207+
208+
176209
parseDescriptionQmlFromAnalysisName <- function(analysisName) {
177210

178211
modulePath <- getModulePathFromRFunction(analysisName)

0 commit comments

Comments
 (0)