From b245c550563d42d2e6d3e84d492f49d95e31b709 Mon Sep 17 00:00:00 2001 From: larann901 Date: Wed, 25 Sep 2024 12:38:28 +0200 Subject: [PATCH] solved errors in read_mpath --- DESCRIPTION | 16 +++++++++------- NAMESPACE | 3 +++ R/read_mpath.R | 28 ++++++++++++---------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3525f12..fb76b7f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,14 +1,15 @@ Package: mpathr -Title: What the Package Does (One Line, Title Case) +Title: Handling Data From The M-Path Platform Version: 0.0.0.9000 Authors@R: c( - person("Merijn", "Mestdagh", email = "merijn.mestdagh@kuleuven.be", role = c("aut"), + person("Merijn", "Mestdagh", email = "merijn.mestdagh@m-path.io", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5077-861X")), - person("Koen", "Niemeijer", email = "koen.niemeijer@kuleuven.be", role = c("aut", "cre"), - comment = c(ORCID = "0000-0002-0816-534X")) + person("Koen", "Niemeijer", email = "koen.niemeijer@kuleuven.be", role = c("aut"), + comment = c(ORCID = "0000-0002-0816-534X")), + person("Lara", "Navarrete", email = "larann901@gmail.com", role = c("aut")) ) -data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAAWElEQVR42mNgGPTAxsZmJsVqQApgmGw1yApwKcQiT7phRBuCzzCSDSHGMKINIeDNmWQlA2IigKJwIssQkHdINgxfmBBtGDEBS3KCxBc7pMQgMYE5c/AXPwAwSX4lV3pTWwAAAABJRU5ErkJggg== -Description: Handling data from the m-path platform. +data: image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAAWElEQVR42mNgGPTAxsZmJsVqQApgmGw1yApwKcQiT7phRBuCzzCSDSHGMKINIeDNmWQlA2IigKJwIssQkHdINgxfmBBtGDEBS3KCxBc7pMQgMYE5c/AXPwAwSX4lV3pTWwAAAABJRU5ErkJggg== +Description: The goal of 'mpathr' is to provide with a few utility functions to be able to read and perform some common operations in ESM data collected through the m-Path platform. License: GPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) @@ -25,7 +26,8 @@ Imports: tidyr, rlang, ggplot2, - jsonlite + jsonlite, + tidyselect Language: en-US URL: https://github.com/m-path-io/mpathr BugReports: https://github.com/m-path-io/mpathr/issues diff --git a/NAMESPACE b/NAMESPACE index 1c92ebe..45ac736 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(mpath_example) export(plot_response_rates) export(read_mpath) export(response_rate) @@ -19,8 +20,10 @@ importFrom(ggplot2,aes) importFrom(ggplot2,aes_string) importFrom(ggplot2,ggplot) importFrom(jsonlite,fromJSON) +importFrom(jsonlite,stream_in) importFrom(lifecycle,deprecated) importFrom(rlang,enquo) importFrom(rlang,quo_name) importFrom(tidyr,pivot_longer) importFrom(tidyr,pivot_wider) +importFrom(tidyselect,all_of) diff --git a/R/read_mpath.R b/R/read_mpath.R index 83774d0..70ebe0b 100644 --- a/R/read_mpath.R +++ b/R/read_mpath.R @@ -177,26 +177,22 @@ read_mpath <- function( # # Integer cols: data_int_lists <- data %>% mutate(across(all_of(int_list_cols), ~ lapply(., function(x) { - if (!is.na(x)) { - tryCatch({ - # Attempt to split, unlist, and convert to integers - as.integer(unlist(strsplit(x, ","))) - }, warning = function(w) { - # Handle warning about 'NAs introduced by coercion to integer range' - if (grepl("NAs introduced by coercion to integer range", conditionMessage(w))) { - return(as.numeric(unlist(strsplit(x, ",")))) # Fallback to numeric if integer fails - } - }) - } else { - x # Return NA or empty cells as-is - } + tryCatch({ + # Attempt to split, unlist, and convert to integers + as.integer(unlist(strsplit(.x, ","))) + }, warning = function(w) { + # Handle warning about 'NAs introduced by coercion to integer range' + if (grepl("NAs introduced by coercion to integer range", conditionMessage(w))) { + return(as.numeric(unlist(strsplit(.x, ",")))) # Fallback to numeric if integer fails + } + }) }))) data[,int_list_cols] <- data_int_lists[,int_list_cols] # Numeric: data_num_lists <- data %>% - mutate(across(all_of(num_list_cols), ~ I(lapply(., ~ as.numeric(strsplit(.x, ",")[[1]]))))) + mutate(across(all_of(num_list_cols), ~ I(lapply(.x, function(x) as.numeric(strsplit(x, ",")[[1]]))))) data[,num_list_cols] <- data_num_lists[,num_list_cols] @@ -217,13 +213,13 @@ read_mpath <- function( # for string cols: we can get rid of the \" using fromJSON data_strings <- data %>% mutate(across(all_of(string_cols), - ~ I(lapply(., function(cell) { + ~ sapply(.x, function(cell) { if (!is.na(cell)) { unlist(fromJSON(cell)) } else { cell # Return NA as is } - })) + }) )) data[,string_cols] <- data_strings[,string_cols]