Skip to content

write_hrc2() fails when the first column contains a single value (e.g. a total column) #63

Description

@jdesclodure

The issue occurs in the write_hrc2() function in R/writehrc.R.

Reproducible example

corr_table <- data.frame(
  niveau1 = c("Total", "Total", "Total"),
  niveau2 = c("A", "A", "B"),
  niveau3 = c("A1", "A2", "B1")
)

write_hrc2(
  corr_table = corr_table,
  file_name = tempfile(fileext = ".hrc"),
  sort_table = TRUE
)

Error

Error in Ops.data.frame(corr_table, corr_table_dec_left) :
  ‘==’ only defined for equally-sized data frames

The same error occurs with sort_table = FALSE.

Possible cause

d is computed before the first column is removed:

d <- dim.data.frame(corr_table)

if (length(unique(as.character(corr_table[,1]))) == 1) {
  corr_table <- corr_table[,-1]
}

After removing the column, d is no longer consistent with corr_table.

A possible fix:

if (length(unique(as.character(corr_table[,1]))) == 1) {
  corr_table <- corr_table[,-1]
  d <- dim.data.frame(corr_table)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions