Skip to content

Commit

Permalink
Fix bug when no resolution column is present
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyRStevens committed Jan 23, 2024
1 parent 46c91ac commit 398c1e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 10 additions & 9 deletions R/use_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ use_labels <- function(x) {
colnames(x) <- attributes(x)$column_map[attributes(x)$column_map$qname %in% column_names, ]$description

# Find extraneous text to remove from computer info columns
text <- x %>%
dplyr::select(dplyr::contains(" - Resolution")) %>%
names() %>%
strsplit(split = " - ")
throwaway <- paste0(text[[1]][1], " - ")

# Rename columns
x <- x %>%
dplyr::rename_with(~ gsub(throwaway, "", .x), dplyr::contains(throwaway))
if (any(grepl("Resolution", column_names))) {
text <- x %>%
dplyr::select(dplyr::contains("Resolution")) %>%
names() %>%
strsplit(split = " - ")
throwaway <- paste0(text[[1]][1], " - ")

# Rename columns
x <- x %>%
dplyr::rename_with(~ gsub(throwaway, "", .x), dplyr::contains(throwaway))
}
return(x)
}
}
6 changes: 5 additions & 1 deletion tests/testthat/test-use_labels.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("alert works", {
expect_error(use_labels(qualtrics_numeric))
expect_error(use_labels(qualtrics_numeric), "Data frame does not have proper Qualtrics labels")
expect_error(use_labels(qualtrics_fetch, NA))
})

Expand All @@ -10,3 +10,7 @@ test_that("labels are correct", {
expect_true(colnames(qualtrics_fetch)[16] == "Q1_Resolution")
expect_true(colnames(use_labels(qualtrics_fetch))[16] == "Resolution")
})

test_that("dataframes without resolution are OK", {
expect_no_error(use_labels(qualtrics_fetch %>% dplyr::select(!contains("Resolution"))))
})

0 comments on commit 398c1e1

Please sign in to comment.