arthur-shaw / susoapi

R interface for Survey Solutions' APIs
https://arthur-shaw.github.io/susoapi/
Other
9 stars 5 forks source link

Error in `get_interview_for_questionnaire` #6

Closed ashwinikalantri closed 3 years ago

ashwinikalantri commented 3 years ago

While using get_interview_for_questionnaire with a questionnaire that doesn't have identifying data, I get this error: Error: Column 'value' not found in '.data'.

arthur-shaw commented 3 years ago

Thanks for flagging this. I thought I'd handled this, but--shame on me--perhaps the code didn't make it into the release. Stay tuned.

ashwinikalantri commented 3 years ago

I can't seem to get this working. get_interviews_for_questionnaire gives this error:

Error: Problem with `mutate()` column `has_identifying`.
ℹ `has_identifying = length(interviews_df$identifyingData[[dplyr::row_number()]])`.
x no such index at level 2

https://github.com/arthur-shaw/susoapi/blob/52de0704854189a9621f30f917ca09ac5d4e5e22/R/questionnaires.R#L406

I tired to isolate the issue but can't seem to understand why we get the error. The mutate function gives error while doing this outside works fine.

script

test <- interviews_df %>%
  dplyr::select(id, .data$identifyingData)

test %>% dplyr::mutate(has_identifying = length(a$identifyingData[[dplyr::row_number()]]))

length(test$identifyingData[[1]])
length(test$identifyingData[[70]])

output

> test <- interviews_df %>%
+   dplyr::select(id, .data$identifyingData)
> test %>% dplyr::mutate(has_identifying = length(test$identifyingData[[dplyr::row_number()]]))
Error: Problem with `mutate()` column `has_identifying`.
ℹ `has_identifying = length(test$identifyingData[[dplyr::row_number()]])`.
x no such index at level 2

Run `rlang::last_error()` to see where the error occurred.
> length(test$identifyingData[[1]])
[1] 0
> length(test$identifyingData[[70]])
[1] 0
ashwinikalantri commented 3 years ago

Adding rowwise() seems to be giving the correct output.

test %>%
  rowwise() %>%
  dplyr::mutate(has_identifying = length(test$identifyingData[[dplyr::row_number()]]))
arthur-shaw commented 3 years ago

Thanks for catching and troubleshooting this! For whatever reason, I couldn't reproduce the error on my end. But I understand the problem. Rather than use dplyr::rowwise(), I'll employ purrr::map_int, which I find more readable than my computation of length. Your propsal lead me to this solution, by way of this article.