From 4d4d8b90daf76aaf54e93fbb5ab22826611692e1 Mon Sep 17 00:00:00 2001 From: Don van den Bergh Date: Tue, 8 Oct 2024 16:59:15 +0200 Subject: [PATCH] fixes for new data reading (#122) * fixes for new data reading * cleanup Description.qml (spaces -> tabs) --- R/networkanalysis.R | 30 +++++++++++++++-------- inst/Description.qml | 34 +++++++++++++-------------- tests/testthat/test-networkanalysis.R | 4 ++-- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/R/networkanalysis.R b/R/networkanalysis.R index 69377b9..a95e56a 100644 --- a/R/networkanalysis.R +++ b/R/networkanalysis.R @@ -49,15 +49,20 @@ NetworkAnalysis <- function(jaspResults, dataset, options) { .networkAnalysisReadData <- function(dataset, options) { - if (!is.null(dataset)) - return(dataset) - - vars2read <- c("variables", "groupingVariable") - exclude <- c() - if (options[["missingValues"]] == "listwise") - exclude <- vars2read + layoutVariables <- c(options[["layoutX"]], options[["layoutY"]]) + layoutVariables <- layoutVariables[layoutVariables != ""] + hasLayoutData <- length(layoutVariables) > 0L + if (hasLayoutData) { + layoutData <- dataset[layoutVariables] + layoutData <- jaspBase::excludeNaListwise(layoutData, layoutVariables) + # remove the two layout columns + dataset[layoutVariables] <- list(NULL) + } - dataset <- jaspBase::readDataSetByVariableTypes(options, vars2read, exclude.na.listwise = exclude) + if (options[["missingValues"]] == "listwise") { + exclude <- c(options[["variables"]], options[["groupingVariable"]]) + dataset <- jaspBase::excludeNaListwise(dataset, exclude[exclude != ""]) + } if (options[["groupingVariable"]] == "") { # one network dataset <- list(dataset) # for compatability with the split behaviour @@ -68,6 +73,10 @@ NetworkAnalysis <- function(jaspResults, dataset, options) { dataset <- split(dataset, groupingVariableData, drop = TRUE) attr(dataset, "groupingVariableData") <- groupingVariableData } + + if (hasLayoutData) + attr(dataset, "layoutData") <- layoutData + return(dataset) } @@ -1289,8 +1298,9 @@ NetworkAnalysis <- function(jaspResults, dataset, options) { # it turns out that we must save the layout in the A1 = ... style. if (options[["layoutX"]] != "" && options[["layoutY"]] != "") { - layoutXData <- .readDataSetToEnd(columns = options[["layoutX"]], exclude.na.listwise = options[["layoutX"]])[[1L]] - layoutYData <- .readDataSetToEnd(columns = options[["layoutY"]], exclude.na.listwise = options[["layoutY"]])[[1L]] + layoutData <- attr(dataset, "layoutData") + layoutXData <- layoutData[[1L]] + layoutYData <- layoutData[[2L]] variables <- unlist(options[["variables"]]) layoutInfo <- .networkAnalysisSanitizeLayoutData(variables, layoutXData, layoutYData, options[["layoutX"]], options[["layoutY"]]) diff --git a/inst/Description.qml b/inst/Description.qml index ea29bc2..1176399 100644 --- a/inst/Description.qml +++ b/inst/Description.qml @@ -12,33 +12,33 @@ Description maintainer: "JASP Team " website: "jasp-stats.org" license: "GPL (>= 2)" + preloadData: true + GroupTitle + { + title: "Classical" + icon: "analysis-network.svg" + } - GroupTitle - { - title: "Classical" - icon: "analysis-network.svg" - } - Analysis { title: qsTr("Network Analysis") - qml: "NetworkAnalysis.qml" - func: "NetworkAnalysis" + qml: "NetworkAnalysis.qml" + func: "NetworkAnalysis" } - + Separator {} - + GroupTitle - { - title: "Bayesian" - icon: "bayesian-analysis-network.svg" - } - + { + title: "Bayesian" + icon: "bayesian-analysis-network.svg" + } + Analysis { title: qsTr("Bayesian Network Analysis") - qml: "BayesianNetworkAnalysis.qml" - func: "BayesianNetworkAnalysis" + qml: "BayesianNetworkAnalysis.qml" + func: "BayesianNetworkAnalysis" } } diff --git a/tests/testthat/test-networkanalysis.R b/tests/testthat/test-networkanalysis.R index 7078915..63e013b 100644 --- a/tests/testthat/test-networkanalysis.R +++ b/tests/testthat/test-networkanalysis.R @@ -196,8 +196,8 @@ n <- 50 p <- 3 dataset <- as.data.frame(matrix(rnorm(n * (p-1)), n, p-1)) dataset$V3 <- dataset$V1 + dataset$V2 + rnorm(n) -dataset$layoutX <- c("V1 = 1", "V2 = 0") -dataset$layoutY <- c("V1 = 1", "V2 = 0") +dataset$layoutX <- factor(c("V1 = 1", "V2 = 0")) +dataset$layoutY <- factor(c("V1 = 1", "V2 = 0")) options <- jaspTools::analysisOptions("NetworkAnalysis") options$estimator <- "ebicGlasso" options$variables <- c("V1", "V2", "V3")