Skip to content

Commit 206b9ec

Browse files
committed
allow saving features with spaces of underscores
1 parent 1b5ab69 commit 206b9ec

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

R/commonMachineLearningClassification.R

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,9 @@
167167
if (!ready) {
168168
table$addFootnote(gettextf("Please provide a target variable and at least %i feature variable(s).", if (type == "knn" || type == "neuralnet" || type == "rpart" || type == "svm" || type == "logistic") 1L else 2L))
169169
}
170-
if (options[["saveModel"]]) {
171-
validNames <- (length(grep(" ", decodeColNames(colnames(dataset)))) == 0) && (length(grep("_", decodeColNames(colnames(dataset)))) == 0)
172-
if (options[["savePath"]] != "" && validNames) {
173-
table$addFootnote(gettextf("The trained model is saved as <i>%1$s</i>.", basename(options[["savePath"]])))
174-
} else if (options[["savePath"]] != "" && !validNames) {
175-
table$addFootnote(gettext("The trained model is <b>not</b> saved because the some of the variable names in the model contain spaces (i.e., ' ') or underscores (i.e., '_'). Please remove all such characters from the variable names and try saving the model again."))
176-
} else {
177-
table$addFootnote(gettext("The trained model is not saved until a file name is specified under 'Save as'."))
178-
}
179-
}
170+
171+
.mlAddSaveModelInfo(table, options)
172+
180173
jaspResults[["classificationTable"]] <- table
181174
if (!ready) {
182175
return()
@@ -330,26 +323,11 @@
330323
)
331324
table$addRows(row)
332325
}
333-
# Save the applied model if requested
334-
if (options[["saveModel"]] && options[["savePath"]] != "") {
335-
validNames <- (length(grep(" ", decodeColNames(colnames(dataset)))) == 0) && (length(grep("_", decodeColNames(colnames(dataset)))) == 0)
336-
if (!validNames) {
337-
return()
338-
}
339-
model <- classificationResult[["model"]]
340-
model[["jaspVars"]] <- list()
341-
model[["jaspVars"]]$decoded <- list(target = decodeColNames(options[["target"]]), predictors = decodeColNames(options[["predictors"]]))
342-
model[["jaspVars"]]$encoded = list(target = options[["target"]], predictors = options[["predictors"]])
343-
model[["jaspScaling"]] <- attr(dataset, "jaspScaling")
344-
model[["jaspVersion"]] <- .baseCitation
345-
model[["explainer"]] <- classificationResult[["explainer"]]
346-
class(model) <- c(class(classificationResult[["model"]]), "jaspClassification", "jaspMachineLearning")
347-
path <- options[["savePath"]]
348-
if (!endsWith(path, ".jaspML")) {
349-
path <- paste0(path, ".jaspML")
350-
}
351-
saveRDS(model, file = path)
352-
}
326+
327+
# Save the model if requested
328+
saveResult <- .mlSaveModelToDisk(options, classificationResult, dataset, class = "jaspClassification")
329+
.mlPossiblyShowSaveResult(table, saveResult, options)
330+
353331
}
354332

