Skip to content

Commit 59d7da9

Browse files
committed
fix(#30488): make polycor an optional correlation dependency
1 parent fa708b6 commit 59d7da9

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Imports:
1414
prediction,
1515
urltools,
1616
psych,
17-
polycor,
1817
dplyr,
1918
tidyr,
2019
tibble,
@@ -46,6 +45,7 @@ Imports:
4645
pwr,
4746
readr
4847
Suggests:
48+
polycor,
4949
testthat,
5050
twitteR,
5151
sentimentr,

R/stats_wrapper.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ do_cor_internal <- function(mat, use, method, diag, output_cols, na.rm) {
317317
# Polychoric coerces every column to ordered so every pair uses the ordinal model.
318318
# Mixed preserves continuous numerics and converts factor/logical columns to ordered,
319319
# allowing hetcor() to select Pearson, polyserial, or polychoric per pair.
320-
loadNamespace("polycor")
320+
if (!requireNamespace("polycor", quietly = TRUE)) {
321+
stop(
322+
"The 'polycor' package is required for polychoric and mixed correlations. ",
323+
"Install it before using this method.",
324+
call. = FALSE
325+
)
326+
}
321327
ordered_df <- as_hetcor_data_frame(mat, force_ordinal = identical(method, "polychoric"))
322328
# hetcor only supports "complete.obs" and "pairwise.complete.obs", while the other
323329
# methods (via cor()/cor.test()) also accept "everything", "all.obs", and

tests/testthat/test_stats_wrapper.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ test_that("do_cor automatic method selects the specified correlation family", {
151151
})
152152

153153
test_that("do_cor supports automatic and mixed correlations with factor inputs", {
154+
skip_if_not_installed("polycor")
155+
154156
set.seed(456)
155157
n <- 100
156158
ordinal <- ordered(cut(rnorm(n), breaks = c(-Inf, -0.84, -0.25, 0.25, 0.84, Inf)))

0 commit comments

Comments
 (0)