get_labels function error
- Dominant language
- R
- Stars
- 17
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
The following error appears when running pm_annual_caaqs() is executed:
_Error in cut.default(x, breaks = breaks, labels = labels, ordered_result = TRUE, :
lengths of 'breaks' and 'labels' differ_
Close inspection on the code shows the error is due to the get_labels() in cut.R. _labels_ and _breaks_ appear to have incompatible number of elements. Suggested fix to the cut.R below, which adds a lower value for labels
```
get_labels <- function(x, output, drop_na) {
if (!output %in% c("labels", "breaks_h", "breaks_u", "colour", "color")) {
stop(output, " is not a valid input for output. It must be either 'labels',",
"'breaks_u' (for unicode encoding), 'breaks_h' (for html encoding)",
" or 'colour' to get hexadecimal colour codes")
}
if (output == "labels") {
labels <- x[["labels"]]
} else if (output == "breaks_u") {
labels <- x[["val_labels_unicode"]]
} else if (output == "breaks_h") {
labels <- x[["val_labels_html"]]
} else if (output %in% c("colour", "color")) {
labels <- x[["colour"]]
}
labels <- c(NA,labels) #added bug fix - JAR 20220811
labels <- factor(labels, levels = labels, ordered = TRUE)
if(drop_na) labels <- droplevels(labels[-1])
if(output == "labels") labels else as.character(labels)
}
```
Contributor guide
Research direction
Reproduce the error by running pm_annual_caaqs(), then inspect get_labels() in cut.R and compare the lengths of labels and breaks passed to cut.default. Evaluate the suggested change in the issue and verify that the function no longer raises the length-mismatch error while preserving its labels, breaks, and colour outputs.
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
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100