#' ODK make wide
#'
#' Take a list of long datasets (as generated by \code{odk_parse_submission}) and convert to "wide" format
#' @param long_list A list generated by \code{odk_parse_submission}
#' @import tidyr
#' @return A list of wide dataframes of identical length to the input. The \code{repeats} element of the list will be of length 1 or greater (depending on the number of repeats in the form)
#' @export
odk_make_wide <- function(long_list){
# Check to make sure thae contents are correct
ok <- length(long_list) == 2
if(!ok){
stop('The input list should be of length 2')
}
ok <- all(names(long_list) %in% c('repeats', 'non_repeats'))
if(!ok){
stop('The names of the input list must be "repeats" and "non_repeats" only')
}
# All ok, widen repeats
repeats <- long_list$repeats
repeats_list <- list()
repeat_names <- sort(unique(repeats$repeat_name))
if(length(repeat_names) > 0){
for(i in 1:length(repeat_names)){
this_repeat_name <- repeat_names[i]
this_data <- repeats %>% filter(repeat_name == this_repeat_name)
this_repeat_wide <- tidyr::spread(data = this_data,
key = key,
value = value,
convert = TRUE)
repeats_list[[i]] <- this_repeat_wide
names(repeats_list)[i] <- this_repeat_name
}
repeats_wide <- repeats_list
} else {
repeats_wide <- data.frame()
}
# All ok, widen non repeats
non_repeats <- long_list$non_repeats
non_repeats_wide <- tidyr::spread(data = non_repeats,
key = key,
value = value,
convert = TRUE)
# Pop back in the list and return
out <- list(repeats = repeats_wide,
non_repeats = non_repeats_wide)
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.