Search code examples
rmodelsummary

datasummary_crosstab returns an error if the piped dataframe is not reduced to selected variables


I am working with the datasummary functions in the modelsummary package.

This code is reproducible and shows that datasummary_crosstab only seems to work if the dataframe that contains the variables is modified to include only those variables. I don't see this in the documentation anywhere. Am I missing something?

#Install my personal package that holds all my recoded variables
# for the CES
devtools::install_github("sjkss/cesdata2")
#Load
library(cesdata2)
# load ces21
data("ces21")
library(modelsummary)
#Returns an error
datasummary_crosstab(past_vote~ vote, data=ces21)
#Works
ces21 %>% 
  select(past_vote, vote) %>% 
  datasummary(past_vote~vote, data=.)

Solution

  • Your data includes columns of a weird non-base R class: haven_labelled.

    modelsummary supports base R types, but not every possible custom types handled by external packages. When modelsummary does not know a type, it tries to coerce it to numeric or character, using as.numeric() and as.character().

    In your particular case, these functions generate an error unless you load the haven library:

    library(cesdata2)
    data("ces21")
    library(modelsummary)
    library(haven)
    
    datasummary_crosstab(
        past_vote~vote, 
        data=ces21[ c("past_vote", "vote")])