From 1980f5dfbcca7fcaa14a06ae569732b03d91bbff Mon Sep 17 00:00:00 2001 From: Julius Pfadt Date: Wed, 1 May 2024 16:46:53 +0200 Subject: [PATCH] add scores to data and also delete old added scores --- R/principalcomponentanalysis.R | 53 +++++++++++++++++-------- inst/qml/PrincipalComponentAnalysis.qml | 8 ---- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/R/principalcomponentanalysis.R b/R/principalcomponentanalysis.R index 9b59832f..3af1b1d7 100644 --- a/R/principalcomponentanalysis.R +++ b/R/principalcomponentanalysis.R @@ -635,31 +635,50 @@ principalComponentAnalysisInternal <- function(jaspResults, dataset, options, .. .pcaAddComponentsToData <- function(jaspResults, modelContainer, options, ready) { if (!ready || - !options[["addComponentScores"]] || - modelContainer$getError() || - !is.null(jaspResults[["addedScoresContainer"]])) { + !is.null(jaspResults[["addedScoresContainer"]]) || + modelContainer$getError()) + { return() } - container <- createJaspContainer() - container$dependOn(optionsFromObject = modelContainer) + colNamesR <- gettextf("Component_%s", seq_len(length(options$variables))) - scores <- modelContainer[["model"]][["object"]][["scores"]] - baseNames <- gettextf("Component_%s", seq_len(ncol(scores))) - # encodedNames <- jaspBase::createColumns(baseNames) + if (options[["addComponentScores"]]) { - # print(length(container[[""]])) - # if (length(encodedNames) == length(baseNames)) { - for (i in seq_len(ncol(scores))) { - container[[baseNames[i]]] <- jaspBase::createJaspColumn(baseNames[i]) - container[[baseNames[i]]]$setScale(scores[, i]) + container <- createJaspContainer() + container$dependOn(optionsFromObject = modelContainer) + + scores <- modelContainer[["model"]][["object"]][["scores"]] + + for (ii in seq_len(ncol(scores))) { + + colNameR <- colNamesR[ii] + + if (jaspBase:::columnExists(colNameR) && !jaspBase:::columnIsMine(colNameR)) { + .quitAnalysis(gettext("Column name already exists in the dataset")) + } + + container[[colNameR]] <- jaspBase::createJaspColumn(colNameR) + container[[colNameR]]$setScale(scores[, ii]) } + jaspResults[["addedScoresContainer"]] <- container - # } else { - # container <- NULL - # jaspResults[["addedScoresContainer"]] <- NULL - # } + # check if there are previous colNames that are not needed anymore and delete the cols + oldNames <- jaspResults[["createdColumnNames"]][["object"]] + newNames <- colNamesR[1:ii] + if (!is.null(oldNames)) { + noMatch <- which(!(oldNames %in% newNames)) + if (length(noMatch) > 0) { + for (i in 1:length(noMatch)) { + jaspBase:::columnDelete(oldNames[noMatch[i]]) + } + } + } + + # save the created col names + jaspResults[["createdColumnNames"]] <- createJaspState(newNames) + } return() diff --git a/inst/qml/PrincipalComponentAnalysis.qml b/inst/qml/PrincipalComponentAnalysis.qml index d3c5ad2f..bfbf1c3e 100644 --- a/inst/qml/PrincipalComponentAnalysis.qml +++ b/inst/qml/PrincipalComponentAnalysis.qml @@ -242,14 +242,6 @@ Form text: qsTr("Add PC scores to data") enabled: variables.count > 1 - // ComputedColumnField - // { - // name: "addComponentScoresPrefix" - // text: qsTr("Column prefix") - // placeholderText: qsTr("Score_") - // fieldWidth: 120 - // enabled: addPC.checked - // } } } }