355333
.mlClassificationTableConfusion <- function(dataset, options, jaspResults, ready, position) {

R/commonMachineLearningRegression.R

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
if (length(factorsWithNewLevels) > 0) {
167167
setType <- switch(type, "test" = gettext("test set"), "validation" = gettext("validation set"), "prediction" = gettext("new dataset"))
168168
additionalMessage <- switch(type,
169-
"test" = gettext(" or use a different test set (e.g., automatically by setting a different seed or manually by specifying the test set indicator)"),
169+
"test" = gettext(" or use a different test set (e.g., automatically by setting a different seed or manually by specifying the test set indicator)"),
170170
"validation" = gettext(" or use a different validation set by setting a different seed"),
171171
"prediction" = "")
172172
factorMessage <- paste(sapply(factorsWithNewLevels, function(i) {
@@ -337,16 +337,9 @@
337337
if (!ready) {
338338
table$addFootnote(gettextf("Please provide a target variable and at least %d feature variable(s).", if (type == "knn" || type == "neuralnet" || type == "rpart" || type == "svm" || type == "lm") 1L else 2L))
339339
}
340-
if (options[["saveModel"]]) {
341-
validNames <- (length(grep(" ", decodeColNames(colnames(dataset)))) == 0) && (length(grep("_", decodeColNames(colnames(dataset)))) == 0)
342-
if (options[["savePath"]] != "" && validNames) {
343-
table$addFootnote(gettextf("The trained model is saved as <i>%1$s</i>.", basename(options[["savePath"]])))
344-
} else if (options[["savePath"]] != "" && !validNames) {
345-
table$addFootnote(gettext("The trained model is <b>not</b> saved because the some of the variable names in the model contain spaces (i.e., ' ') or underscores (i.e., '_'). Please remove all such characters from the variable names and try saving the model again."))
346-
} else {
347-
table$addFootnote(gettext("The trained model is not saved until a file name is specified under 'Save as'."))
348-
}
349-
}
340+
341+
.mlAddSaveModelInfo(table, options)
342+
350343
jaspResults[["regressionTable"]] <- table
351344
if (!ready) {
352345
return()
@@ -492,25 +485,58 @@
492485
)
493486
table$addRows(row)
494487
}
488+
495489
# Save the model if requested
496-
if (options[["saveModel"]] && options[["savePath"]] != "") {
497-
validNames <- (length(grep(" ", decodeColNames(colnames(dataset)))) == 0) && (length(grep("_", decodeColNames(colnames(dataset)))) == 0)
498-
if (!validNames) {
499-
return()
500-
}
501-
model <- regressionResult[["model"]]
490+
saveResult <- .mlSaveModelToDisk(options, regressionResult, dataset, class = "jaspRegression")
491+
.mlPossiblyShowSaveResult(table, saveResult, options)
492+
493+
494+
}
495+
496+
.mlAddSaveModelInfo <- function(table, options) {
497+
if (options[["saveModel"]] && options[["savePath"]] == "") {
498+
table$addFootnote(gettext("The trained model is not saved until a file name is specified under 'Save as'."))
499+
}
500+
}
501+
502+
.mlSaveModelToDisk <- function(options, mlResult, dataset, class = c("jaspRegression", "jaspClassification")) {
503+
504+
if (!options[["saveModel"]] || options[["savePath"]] == "")
505+
return()
506+
507+
class <- match.arg(class)
508+
509+
error <- try({
510+
model <- mlResult[["model"]]
502511
model[["jaspVars"]] <- list()
503512
model[["jaspVars"]]$decoded <- list(target = decodeColNames(options[["target"]]), predictors = decodeColNames(options[["predictors"]]))
504513
model[["jaspVars"]]$encoded = list(target = options[["target"]], predictors = options[["predictors"]])
505514
model[["jaspScaling"]] <- attr(dataset, "jaspScaling")
506515
model[["jaspVersion"]] <- .baseCitation
507-
model[["explainer"]] <- regressionResult[["explainer"]]
508-
class(model) <- c(class(regressionResult[["model"]]), "jaspRegression", "jaspMachineLearning")
516+
model[["explainer"]] <- mlResult[["explainer"]]
517+
class(model) <- c(class(mlResult[["model"]]), class, "jaspMachineLearning")
509518
path <- options[["savePath"]]
510519
if (!endsWith(path, ".jaspML")) {
511520
path <- paste0(path, ".jaspML")
512521
}
513522
saveRDS(model, file = path)
523+
524+
"success"
525+
})
526+
return(list(exists = file.exists(path), error = error))
527+
}
528+
529+
.mlPossiblyShowSaveResult <- function(table, saveResult, options) {
530+
531+
if (is.null(saveResult))
532+
return()
533+
534+
if (identical(saveResult[["error"]], "success") && isTRUE(saveResult[["exists"]])) {
535+
table$addFootnote(gettextf("The model is saved as <i>%1$s</i>.", basename(options[["savePath"]])))
536+
} else if (!identical(saveResult[["error"]], "success")) {
537+
table$addFootnote(gettextf("The model could not be saved because the following error occured: %s", saveResult[["error"]][["message"]]))
538+
} else if (!isTRUE(saveResult[["exists"]]) && !is.null(saveResult[["error"]])) {
539+
table$addFootnote(gettextf("The model could not be saved because an unexpected error occured."))
514540
}
515541
}
516542

0 commit comments

Comments
 (0)