InseeFrLab / InseeFrLab/rtauargus
write_hrc2() fails when the first column contains a single value (e.g. a total column)
- Dominant language
- HTML
- Stars
- 11
- Forks
- 5
- Avg merge
- 1m
- Merged PRs (30d)
- 4
Description
The issue occurs in the [`write_hrc2()`](https://github.com/InseeFrLab/rtauargus/blob/master/R/writehrc.R) function in `R/writehrc.R`.
### Reproducible example
```r
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
```text
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:
```r
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:
```r
if (length(unique(as.character(corr_table[,1]))) == 1) {
corr_table <- corr_table[,-1]
d <- dim.data.frame(corr_table)
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in R/writehrc.R at write_hrc2(), using the reproducible corr_table example from the issue. Inspect how d is used after a single-valued first column is removed, then verify the function succeeds with both sort_table = TRUE and sort_table = FALSE and produces the expected HRC output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 88/100