strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
603 stars 91 forks source link

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

Open isaactpetersen opened 2 years ago

isaactpetersen commented 2 years ago

Note that this is not an issue with sjPlot per se, but I was encouraged to open a new issue here with a reproducible example: https://github.com/strengejacke/sjPlot/issues/229

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/

Here is the code (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")