R/dcvts_getrespondents.R

Defines functions dcvts_getrespondents

Documented in dcvts_getrespondents

#' Get respondents from DCVTS limesurvey panel.
#' @description `dcvts_getrespondents()` fetches responses from DCVTS's limesurvey panel and transforms the data into easily analysable "tidy" format.
#' @param round DCVTS Round. This can be `r1`, `r2` or `r3`.
#' @param ... Extra parameters. You will likely not need this.
#' @details Sometimes this might take longer depending on your connection.
#' @import dplyr
#' @importFrom readr parse_number
#' @importFrom janitor clean_names
#' @importFrom lubridate as_datetime is.POSIXct date
#' @importFrom limer call_limer
#' @importFrom jsonlite flatten
#' @importFrom stringr str_to_title str_c
#' @importFrom rlang .data
#' @export
#' @return A tibble.
#' @examples
#' \dontrun{
#' dcvts_getresponses("r2", "all")
#' dcvts_getresponses("r2", "incomplete")
#' dcvts_getresponses("r1", "complete")
#' }
dcvts_getrespondents <- function(round, ...) {
  round_id <- case_when(round == "r1" ~ 21,
                        round == "r2" ~ 23,
                        round == "r3" ~ 31)

  if (round == "r1") {
    lsattributes <- c('attribute_51', 'attribute_52', 'attribute_53', 'attribute_54', 'attribute_55', 'attribute_56', 'attribute_57', 'attribute_58', 'attribute_60', 'attribute_61', 'attribute_62', 'attribute_63', 'attribute_64')
    lsattributes_vars <- c("tid", "token", "phone1", "phone2", "village", "urbanicity", "state", "district", "iwername", "iwersex", "releaselot", "righthandneighbourname", "lefthandneighbourname", "sampleid", "hohname", "iweremail")

    df_respondents <- call_limer(method = "list_participants",
                                      params = list(iSurveyID = round_id,
                                                    iStart=0,
                                                    iLimit = 3000,
                                                    bUnused=FALSE,
                                                    aAttributes= lsattributes)) %>%
      select(-.data$attribute_55, -.data$attribute_60) %>%
      jsonlite::flatten() %>%
      as_tibble()

    names(df_respondents) <- lsattributes_vars

    df_respondents <- df_respondents %>%
      select(.data$tid, .data$token, .data$sampleid, .data$hohname, .data$iwername, .data$iweremail, everything())

    return(df_respondents)
  }
  else if (round == "r2") {
    lsattributes <- c('attribute_51', 'attribute_53', 'attribute_54', 'attribute_55', 'attribute_56', 'attribute_57', 'attribute_58', 'attribute_60', 'attribute_61', 'attribute_62')
    lsattributes_vars <- c("tid", "token", "phone", "village", "urbanicity", "state", "district", "iwername", "area", "iwersex", "releaselot", "sampleid", "hohname", "iweremail")
    # area and urbanicity swapped for lot4
    lsattributes_vars_lot4 <- c("tid", "token", "phone", "village", "area", "state", "district", "iwername", "urbanicity", "iwersex", "releaselot", "sampleid", "hohname", "iweremail")

    df_respondents_raw <- call_limer(method = "list_participants",
                                      params = list(iSurveyID = round_id,
                                                    iStart=0,
                                                    iLimit = 3000,
                                                    bUnused=FALSE,
                                                    aAttributes= lsattributes)) %>%
      select(-.data$attribute_55) %>%
      jsonlite::flatten() %>%
      as_tibble()

    df_respondents_lot13 <- df_respondents_raw %>%
      filter(parse_number(.data$tid) <= 2700)

    df_respondents_lot4 <- df_respondents_raw %>%
      filter(parse_number(.data$tid) > 2700) %>%
      mutate(attribute_60 = str_to_title(.data$attribute_60))

    names(df_respondents_lot13) <- lsattributes_vars
    names(df_respondents_lot4) <- lsattributes_vars_lot4

    df_respondents <- dplyr::bind_rows(df_respondents_lot13,
                                        df_respondents_lot4) %>%
      select(.data$tid, .data$token, .data$sampleid, .data$hohname, .data$iwername, .data$iweremail, everything()) %>%
      mutate(sampleid = case_when(!str_starts(.data$sampleid, "0") ~ str_c("0", .data$sampleid),
                                  TRUE ~ .data$sampleid))

    rm(df_respondents_raw, df_respondents_lot13, df_respondents_lot4)
    return(df_respondents)
  }
  else {
    lsattributes <- c('attribute_51', 'attribute_52', 'attribute_53', 'attribute_54', 'attribute_56', 'attribute_57', 'attribute_58', 'attribute_61', 'attribute_62', 'attribute_65', 'attribute_66')
    lsattributes_vars <- c("tid", "token", "phone1", "phone2", "village", "urbanicity", "state", "district", "iwername", "iwersex", "releaselot", "round", "previous_rounds_lots", "sampleid", "hohname", "iweremail")
    df_respondents <- call_limer(method = "list_participants",
                                      params = list(iSurveyID = 31,
                                                    iStart=0,
                                                    iLimit = 6000,
                                                    bUnused=FALSE,
                                                    aAttributes= lsattributes)) %>%
      jsonlite::flatten() %>%
      as_tibble()

    names(df_respondents) <- lsattributes_vars

    df_respondents <- df_respondents %>%
      select(.data$tid, .data$token, .data$sampleid, .data$hohname, .data$iwername, .data$iweremail, everything()) %>%
      filter(.data$sampleid != "00000000") %>%
      mutate(sampleid = str_c("0", .data$sampleid))

    return(df_respondents)
    }
}
maskegger/dcvts documentation built on Aug. 8, 2020, 4:34 p.m.