From 073c16620ddc0e188eca5c2a666d8635fae3db73 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Thu, 17 Oct 2024 17:32:51 +0200 Subject: [PATCH 01/24] feat: add notification helpers --- NAMESPACE | 2 ++ R/duplicates.R | 58 +++++++++++++++++++++++++++++++++++++++++ R/utils.R | 64 ++++++++++++++++++++++++++++++++++++++++++++++ man/get_env_var.Rd | 22 ++++++++++++++++ man/send_email.Rd | 33 ++++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 man/get_env_var.Rd create mode 100644 man/send_email.Rd diff --git a/NAMESPACE b/NAMESPACE index a9f9de03..f1e1b916 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -55,6 +55,7 @@ export(get_default_identifiers) export(get_duplicated_rows) export(get_env_assay_names) export(get_env_identifiers) +export(get_env_var) export(get_expect_one_identifiers) export(get_experiment_groups) export(get_header) @@ -98,6 +99,7 @@ export(rename_DFrame) export(rename_bumpy) export(reset_env_identifiers) export(round_concentration) +export(send_email) export(set_SE_experiment_metadata) export(set_SE_experiment_raw_data) export(set_SE_fit_parameters) diff --git a/R/duplicates.R b/R/duplicates.R index 95173e8f..8b415def 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -185,3 +185,61 @@ throw_msg_if_duplicates <- function(dt, assay_name = "unknown", msg_f = stop, pr msg_f(paste0(msg, msg2, msg3)) } } + +#' send email if assay data.table contains duplicated rows +#' +#' An auxiliary function to send email if duplicated rows in assay data.table are found +#' +#' @param dt data.table with assay data +#' @param by charvec with notification methods (currently 'email' supported) +#' @param assay_name string with the name of the assay +#' @param preview_max_numb number of rows to preview if duplicates found +#' @param metadata list with the additional metadata to send via email +#' @examples +#' sdata <- get_synthetic_data("finalMAE_small") +#' smetrics_data <- convert_se_assay_to_dt(sdata[[1]], "Metrics") +#' throw_msg_if_duplicates(smetrics_data, assay_name = "Metrics", msg_f = futile.logger::flog.info) +#' @return NULL +#' @keywords duplicates +#' +#' @export +#' +notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", preview_max_numb = 4, metadata = NULL) { + + checkmate::assert_data_table(dt) + checkmate::assert_subset(by, c("email", "slack")) + checkmate::assert_string(assay_name) + checkmate::assert_number(preview_max_numb) + checkmate::assert_list(metadata, null.ok = TRUE) + + dup_dt <- get_assay_dt_duplicated_rows(dt, output = "data") + + msg <- sprintf( + "The %i ouf of %i rows are duplicated in the assay '%s'", + NROW(dup_dt), + NROW(dt), + assay_name) + msg2 <- sprintf(" when checking uniquness with the following set of columns: '%s'. ", + toString(get_assay_req_uniq_cols(dt))) + msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'", + preview_numb, + jsonlite::toJSON(dup_dt[seq(preview_numb), ])) + msg <- paste0(msg, msg2, msg3) + + if ("mail" %in% by) { + att_l <- list(c(dup_dt = dupt_dt, metadata)) + att_f <- tempfile() + qs::qsave(att_l, att_f) + m_to <- get_env_var("EMAIL_RECIPIENT") + stopifnot(nchar(m_to) > 0) + send_email(body = msg, to = m_to, from = from, attached_files = att_f) + } + + if ("slack" %in% by) { + s_to <- get_env_var("EMAIL_SLACK_NOTIFICATION") + stopifnot(nchar(s_to) > 0) + send_email(body = msg, to = s_to, from = from) + } + + + } diff --git a/R/utils.R b/R/utils.R index e96d68c2..28299b23 100644 --- a/R/utils.R +++ b/R/utils.R @@ -757,3 +757,67 @@ calc_sd <- function(x) { } } + +#' send email by wrapping `mailR::send.mail` +#' +#' send email by wrapping `mailR::send.mail` +#' +#' @param body a body content of the email +#' @param to a string with the recipient +#' @param subject a string with the subject +#' @param from a string with the sender +#' @param html a logical flag indicating whether the body of the email should be parsed as HTML +#' @param inline a logical flag indicating whether images in the HTML file should be embedded inline +#' @param host_name a string with the hostname +#' @param attached_files a character with file paths to be attached to the email +#' +#' @keywords utils +#' @export +send_email <- + function(body, + subject, + to = get_env_var("EMAIL_RECIPIENT"), + from = get_env_var("EMAIL_SENDER"), + html = TRUE, + inline = FALSE, + host_name = get_env_var("EMAIL_HOSTNAME"), + attached_files = NULL) { + + checkmate::assert_character(body) + checkmate::assert_string(subject, min.chars = 1) + checkmate::assert_string(to, min.chars = 1) + checkmate::assert_from(from, min.chars = 1) + checkmate::assert_flag(html) + checkmate::assert_flag(inline) + checkmate::assert_from(host_name, min.chars = 1) + checkmate::assert_character(attached_files) + checkmate::assert_file_exists(attached_files) + + mailR::send.mail( + from = from, + to = to, + subject = subject, + body = html_data, + smtp = list(host.name = host_name), + html = html, + inline = inline, + attach.files = attached_files + ) + } + + +#' safe wrapper of Sys.getenv() +#' +#' So far the helper is needed to handle env vars containing `:` +#' for which the backslash is automatically added in some contexts +#' and R could not get the original value for these env vars. +#' +#' @param x string with the name of the environemntal variable +#' @param ... additional params for Sys.getenev +#' @keywords utils +# +#' @export +#' @return sanitized value of the env variable +get_env_var <- function(x, ...) { + gsub("\\\\", "", Sys.getenv(x, ...)) +} diff --git a/man/get_env_var.Rd b/man/get_env_var.Rd new file mode 100644 index 00000000..50a90b42 --- /dev/null +++ b/man/get_env_var.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{get_env_var} +\alias{get_env_var} +\title{safe wrapper of Sys.getenv()} +\usage{ +get_env_var(x, ...) +} +\arguments{ +\item{x}{string with the name of the environemntal variable} + +\item{...}{additional params for Sys.getenev} +} +\value{ +sanitized value of the env variable +} +\description{ +So far the helper is needed to handle env vars containing \code{:} +for which the backslash is automatically added in some contexts +and R could not get the original value for these env vars. +} +\keyword{utils} diff --git a/man/send_email.Rd b/man/send_email.Rd new file mode 100644 index 00000000..903903d0 --- /dev/null +++ b/man/send_email.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{send_email} +\alias{send_email} +\title{send email by wrapping \code{mailR::send.mail}} +\usage{ +send_email( + html_data, + to, + subject, + from = get_env_var("EMAIL_SENDER", "gdr-support-d@gene.com"), + inline = FALSE +) +} +\arguments{ +\item{to}{string with the recipient} + +\item{subject}{string with the subject} + +\item{from}{string with the sender} + +\item{inline}{A boolean indicating whether images in the HTML file should be embedded inline} + +\item{body}{body content of the email} + +\item{html}{A boolean indicating whether the body of the email should be parsed as HTML} + +\item{attached_files}{A character with file paths to be attached to the email} +} +\description{ +send email by wrapping \code{mailR::send.mail} +} +\keyword{utils} From 5d526e6ebc5721f8606838195e1a3be605294ca4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Fri, 18 Oct 2024 08:06:31 +0200 Subject: [PATCH 02/24] chore: add notification helpers --- DESCRIPTION | 4 ++-- NEWS.md | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 65105279..6387f0cc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: gDRutils Type: Package Title: A package with helper functions for processing drug response data -Version: 1.3.16 -Date: 2024-10-11 +Version: 1.3.17 +Date: 2024-10-18 Authors@R: c(person("Bartosz", "Czech", role=c("aut"), comment = c(ORCID = "0000-0002-9908-3007")), person("Arkadiusz", "Gladki", role=c("cre", "aut"), email="gladki.arkadiusz@gmail.com", diff --git a/NEWS.md b/NEWS.md index b7ede461..0c860316 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +## gDRutils 1.3.17 - 2024-10-18 +* add notification helpers + ## gDRutils 1.3.16 - 2024-10-11 * make duplicates' helpers supporting combo assays as well From 9f99771847a57dbe0930ac673b92a7d50578941a Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:00 +0200 Subject: [PATCH 03/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index 8b415def..c9543a27 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -215,7 +215,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ dup_dt <- get_assay_dt_duplicated_rows(dt, output = "data") msg <- sprintf( - "The %i ouf of %i rows are duplicated in the assay '%s'", + "The %i out of %i rows are duplicated in the assay '%s'", NROW(dup_dt), NROW(dt), assay_name) From 286f0683153124c7694eddc70a686756a2967781 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:07 +0200 Subject: [PATCH 04/24] Update R/utils.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/utils.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 28299b23..677a27c8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -758,7 +758,6 @@ calc_sd <- function(x) { } -#' send email by wrapping `mailR::send.mail` #' #' send email by wrapping `mailR::send.mail` #' From 979ce605eff4dedb67715dccb8b4a8679c69d4ac Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:15 +0200 Subject: [PATCH 05/24] Update R/utils.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/utils.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 677a27c8..5eb9d325 100644 --- a/R/utils.R +++ b/R/utils.R @@ -758,7 +758,6 @@ calc_sd <- function(x) { } -#' #' send email by wrapping `mailR::send.mail` #' #' @param body a body content of the email From cb2226d3d2792682256777471e37be6b7d2dc6ca Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:29 +0200 Subject: [PATCH 06/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index c9543a27..58a1bd3f 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -219,7 +219,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ NROW(dup_dt), NROW(dt), assay_name) - msg2 <- sprintf(" when checking uniquness with the following set of columns: '%s'. ", + msg2 <- sprintf(" when checking uniqueness with the following set of columns: '%s'. ", toString(get_assay_req_uniq_cols(dt))) msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'", preview_numb, From 7fb67788afcc70467806794754e19cef65f4dea1 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:38 +0200 Subject: [PATCH 07/24] Update R/utils.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 5eb9d325..ce37e3fd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -760,7 +760,7 @@ calc_sd <- function(x) { #' send email by wrapping `mailR::send.mail` #' -#' @param body a body content of the email +#' @param body a character containing the body content of the email #' @param to a string with the recipient #' @param subject a string with the subject #' @param from a string with the sender From 351b49b31d6b840c37553f820af4c4af5037a4cf Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:12:50 +0200 Subject: [PATCH 08/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index 58a1bd3f..d99e3c24 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -227,7 +227,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ msg <- paste0(msg, msg2, msg3) if ("mail" %in% by) { - att_l <- list(c(dup_dt = dupt_dt, metadata)) + att_l <- list(c(dup_dt = dup_dt, metadata)) att_f <- tempfile() qs::qsave(att_l, att_f) m_to <- get_env_var("EMAIL_RECIPIENT") From 30767d9d3e004510e1690957e06d5b3ba7b957c7 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:13:03 +0200 Subject: [PATCH 09/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index d99e3c24..5f616daf 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -226,7 +226,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ jsonlite::toJSON(dup_dt[seq(preview_numb), ])) msg <- paste0(msg, msg2, msg3) - if ("mail" %in% by) { + if ("email" %in% by) { att_l <- list(c(dup_dt = dup_dt, metadata)) att_f <- tempfile() qs::qsave(att_l, att_f) From 708cae47e89427a20c692bfe2f47214d6761554f Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Fri, 18 Oct 2024 10:33:59 +0200 Subject: [PATCH 10/24] chore: improve asserts --- R/utils.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index 28299b23..dd88a1f2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -790,8 +790,11 @@ send_email <- checkmate::assert_flag(html) checkmate::assert_flag(inline) checkmate::assert_from(host_name, min.chars = 1) - checkmate::assert_character(attached_files) - checkmate::assert_file_exists(attached_files) + if (!is.null(attached_files)) { + checkmate::assert_character(attached_files) + checkmate::assert_file_exists(attached_files) + } + mailR::send.mail( from = from, From 1c494b0e7ec0f4d8d5d16b97645683c3173f168f Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Fri, 18 Oct 2024 10:35:44 +0200 Subject: [PATCH 11/24] chore: fix bug in the logic --- R/duplicates.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/duplicates.R b/R/duplicates.R index 8b415def..871af218 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -232,13 +232,13 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ qs::qsave(att_l, att_f) m_to <- get_env_var("EMAIL_RECIPIENT") stopifnot(nchar(m_to) > 0) - send_email(body = msg, to = m_to, from = from, attached_files = att_f) + send_email(body = msg, to = m_to, attached_files = att_f) } if ("slack" %in% by) { s_to <- get_env_var("EMAIL_SLACK_NOTIFICATION") stopifnot(nchar(s_to) > 0) - send_email(body = msg, to = s_to, from = from) + send_email(body = msg, to = s_to) } From b2d6c5e5920e6617fe7a24483c5f00dd2bac1b82 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Fri, 18 Oct 2024 10:41:15 +0200 Subject: [PATCH 12/24] chore: minore improvements --- R/duplicates.R | 5 +++-- R/utils.R | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/R/duplicates.R b/R/duplicates.R index 871af218..cea779ae 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -225,6 +225,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ preview_numb, jsonlite::toJSON(dup_dt[seq(preview_numb), ])) msg <- paste0(msg, msg2, msg3) + m_sbj <- "[gDR] Error - unexpected duplicates found" if ("mail" %in% by) { att_l <- list(c(dup_dt = dupt_dt, metadata)) @@ -232,13 +233,13 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ qs::qsave(att_l, att_f) m_to <- get_env_var("EMAIL_RECIPIENT") stopifnot(nchar(m_to) > 0) - send_email(body = msg, to = m_to, attached_files = att_f) + send_email(body = msg, subject = m_sbj, to = m_to, attached_files = att_f) } if ("slack" %in% by) { s_to <- get_env_var("EMAIL_SLACK_NOTIFICATION") stopifnot(nchar(s_to) > 0) - send_email(body = msg, to = s_to) + send_email(body = msg, subject = m_sbj, to = s_to) } diff --git a/R/utils.R b/R/utils.R index dd88a1f2..9218f185 100644 --- a/R/utils.R +++ b/R/utils.R @@ -800,7 +800,7 @@ send_email <- from = from, to = to, subject = subject, - body = html_data, + body = body, smtp = list(host.name = host_name), html = html, inline = inline, From 32ab629676b3fba5010c4aebab65c444bd095a75 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:43:09 +0200 Subject: [PATCH 13/24] Apply suggestions from code review Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/utils.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index 0c1e2150..a4dc7570 100644 --- a/R/utils.R +++ b/R/utils.R @@ -761,9 +761,9 @@ calc_sd <- function(x) { #' send email by wrapping `mailR::send.mail` #' #' @param body a character containing the body content of the email -#' @param to a string with the recipient -#' @param subject a string with the subject -#' @param from a string with the sender +#' @param subject A string with the subject of the email +#' @param to a string with the recipient's email address +#' @param string with the sender's email address #' @param html a logical flag indicating whether the body of the email should be parsed as HTML #' @param inline a logical flag indicating whether images in the HTML file should be embedded inline #' @param host_name a string with the hostname @@ -787,7 +787,7 @@ send_email <- checkmate::assert_from(from, min.chars = 1) checkmate::assert_flag(html) checkmate::assert_flag(inline) - checkmate::assert_from(host_name, min.chars = 1) + checkmate::assert_string(host_name, min.chars = 1) if (!is.null(attached_files)) { checkmate::assert_character(attached_files) checkmate::assert_file_exists(attached_files) From 0ab4bd3555ec892dcd7c4756394515261a008e2f Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Fri, 18 Oct 2024 10:44:26 +0200 Subject: [PATCH 14/24] doc: update docs --- NAMESPACE | 1 + R/utils.R | 2 +- man/notify_if_duplicates.Rd | 34 ++++++++++++++++++++++++++++++++++ man/send_email.Rd | 27 ++++++++++++++++----------- 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 man/notify_if_duplicates.Rd diff --git a/NAMESPACE b/NAMESPACE index f1e1b916..5da1de1c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -88,6 +88,7 @@ export(merge_assay) export(merge_metadata) export(modifyData) export(mrowData) +export(notify_if_duplicates) export(predict_conc_from_efficacy) export(predict_efficacy_from_conc) export(prettify_flat_metrics) diff --git a/R/utils.R b/R/utils.R index a4dc7570..ca62904e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -763,7 +763,7 @@ calc_sd <- function(x) { #' @param body a character containing the body content of the email #' @param subject A string with the subject of the email #' @param to a string with the recipient's email address -#' @param string with the sender's email address +#' @param from string with the sender's email address #' @param html a logical flag indicating whether the body of the email should be parsed as HTML #' @param inline a logical flag indicating whether images in the HTML file should be embedded inline #' @param host_name a string with the hostname diff --git a/man/notify_if_duplicates.Rd b/man/notify_if_duplicates.Rd new file mode 100644 index 00000000..281d4cde --- /dev/null +++ b/man/notify_if_duplicates.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/duplicates.R +\name{notify_if_duplicates} +\alias{notify_if_duplicates} +\title{send email if assay data.table contains duplicated rows} +\usage{ +notify_if_duplicates( + dt, + by = "email", + assay_name = "unknown", + preview_max_numb = 4, + metadata = NULL +) +} +\arguments{ +\item{dt}{data.table with assay data} + +\item{by}{charvec with notification methods (currently 'email' supported)} + +\item{assay_name}{string with the name of the assay} + +\item{preview_max_numb}{number of rows to preview if duplicates found} + +\item{metadata}{list with the additional metadata to send via email} +} +\description{ +An auxiliary function to send email if duplicated rows in assay data.table are found +} +\examples{ +sdata <- get_synthetic_data("finalMAE_small") +smetrics_data <- convert_se_assay_to_dt(sdata[[1]], "Metrics") +throw_msg_if_duplicates(smetrics_data, assay_name = "Metrics", msg_f = futile.logger::flog.info) +} +\keyword{duplicates} diff --git a/man/send_email.Rd b/man/send_email.Rd index 903903d0..b2ac9ce1 100644 --- a/man/send_email.Rd +++ b/man/send_email.Rd @@ -5,27 +5,32 @@ \title{send email by wrapping \code{mailR::send.mail}} \usage{ send_email( - html_data, - to, + body, subject, - from = get_env_var("EMAIL_SENDER", "gdr-support-d@gene.com"), - inline = FALSE + to = get_env_var("EMAIL_RECIPIENT"), + from = get_env_var("EMAIL_SENDER"), + html = TRUE, + inline = FALSE, + host_name = get_env_var("EMAIL_HOSTNAME"), + attached_files = NULL ) } \arguments{ -\item{to}{string with the recipient} +\item{body}{a character containing the body content of the email} -\item{subject}{string with the subject} +\item{subject}{A string with the subject of the email} -\item{from}{string with the sender} +\item{to}{a string with the recipient's email address} -\item{inline}{A boolean indicating whether images in the HTML file should be embedded inline} +\item{from}{string with the sender's email address} -\item{body}{body content of the email} +\item{html}{a logical flag indicating whether the body of the email should be parsed as HTML} -\item{html}{A boolean indicating whether the body of the email should be parsed as HTML} +\item{inline}{a logical flag indicating whether images in the HTML file should be embedded inline} -\item{attached_files}{A character with file paths to be attached to the email} +\item{host_name}{a string with the hostname} + +\item{attached_files}{a character with file paths to be attached to the email} } \description{ send email by wrapping \code{mailR::send.mail} From a48fb303b23874526d506e475bd26834d1db39bd Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:48:56 +0200 Subject: [PATCH 15/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index 45a89431..78dff788 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -222,7 +222,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ msg2 <- sprintf(" when checking uniqueness with the following set of columns: '%s'. ", toString(get_assay_req_uniq_cols(dt))) msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'", - preview_numb, + preview_max_numb, jsonlite::toJSON(dup_dt[seq(preview_numb), ])) msg <- paste0(msg, msg2, msg3) m_sbj <- "[gDR] Error - unexpected duplicates found" From 2c3781abbd1830df8890648ce13bf790a1368dd1 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:49:25 +0200 Subject: [PATCH 16/24] Update R/utils.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index ca62904e..11d6e892 100644 --- a/R/utils.R +++ b/R/utils.R @@ -784,7 +784,7 @@ send_email <- checkmate::assert_character(body) checkmate::assert_string(subject, min.chars = 1) checkmate::assert_string(to, min.chars = 1) - checkmate::assert_from(from, min.chars = 1) + checkmate::assert_string(from, min.chars = 1) checkmate::assert_flag(html) checkmate::assert_flag(inline) checkmate::assert_string(host_name, min.chars = 1) From e30455fb7e436dcff63c841dd821718ad4f8e745 Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:50:33 +0200 Subject: [PATCH 17/24] Update R/duplicates.R Co-authored-by: Bartek <32614650+bczech@users.noreply.github.com> --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index 78dff788..dd954d89 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -223,7 +223,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ toString(get_assay_req_uniq_cols(dt))) msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'", preview_max_numb, - jsonlite::toJSON(dup_dt[seq(preview_numb), ])) + jsonlite::toJSON(dup_dt[seq(preview_max_numb), ])) msg <- paste0(msg, msg2, msg3) m_sbj <- "[gDR] Error - unexpected duplicates found" From 760b98172c4db55de86dcac4f00c45d0241a452e Mon Sep 17 00:00:00 2001 From: Arek Gladki <41166437+gladkia@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:59:30 +0200 Subject: [PATCH 18/24] Update R/duplicates.R --- R/duplicates.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/duplicates.R b/R/duplicates.R index dd954d89..495a6af7 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -228,7 +228,7 @@ notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", previ m_sbj <- "[gDR] Error - unexpected duplicates found" if ("email" %in% by) { - att_l <- list(c(dup_dt = dup_dt, metadata)) + att_l <- c(list(dup_dt = dup_dt), metadata) att_f <- tempfile() qs::qsave(att_l, att_f) m_to <- get_env_var("EMAIL_RECIPIENT") From 26c2dd0bd4e4a87aa3cb79364b50159e4a47a632 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Mon, 4 Nov 2024 08:09:23 +0100 Subject: [PATCH 19/24] feat: move helper functions from gDRutils to gDRcomponents --- NAMESPACE | 2 -- R/duplicates.R | 58 ------------------------------------- R/utils.R | 48 ------------------------------ man/notify_if_duplicates.Rd | 34 ---------------------- man/send_email.Rd | 38 ------------------------ 5 files changed, 180 deletions(-) delete mode 100644 man/notify_if_duplicates.Rd delete mode 100644 man/send_email.Rd diff --git a/NAMESPACE b/NAMESPACE index 5da1de1c..046e9c27 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -88,7 +88,6 @@ export(merge_assay) export(merge_metadata) export(modifyData) export(mrowData) -export(notify_if_duplicates) export(predict_conc_from_efficacy) export(predict_efficacy_from_conc) export(prettify_flat_metrics) @@ -100,7 +99,6 @@ export(rename_DFrame) export(rename_bumpy) export(reset_env_identifiers) export(round_concentration) -export(send_email) export(set_SE_experiment_metadata) export(set_SE_experiment_raw_data) export(set_SE_fit_parameters) diff --git a/R/duplicates.R b/R/duplicates.R index 495a6af7..f2208995 100644 --- a/R/duplicates.R +++ b/R/duplicates.R @@ -186,61 +186,3 @@ throw_msg_if_duplicates <- function(dt, assay_name = "unknown", msg_f = stop, pr } } -#' send email if assay data.table contains duplicated rows -#' -#' An auxiliary function to send email if duplicated rows in assay data.table are found -#' -#' @param dt data.table with assay data -#' @param by charvec with notification methods (currently 'email' supported) -#' @param assay_name string with the name of the assay -#' @param preview_max_numb number of rows to preview if duplicates found -#' @param metadata list with the additional metadata to send via email -#' @examples -#' sdata <- get_synthetic_data("finalMAE_small") -#' smetrics_data <- convert_se_assay_to_dt(sdata[[1]], "Metrics") -#' throw_msg_if_duplicates(smetrics_data, assay_name = "Metrics", msg_f = futile.logger::flog.info) -#' @return NULL -#' @keywords duplicates -#' -#' @export -#' -notify_if_duplicates <- function(dt, by = "email", assay_name = "unknown", preview_max_numb = 4, metadata = NULL) { - - checkmate::assert_data_table(dt) - checkmate::assert_subset(by, c("email", "slack")) - checkmate::assert_string(assay_name) - checkmate::assert_number(preview_max_numb) - checkmate::assert_list(metadata, null.ok = TRUE) - - dup_dt <- get_assay_dt_duplicated_rows(dt, output = "data") - - msg <- sprintf( - "The %i out of %i rows are duplicated in the assay '%s'", - NROW(dup_dt), - NROW(dt), - assay_name) - msg2 <- sprintf(" when checking uniqueness with the following set of columns: '%s'. ", - toString(get_assay_req_uniq_cols(dt))) - msg3 <- sprintf("Here is the preview of the first %i duplicated rows in JSON format: '%s'", - preview_max_numb, - jsonlite::toJSON(dup_dt[seq(preview_max_numb), ])) - msg <- paste0(msg, msg2, msg3) - m_sbj <- "[gDR] Error - unexpected duplicates found" - - if ("email" %in% by) { - att_l <- c(list(dup_dt = dup_dt), metadata) - att_f <- tempfile() - qs::qsave(att_l, att_f) - m_to <- get_env_var("EMAIL_RECIPIENT") - stopifnot(nchar(m_to) > 0) - send_email(body = msg, subject = m_sbj, to = m_to, attached_files = att_f) - } - - if ("slack" %in% by) { - s_to <- get_env_var("EMAIL_SLACK_NOTIFICATION") - stopifnot(nchar(s_to) > 0) - send_email(body = msg, subject = m_sbj, to = s_to) - } - - - } diff --git a/R/utils.R b/R/utils.R index 728b19c4..e37b7190 100644 --- a/R/utils.R +++ b/R/utils.R @@ -762,54 +762,6 @@ calc_sd <- function(x) { } -#' send email by wrapping `mailR::send.mail` -#' -#' @param body a character containing the body content of the email -#' @param subject A string with the subject of the email -#' @param to a string with the recipient's email address -#' @param from string with the sender's email address -#' @param html a logical flag indicating whether the body of the email should be parsed as HTML -#' @param inline a logical flag indicating whether images in the HTML file should be embedded inline -#' @param host_name a string with the hostname -#' @param attached_files a character with file paths to be attached to the email -#' -#' @keywords utils -#' @export -send_email <- - function(body, - subject, - to = get_env_var("EMAIL_RECIPIENT"), - from = get_env_var("EMAIL_SENDER"), - html = TRUE, - inline = FALSE, - host_name = get_env_var("EMAIL_HOSTNAME"), - attached_files = NULL) { - - checkmate::assert_character(body) - checkmate::assert_string(subject, min.chars = 1) - checkmate::assert_string(to, min.chars = 1) - checkmate::assert_string(from, min.chars = 1) - checkmate::assert_flag(html) - checkmate::assert_flag(inline) - checkmate::assert_string(host_name, min.chars = 1) - if (!is.null(attached_files)) { - checkmate::assert_character(attached_files) - checkmate::assert_file_exists(attached_files) - } - - - mailR::send.mail( - from = from, - to = to, - subject = subject, - body = body, - smtp = list(host.name = host_name), - html = html, - inline = inline, - attach.files = attached_files - ) - } - #' safe wrapper of Sys.getenv() #' diff --git a/man/notify_if_duplicates.Rd b/man/notify_if_duplicates.Rd deleted file mode 100644 index 281d4cde..00000000 --- a/man/notify_if_duplicates.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/duplicates.R -\name{notify_if_duplicates} -\alias{notify_if_duplicates} -\title{send email if assay data.table contains duplicated rows} -\usage{ -notify_if_duplicates( - dt, - by = "email", - assay_name = "unknown", - preview_max_numb = 4, - metadata = NULL -) -} -\arguments{ -\item{dt}{data.table with assay data} - -\item{by}{charvec with notification methods (currently 'email' supported)} - -\item{assay_name}{string with the name of the assay} - -\item{preview_max_numb}{number of rows to preview if duplicates found} - -\item{metadata}{list with the additional metadata to send via email} -} -\description{ -An auxiliary function to send email if duplicated rows in assay data.table are found -} -\examples{ -sdata <- get_synthetic_data("finalMAE_small") -smetrics_data <- convert_se_assay_to_dt(sdata[[1]], "Metrics") -throw_msg_if_duplicates(smetrics_data, assay_name = "Metrics", msg_f = futile.logger::flog.info) -} -\keyword{duplicates} diff --git a/man/send_email.Rd b/man/send_email.Rd deleted file mode 100644 index b2ac9ce1..00000000 --- a/man/send_email.Rd +++ /dev/null @@ -1,38 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{send_email} -\alias{send_email} -\title{send email by wrapping \code{mailR::send.mail}} -\usage{ -send_email( - body, - subject, - to = get_env_var("EMAIL_RECIPIENT"), - from = get_env_var("EMAIL_SENDER"), - html = TRUE, - inline = FALSE, - host_name = get_env_var("EMAIL_HOSTNAME"), - attached_files = NULL -) -} -\arguments{ -\item{body}{a character containing the body content of the email} - -\item{subject}{A string with the subject of the email} - -\item{to}{a string with the recipient's email address} - -\item{from}{string with the sender's email address} - -\item{html}{a logical flag indicating whether the body of the email should be parsed as HTML} - -\item{inline}{a logical flag indicating whether images in the HTML file should be embedded inline} - -\item{host_name}{a string with the hostname} - -\item{attached_files}{a character with file paths to be attached to the email} -} -\description{ -send email by wrapping \code{mailR::send.mail} -} -\keyword{utils} From ec77310ddfd1c944ab7b997dbbee29a9343d9391 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Mon, 4 Nov 2024 08:11:20 +0100 Subject: [PATCH 20/24] chore: update news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index ca240930..982fb8e6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,5 @@ ## gDRutils 1.3.18 - 2024-11-04 -* add notification helpers +* add `get_env_var` helper ## gDRutils 1.3.17 - 2024-10-24 * make average_biological_replicates_dt more restrictive - fewer columns to check for duplicates From ed15219f4acbf3cb4e57518c6b60d3bfe408464d Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Mon, 4 Nov 2024 17:47:28 +0100 Subject: [PATCH 21/24] doc: update docs --- R/utils.R | 2 +- man/get_env_var.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index e37b7190..a811117d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -771,7 +771,7 @@ calc_sd <- function(x) { #' #' @param x string with the name of the environemntal variable #' @param ... additional params for Sys.getenev -#' @keywords utils +#' @keywords package_utils # #' @export #' @return sanitized value of the env variable diff --git a/man/get_env_var.Rd b/man/get_env_var.Rd index 50a90b42..ef666a92 100644 --- a/man/get_env_var.Rd +++ b/man/get_env_var.Rd @@ -19,4 +19,4 @@ So far the helper is needed to handle env vars containing \code{:} for which the backslash is automatically added in some contexts and R could not get the original value for these env vars. } -\keyword{utils} +\keyword{package_utils} From 33210dcff6cf0c09163f7b226bec09725d6136e1 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Tue, 5 Nov 2024 10:58:12 +0100 Subject: [PATCH 22/24] build: trigger build From 545fc90d07296cc9bfe2b7f16f1c25640d89deeb Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Wed, 6 Nov 2024 14:03:42 +0100 Subject: [PATCH 23/24] doc: add example --- R/utils.R | 3 +++ man/get_env_var.Rd | 3 +++ 2 files changed, 6 insertions(+) diff --git a/R/utils.R b/R/utils.R index a811117d..69ce7b59 100644 --- a/R/utils.R +++ b/R/utils.R @@ -772,6 +772,9 @@ calc_sd <- function(x) { #' @param x string with the name of the environemntal variable #' @param ... additional params for Sys.getenev #' @keywords package_utils +#' +#' @examples +#' get_env_var("HOME") # #' @export #' @return sanitized value of the env variable diff --git a/man/get_env_var.Rd b/man/get_env_var.Rd index ef666a92..c9ef9220 100644 --- a/man/get_env_var.Rd +++ b/man/get_env_var.Rd @@ -19,4 +19,7 @@ So far the helper is needed to handle env vars containing \code{:} for which the backslash is automatically added in some contexts and R could not get the original value for these env vars. } +\examples{ +get_env_var("HOME") +} \keyword{package_utils} From cdcbff16379a28ee7909f644c1257d78a0b33823 Mon Sep 17 00:00:00 2001 From: Arkadiusz Gladki Date: Thu, 7 Nov 2024 07:55:45 +0100 Subject: [PATCH 24/24] build: trigger build