Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug - speed up get_chi() #68

Merged
merged 18 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/get_anon_chi.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#' chi_cohort %>% get_anon_chi()
#' chi_cohort %>% get_anon_chi(chi_var = "upi_number")
#' }
get_anon_chi <- function(chi_cohort, chi_var = "chi", drop = TRUE, check = TRUE) {

Check warning on line 21 in R/get_anon_chi.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_anon_chi.R,line=21,col=81,[line_length_linter] Lines should not be more than 80 characters.
if (check) {
# Optional code, if the user has phsmethods installed check the CHIs with it.

Check warning on line 23 in R/get_anon_chi.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_anon_chi.R,line=23,col=81,[line_length_linter] Lines should not be more than 80 characters.
if (rlang::is_installed("phsmethods")) {
checked_chi <- phsmethods::chi_check(
dplyr::pull(chi_cohort, {{ chi_var }})
Expand All @@ -39,11 +39,11 @@
))
} else if (n_invalid > 0) {
cli::cli_alert_warning(
"{n_invalid} CHI number{?s} {?is/are} invalid according to {.fn phsmethods::chi_check}."

Check warning on line 42 in R/get_anon_chi.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_anon_chi.R,line=42,col=81,[line_length_linter] Lines should not be more than 80 characters.
)
print(
tibble::tibble(
{{ chi_var }} := dplyr::pull(chi_cohort, {{ chi_var }})[which_invalid],

Check warning on line 46 in R/get_anon_chi.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_anon_chi.R,line=46,col=81,[line_length_linter] Lines should not be more than 80 characters.
reason = checked_chi[which_invalid]
)
)
Expand All @@ -54,7 +54,9 @@
lookup <- tibble::tibble(
chi = unique(chi_cohort[[chi_var]])
) %>%
dplyr::mutate(anon_chi = convert_chi_to_anon_chi(.data$chi))
dplyr::mutate(chi = dplyr::if_else(is.na(.data$chi), "", .data$chi),
anon_chi = purrr::map_chr(.data$chi, openssl::base64_encode),
anon_chi = dplyr::if_else(.data$anon_chi == "", NA_character_, .data$anon_chi))

Check warning on line 59 in R/get_anon_chi.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_anon_chi.R,line=59,col=81,[line_length_linter] Lines should not be more than 80 characters.

chi_cohort <- chi_cohort %>%
dplyr::left_join(
Expand Down
22 changes: 8 additions & 14 deletions R/get_chi.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ get_chi <- function(data, anon_chi_var = "anon_chi", drop = TRUE) {
lookup <- tibble::tibble(
anon_chi = unique(data[[anon_chi_var]])
) %>%
dplyr::mutate(chi = convert_anon_chi_to_chi(.data$anon_chi))

dplyr::mutate(anon_chi = dplyr::if_else(is.na(.data$anon_chi), "", .data$anon_chi),
chi = unname(convert_anon_chi_to_chi(.data$anon_chi)),
chi = dplyr::if_else(.data$chi == "", NA_character_, .data$chi))
data <- data %>%
dplyr::left_join(
lookup,
Expand All @@ -36,17 +37,10 @@ get_chi <- function(data, anon_chi_var = "anon_chi", drop = TRUE) {
return(data)
}

convert_anon_chi_to_chi <- function(anon_chi) {
chi <- purrr::map_chr(
anon_chi,
~ dplyr::case_match(.x,
NA_character_ ~ NA_character_,
"" ~ "",
.default = openssl::base64_decode(.x) %>%
substr(2, 2) %>%
paste0(collapse = "")
)
)
convert_anon_chi_to_chi <- Vectorize(function(anon_chi) {
chi <- openssl::base64_decode(anon_chi) %>%
substr(2, 2) %>%
paste0(collapse = "")

return(chi)
}
})
2 changes: 1 addition & 1 deletion man/read_slf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/read_slf_episode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/read_slf_individual.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading