Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tz parameter to functions that use check_data_columns interna… #160

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: iglu
Type: Package
Title: Interpreting Glucose Data from Continuous Glucose Monitors
Version: 4.1.6
Version: 4.1.7
Authors@R: c(person("Elizabeth", "Chun",
role = c("aut")),
person("Steve", "Broll",
Expand Down Expand Up @@ -41,7 +41,7 @@ Description: Implements a wide range of metrics for measuring glucose control an
License: GPL-2
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Depends: R (>= 3.5.0)
Imports:
caTools,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# iglu 4.1.7
* added tz parameter to functions that require it

# iglu 4.1.6
* Updates to the MAGE vignette
* Minor documentation updates
Expand Down
10 changes: 6 additions & 4 deletions R/active_percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#' The function `active_percent` produces the % of time CGM is active together with the length of the measurement period
#'
#' @usage
#' active_percent(data, dt0 = NULL)
#' active_percent(data, dt0 = NULL, tz = "")
#'
#' @inheritParams plot_lasagna
#'
#' @param tz \strong{tz = "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @details
#'The function `active_percent` produces a tibble object with values equal to the
#' percentage of time the CGM was active, the total number of observed days, the start date, and the end date. For example, if a CGM's (5 min frequency) times were 0, 5, 10, 15 and
Expand Down Expand Up @@ -38,14 +40,14 @@
#' data(example_data_5_subject)
#'
#' active_percent(example_data_5_subject)
#' active_percent(example_data_5_subject, dt0 = 5)
#' active_percent(example_data_5_subject, dt0 = 5, tz = 'GMT')
#'


active_percent <- function(data, dt0 = NULL) {
active_percent <- function(data, dt0 = NULL, tz = "") {
active_percent = gl = id = NULL
rm(list = c("gl", "id", "active_percent"))
data = check_data_columns(data, time_check = TRUE)
data = check_data_columns(data, time_check = TRUE, tz = tz)
is_vector = attr(data, "is_vector")

subject = unique(data$id)
Expand Down
2 changes: 1 addition & 1 deletion R/agp.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ agp <- function(data, maxd = 14, inter_gap = 45, dt0 = NULL, tz = "", daily = TR
}

# Calculate range of measurements
out_range = active_percent(data)
out_range = active_percent(data, tz = tz)

# Calculate metrics
tableStat = data.frame("Glucose statistics", "Value")
Expand Down
8 changes: 5 additions & 3 deletions R/agp_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#' `cv_glu`, `below_percent`, `in_range_percent`, `above_percent`.
#'
#' @usage
#' agp_metrics(data, shinyformat = FALSE)
#' agp_metrics(data, shinyformat = FALSE, tz = '')
#'
#' @param data DataFrame object with column names "id", "time", and "gl".
#'
#' @param shinyformat \strong{Default: FALSE.} Boolean indicating whether the output should be formatted for the single subject
#' AGP page in shiny.
#'
#' @param tz \strong{Default: "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @return
#' By default, a tibble object with 1 row for each subject, and 13 columns is returned:
#' a column for subject id,
Expand Down Expand Up @@ -66,12 +68,12 @@
#'


agp_metrics <- function (data, shinyformat = FALSE) {
agp_metrics <- function (data, shinyformat = FALSE, tz = "") {

id = . = start_date = end_date = ndays = CV = GMI = NULL
rm(list = c("id", ".", "start_date", "end_date", "ndays", "CV", "GMI"))

activity <- active_percent(data)
activity <- active_percent(data, tz = tz)

out = list("Start Date" = activity[, c(1,4)],
"End Date" = activity[, c(1,5)],
Expand Down
8 changes: 4 additions & 4 deletions R/all_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ all_metrics <- function(data, dt0 = NULL, inter_gap = 45, tz = "", timelag = 15,
"SD_GLU" = sd_glu(data),
"Mean_GLU" = mean_glu(data),
"CV_GLU" = cv_glu(data),
"Active_Percent" = active_percent(data), # TODO: potentially only keep the 'active_percent' column
"Active_Percent" = active_percent(data, tz = tz), # TODO: potentially only keep the 'active_percent' column
"Percent_In_Tight_Range" = in_range_percent(data, target_ranges = list(c(70,140))),
"GMI" = gmi(data),
"GRI" = gri(data),
"GRI" = gri(data, tz = tz),
"Extended_Hypo" = extended_hypo,
"Extended_Hyper" = extended_hyper
)
Expand All @@ -92,7 +92,7 @@ all_metrics <- function(data, dt0 = NULL, inter_gap = 45, tz = "", timelag = 15,
"eA1C" = ea1c(data),
"episodes" = ep_out,
"GMI" = gmi(data),
"GRI" = gri(data),
"GRI" = gri(data, tz = tz),
"GRADE" = grade(data),
"GRADE_Euglycemia" = grade_eugly(data),
"GRADE_Hyperglycemia" = grade_hyper(data),
Expand All @@ -107,7 +107,7 @@ all_metrics <- function(data, dt0 = NULL, inter_gap = 45, tz = "", timelag = 15,
"M_Value" = m_value(data),
"Mad_GLU" = mad_glu(data),
"MAGE" = mage(data),
"Percent_Active" = active_percent(data),
"Percent_Active" = active_percent(data, tz = tz),
"Percent_Above" = above_percent(data),
"Percent_Below" = below_percent(data),
"Percent_In_Range" = in_range_percent(data),
Expand Down
10 changes: 6 additions & 4 deletions R/gri.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#' GRI value. ' The output rows correspond to subjects.
#'
#' @usage
#' gri(data)
#' gri(data, tz = "")
#'
#' @inheritParams mean_glu
#'
#' @param tz \strong{Default: "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @details
#' A tibble object with 1 row for each subject, a column for subject id and
#' column for GRI is returned. The formula for GRI is as follows:
Expand Down Expand Up @@ -40,10 +42,10 @@
#' gri(example_data_1_subject)
#'
#' data(example_data_5_subject)
#' gri(example_data_5_subject)
#' gri(example_data_5_subject, tz = 'GMT')
#'

gri <- function(data){
gri <- function(data, tz = ""){

gri_single <- function(data) {
# get percent in each range from agp aggregated metrics
Expand All @@ -61,7 +63,7 @@ gri <- function(data){

id = NULL
rm(list = c("id"))
data = check_data_columns(data, time_check = TRUE)
data = check_data_columns(data, time_check = TRUE, tz = tz)

out = data %>%
dplyr::group_by(id) %>%
Expand Down
5 changes: 3 additions & 2 deletions R/heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param clustering_method the agglomeration method for hierarchical clustering, accepts same values as \code{\link{hclust}}, default value is 'complete'
#' @param clustering_distance_metrics the distance measure for metrics clustering, accepts same values as \code{\link{dist}}, default value is 'correlation' distance
#' @param clustering_distance_subjects the distance measure for subjects clustering, accepts same values as \code{\link{dist}}, default value is 'correlation' distance
#' @param tz \strong{Default: "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @return A heatmap of metrics by subjects generated via \code{\link{pheatmap}}
#' @export
Expand All @@ -16,14 +17,14 @@
#' metrics_heatmap(metrics = mecs)
metrics_heatmap <- function(data = NULL, metrics = NULL, metric_cluster = 6, clustering_method = "complete",
clustering_distance_metrics = "correlation",
clustering_distance_subjects = "correlation"){
clustering_distance_subjects = "correlation", tz = ""){
# Check whether pre-calculated metrics are provided
mecs = metrics
if(is.null(mecs)){
if(is.null(data)){
stop("Either CGM data or precalculated metrics must be provided")
}else{
data = check_data_columns(data, time_check=TRUE)
data = check_data_columns(data, time_check=TRUE, tz = tz)
# No metrics are provided, calculate all by default
mecs = all_metrics(data)
}
Expand Down
12 changes: 7 additions & 5 deletions R/plot_meals.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#' The function plot_meals produces a visual for meals data
#'
#' @usage
#' plot_meals(data, mealtimes, plot_type=c('ggplot','plotly'))
#' plot_meals(data, mealtimes, plot_type=c('ggplot','plotly'), tz = "")
#'
#' @inheritParams meal_metrics
#' @inheritParams mage_ma_single
#' @param plot_type Default: "ggplot". One of 'ggplot', 'plotly'. Determines whether the function returns a static publication-ready image or an interactive GUI.
#' @param plot_type \strong{Default: "ggplot".} One of 'ggplot', 'plotly'. Determines whether the function returns a static publication-ready image or an interactive GUI.
#' @param tz \strong{Default: "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @return Plot to visualize meals data.
#'
#' @export
Expand All @@ -34,10 +36,10 @@
#'
#' select_subject = example_data_hall[example_data_hall$id == "2133-018", ]
#' select_meals = example_meals_hall[example_meals_hall$id == "2133-018", ]
#' plot_meals(select_subject, select_meals)
#' plot_meals(select_subject, select_meals, tz = 'GMT')


plot_meals = function(data, mealtimes, plot_type=c('ggplot','plotly')) {
plot_meals = function(data, mealtimes, plot_type=c('ggplot','plotly'), tz = '') {

id = meal = mealtime = peaktime = recovertime = basegl = peakgl = recovergl =
time_window = x = y = NULL
Expand All @@ -53,7 +55,7 @@ plot_meals = function(data, mealtimes, plot_type=c('ggplot','plotly')) {
data = data[data$id == subject, ]
}

metrics = meal_metrics(data, mealtimes, glucose_times = TRUE)
metrics = meal_metrics(data, mealtimes, glucose_times = TRUE, tz = tz)

metrics_expanded = metrics %>%
dplyr::rowwise() %>%
Expand Down
2 changes: 1 addition & 1 deletion R/sd_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ sd_measures <- function(data, dt0 = NULL, inter_gap = 45, tz = ""){

id = NULL
rm(list = c("id"))
data = check_data_columns(data, time_check=TRUE)
data = check_data_columns(data, time_check=TRUE, tz = tz)
subject = unique(data$id)
ns = length(subject)

Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ check_data_time <- function(data, tz = ""){
if (!lubridate::is.POSIXct(data$time)){ # Check if already in date format
tr = as.character(data$time)
data$time = as.POSIXct(tr, format='%Y-%m-%d %H:%M:%S', tz = tz)

if (any(is.na(data$time))) {
stop('Automatic identification of timezone unsuccessful. Please manually set `tz` parameter or double check if the timezone is correct.')
}
}

out = data %>%
Expand Down
6 changes: 4 additions & 2 deletions man/active_percent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/agp_metrics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/gri.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/metrics_heatmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/plot_meals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading