From ab84109d5587a4b4dff340598d3e5b7f17bc977d Mon Sep 17 00:00:00 2001 From: junos Date: Wed, 24 Nov 2021 19:07:56 +0100 Subject: [PATCH] Prepare a function to compile participants data. It combines functions from container.R --- ...anslate_usernames_into_participants_data.R | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/data/translate_usernames_into_participants_data.R diff --git a/src/data/translate_usernames_into_participants_data.R b/src/data/translate_usernames_into_participants_data.R new file mode 100644 index 00000000..5194de01 --- /dev/null +++ b/src/data/translate_usernames_into_participants_data.R @@ -0,0 +1,52 @@ +library(RPostgres) +library(magrittr) +library(tidyverse) +library(lubridate) + +load_container_script <- function(stream_container){ + language <- if_else(endsWith(tolower(stream_container), "py"), "python", "r") + if(language == "python"){ + library(reticulate) + container <- import_from_path(gsub(pattern = "\\.py$", "", basename(stream_container)), path = dirname(stream_container)) + if(!py_has_attr(container, "pull_data")) + stop(paste0("The following container.py script does not have a pull_data function: ", stream_container)) + if(!py_has_attr(container, "infer_device_os")) + stop(paste0("The following container.py script does not have a infer_device_os function: ", stream_container)) + return(list("infer_device_os" = container$infer_device_os, "pull_data" = container$pull_data)) + } else if(language == "r"){ + source(stream_container) + if(!exists("pull_data")) + stop(paste0("The following container.R script does not have a pull_data function: ", stream_container)) + if(!exists("infer_device_os")) + stop(paste0("The following container.R script does not have a infer_device_os function: ", stream_container)) + return(list("infer_device_os" = infer_device_os, "pull_data" = pull_data)) + } +} + +prepare_participants_file <- function() { + #TODO Define appropriate arguments and pass them from config.yaml + stream_container <- snakemake@input[["stream_container"]] + container_functions <- load_container_script(stream_container) + + pull_data_container <- container_functions$pull_data + # TODO Figure out how to use the functions in the container + + participant_data <- pull_participants_ids("whatever", usernames, participants_container) + participant_data %<>% + rename(participant_id = id) + + device_ids <- pull_participants_device_ids("whatever", participant_data$participant_id, device_id_container) + device_ids %<>% + group_by(participant_id) %>% + summarise(device_ids = list(unique(device_id))) + participant_data %<>% + left_join(device_ids, by = "participant_id") + + start_end_datetimes <- pull_participants_start_end_dates("whatever", participant_data$participant_id, start_end_date_container) + participant_data %<>% + left_join(start_end_datetimes, by = "participant_id") + + return(participant_data) +} + +