Skip to content

Commit 2a6a0ca

Browse files
authored
Handle JASP files without data gracefully (#65)
1 parent 64986fa commit 2a6a0ca

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

R/dataset.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ extractDatasetFromJASPFile <- function(jaspFile, dataSetIndex = 1L) {
7373
))
7474

7575
if (nrow(columns) == 0) {
76-
stop("No columns found for dataSet ", dataSetIndex)
76+
# No data in this JASP file - return NULL
77+
return(NULL)
7778
}
7879

7980
# Get the labels table - include originalValueJson for value reconstruction
@@ -82,7 +83,8 @@ extractDatasetFromJASPFile <- function(jaspFile, dataSetIndex = 1L) {
8283
# Get the data from DataSet_N table
8384
dataTableName <- paste0("DataSet_", dataSetIndex)
8485
if (!dataTableName %in% DBI::dbListTables(con)) {
85-
stop("Data table '", dataTableName, "' not found in the database")
86+
# No data table in this JASP file - return NULL
87+
return(NULL)
8688
}
8789

8890
# Build a query that casts DBL columns to TEXT to preserve nan/inf values
@@ -544,6 +546,15 @@ devcat <- function(..., file = "", sep = " ", fill = FALSE, labels = NULL,
544546
#' @export
545547
encodeOptionsAndDataset <- function(options, dataset) {
546548

549+
# Handle NULL dataset (analysis doesn't require data)
550+
if (is.null(dataset)) {
551+
return(list(
552+
options = options,
553+
dataset = NULL,
554+
encodingMap = data.frame(original = character(0), encoded = character(0), type = character(0))
555+
))
556+
}
557+
547558
# Load the dataset if it's a path or name
548559
dataset <- loadCorrectDataset(dataset)
549560
allColumnNames <- colnames(dataset)
5.09 KB
Binary file not shown.

tests/testthat/test-analysisOptions.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,20 @@ test_that("analysisOptions flattens deeply nested types/value structures", {
8181
expect_true(is.list(opts$`variables.types`[[1]]$coefficientAlpha))
8282
expect_equal(length(opts$`variables.types`[[1]]$coefficientAlpha), 0)
8383
})
84+
85+
test_that("analysisOptions extracts options from no-data JASP file", {
86+
87+
jaspFile <- file.path(testthat::test_path(), "..", "JASPFiles",
88+
"no_data_summary_stats.jasp")
89+
90+
skip_if_not(file.exists(jaspFile), "No-data JASP file not found")
91+
92+
# Should be able to extract options without error
93+
opts <- jaspTools::analysisOptions(jaspFile)
94+
95+
# Should return a valid options list
96+
expect_true(is.list(opts))
97+
98+
# Should have an analysisName attribute
99+
expect_true(!is.null(attr(opts, "analysisName")))
100+
})

tests/testthat/test-encodeOptionsAndDataset.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,62 @@ test_that("encodeOptionsAndDataset handles empty types gracefully", {
183183
expect_equal(nrow(result$encodingMap), 0)
184184
})
185185

186+
test_that("encodeOptionsAndDataset handles NULL dataset gracefully", {
187+
188+
# Create a simple options list (typical for summary stats analyses without data)
189+
opts <- list(
190+
n = 100,
191+
mean = 50,
192+
sd = 10,
193+
`.meta` = list()
194+
)
195+
196+
# Should not warn or error when dataset is NULL
197+
result <- jaspTools:::encodeOptionsAndDataset(opts, NULL)
198+
199+
# Should return original options unchanged
200+
expect_equal(result$options, opts)
201+
202+
# Dataset should remain NULL
203+
expect_null(result$dataset)
204+
205+
# Encoding map should be empty
206+
expect_equal(nrow(result$encodingMap), 0)
207+
expect_true(is.data.frame(result$encodingMap))
208+
expect_true(all(c("original", "encoded", "type") %in% names(result$encodingMap)))
209+
})
210+
211+
test_that("encodeOptionsAndDataset works with no-data JASP file", {
212+
213+
jaspFile <- file.path(testthat::test_path(), "..", "JASPFiles",
214+
"no_data_summary_stats.jasp")
215+
216+
skip_if_not(file.exists(jaspFile), "No-data JASP file not found")
217+
218+
# Extract options and dataset
219+
opts <- jaspTools::analysisOptions(jaspFile)
220+
dataset <- jaspTools::extractDatasetFromJASPFile(jaspFile)
221+
222+
# Dataset should be NULL
223+
expect_null(dataset)
224+
225+
# Encode should work without errors
226+
result <- jaspTools:::encodeOptionsAndDataset(opts, dataset)
227+
228+
# Result structure should be correct
229+
expect_true(is.list(result))
230+
expect_true(all(c("options", "dataset", "encodingMap") %in% names(result)))
231+
232+
# Options should be unchanged (since no dataset columns to encode)
233+
expect_equal(result$options, opts)
234+
235+
# Dataset should remain NULL
236+
expect_null(result$dataset)
237+
238+
# Encoding map should be empty
239+
expect_equal(nrow(result$encodingMap), 0)
240+
})
241+
186242
test_that("encodeOptionsAndDataset correctly encodes nested option structures", {
187243

188244
jaspFile <- file.path(testthat::test_path(), "..", "JASPFiles", "Effectiveness_of_the_BCG_Vaccine_Against_Tuberculosis.jasp")

tests/testthat/test-extractDatasetFromJASPfile.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,16 @@ test_that("extractDatasetFromJASPFile works for BCG Vaccine dataset", {
195195
}
196196
}
197197
})
198+
199+
test_that("extractDatasetFromJASPFile returns NULL for JASP files without data", {
200+
201+
jaspFile <- file.path(testthat::test_path(), "..", "JASPFiles",
202+
"no_data_summary_stats.jasp")
203+
204+
skip_if_not(file.exists(jaspFile), "No-data JASP file not found")
205+
206+
# Should return NULL instead of error
207+
result <- extractDatasetFromJASPFile(jaspFile)
208+
209+
expect_null(result, info = "JASP files without data should return NULL")
210+
})

0 commit comments

Comments
 (0)