harrelfe / Hmisc

Harrell Miscellaneous
Other
208 stars 81 forks source link

Conflict with `psych::describe()` when loading the `Hmisc` package #155

Closed isaactpetersen closed 2 years ago

isaactpetersen commented 2 years ago

I'm trying to use the describe() function from the psych package. However, I receive an error when using it after having loaded the Hmisc package, even if specifying psych::describe():

> psych::describe(df)
Error in as.data.frame.default(utils::head(x, maxPrint)): cannot coerce class '"describe"' to a data.frame

I get the error when rendering an rmarkdown (.Rmd) file. I do not get the error when running the underlying R code. I still get the error even after unloading Hmisc. I've isolated the issue to when I am specifying df_print: paged in the YAML settings for html_document.

Here is a reproducible example with session info etc. (the error occurs in Section 9): https://isaactpetersen.github.io/reprex/

I understand that this may not be an issue with the Hmisc package per se, bur rather the interaction of Hmisc and psych. Nevertheless, it is well documented that Hmisc and psych have key conflicts:

https://stackoverflow.com/questions/28986140/psych-and-hmisc-packages-with-describe-class

https://github.com/harrelfe/rms/issues/100

https://github.com/strengejacke/sjPlot/issues/229

One potential solution to this is for the user to not load the Hmisc package at all (and to not load any packages that depend on Hmisc), at least not loading them before using any psych functions. However, that is not an appealing solution. I hope there is a workable solution that allows loading both packages in whatever order, but specifying which function is intended (and respecting the specified one). I'd like to be able to continue to load and use Hmisc, if possible.

Here is the reproducible example (index.Rmd):

---
title: "Reproducible Example"
output:
  html_document:
    number_sections: true
    df_print: paged
---

```{r setup}
knitr::opts_chunk$set(echo = TRUE,
                      error = TRUE)

Methods

methods("describe")

Load Libraries

library("Hmisc", exclude = c("describe")) 
library("psych")

Simulate Data

set.seed(52242)

n <- 1000

ID <- rep(1:100, each = 10)
predictor <- rbeta(n, 1.5, 5) * 100
outcome <- predictor + rnorm(n, mean = 0, sd = 20) + 50

df <- data.frame(ID = ID,
                 predictor = predictor,
                 outcome = outcome)

Session Info

Hmisc is loaded:

sessionInfo()

Methods

methods("describe")

Unload Hmisc

unloadNamespace("Hmisc")

Session Info

Hmisc is unloaded:

sessionInfo()

Methods

methods("describe")

Use psych::describe()

Throws error:

psych::describe(df)

Session Info

sessionInfo()

I render the document using the following code:

rmarkdown::render("index.Rmd")

couthcommander commented 2 years ago

R is calling the wrong print method (Hmisc:::print.describe which is internal). You really want to use print.psych, and can change the line to print(psych::describe(df)). I'm not familiar with psych and its print method but it doesn't look like its output will look nice in "paged" format as it is not a data.frame.