lifebit-ai/cloudos

potential bug in `cb_get_participants_table`?

hms1 opened this issue · 0 comments

hms1 commented

While reviewing #80 I noticed a potential bug in cb_get_participants_table. Using the relevant branch:

library(cloudos)

cohortv2 <- cb_load_cohort("61f1bd4b3de81e52ec46d0ea")
cb_get_participants_table(cohortv2)
 Error: All elements must be size one, use `list()` to wrap.
x Element `f4i0a0` is of size 0.
Run `rlang::last_error()` to see where the error occurred.

It looks like the error is happening here:

for (n in c(list(emptyrow), res$data)) {
# important to change NULL to NA using .null_to_na_nested
dta <- .null_to_na_nested(n)
# change types within lists according to col_type
for (name in names(dta)) {
if (is.list(dta[[name]])){
type_func <- col_types[[name]]
dta[[name]] <- list(type_func(dta[[name]]))
}
}
dta <- tibble::as_tibble_row(dta)
df_list <- c(df_list, list(dta))
}

I am not familiar with this code, so someone with a better understanding should look properly, but it appears it can be fixed using something like:

  for (dta in c(list(emptyrow), res$data)) {
    # drop all null elements entirely
    dta[sapply(dta, is.null)] <- NULL
    # change types according to col_type
    for (name in names(dta)) {
      type_func <- col_types[[name]]
      dta[[name]] <- type_func(dta[[name]])
    }
    dta <- tibble::as_tibble_row(dta)
    df_list <- c(df_list, list(dta))
  }