Skip to content

Commit

Permalink
solved errors in read_mpath
Browse files Browse the repository at this point in the history
  • Loading branch information
larann901 committed Sep 25, 2024
1 parent 7a3dc38 commit b245c55
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
16 changes: 9 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
28 changes: 12 additions & 16 deletions R/read_mpath.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]
Expand Down

0 comments on commit b245c55

Please sign in to comment.