From ef6d9e440afea79189c68bd52c25b5fde550b72e Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 11:16:55 +0100 Subject: [PATCH 1/7] logging functions added --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/global.R | 37 +++++++++++++++++++++++++++++++++++++ man/log_message.Rd | 19 +++++++++++++++++++ man/set_logger.Rd | 21 +++++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 R/global.R create mode 100644 man/log_message.Rd create mode 100644 man/set_logger.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 828acac..3fd21f4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,6 +44,7 @@ Imports: shiny, factoextra, heatmaply, shinyBS, - shinythemes + shinythemes, + log4r RoxygenNote: 6.0.1 biocViews: diff --git a/NAMESPACE b/NAMESPACE index 4da3252..dac65a7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ export(pca) export(pcaUI) export(scatterPlot) export(scatterPlotUI) +export(set_logger) export(transformation) export(transformationUI) import(data.table) diff --git a/R/global.R b/R/global.R new file mode 100644 index 0000000..3845d2f --- /dev/null +++ b/R/global.R @@ -0,0 +1,37 @@ + +wilson.globals <- new.env(parent = emptyenv()) + +#' set a log4r logger used within the package +#' +#' @param logger A logger object see \code{\link[log4r]{create.logger}}. NULL to disable logging. +#' @param token Set a unique identifier for this logger. +#' +#' @details This function will save each logger in the wilson.globals environment. Each logger is stored by the name 'logger'[token] (e.g. 'logger6b821824b0b53b1a3e8f531a34d0d6e6'). +#' @details Use onSessionEnded to clean up after logging. See \code{\link[shiny]{onFlush}}. +#' +#' @export +set_logger <- function(logger, token = NULL) { + if(is.null(logger) || is(logger, "logger")) { + assign(x = paste0("logger", token), value = logger, envir = wilson.globals) + } +} + +#' logger message convenience function +#' +#' @param message String of message to be written in log. See \code{\link[log4r]{levellog}}. +#' @param level Set priority level of the message (number or character). See \code{\link[log4r]{levellog}}. +#' @param token Use token bound to this identifier. +#' +log_message <- function(message, level = c("DEBUG", "INFO", "WARN", "ERROR", "FATAL"), token = NULL) { + logger <- get(paste0("logger", token), envir = wilson.globals) + + if(!is.null(logger)) { + switch(level, + DEBUG = log4r::debug(logger, message), + INFO = log4r::info(logger, message), + WARN = log4r::warn(logger, message), + ERROR = log4r::error(logger, message), + FATAL = log4r::fatal(logger, message) + ) + } +} diff --git a/man/log_message.Rd b/man/log_message.Rd new file mode 100644 index 0000000..d94a471 --- /dev/null +++ b/man/log_message.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/global.R +\name{log_message} +\alias{log_message} +\title{logger message convenience function} +\usage{ +log_message(message, level = c("DEBUG", "INFO", "WARN", "ERROR", "FATAL"), + token = NULL) +} +\arguments{ +\item{message}{String of message to be written in log. See \code{\link[log4r]{levellog}}.} + +\item{level}{Set priority level of the message (number or character). See \code{\link[log4r]{levellog}}.} + +\item{token}{Use token bound to this identifier.} +} +\description{ +logger message convenience function +} diff --git a/man/set_logger.Rd b/man/set_logger.Rd new file mode 100644 index 0000000..38ef957 --- /dev/null +++ b/man/set_logger.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/global.R +\name{set_logger} +\alias{set_logger} +\title{set a log4r logger used within the package} +\usage{ +set_logger(logger, token = NULL) +} +\arguments{ +\item{logger}{A logger object see \code{\link[log4r]{create.logger}}. NULL to disable logging.} + +\item{token}{Set a unique identifier for this logger.} +} +\description{ +set a log4r logger used within the package +} +\details{ +This function will save each logger in the wilson.globals environment. Each logger is stored by the name 'logger'[token] (e.g. 'logger6b821824b0b53b1a3e8f531a34d0d6e6'). + +Use onSessionEnded to clean up after logging. See \code{\link[shiny]{onFlush}}. +} From bed8aab696dd2f65c94eb5aa41b095d51a0fba1a Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:14:50 +0100 Subject: [PATCH 2/7] featureSelector; and: log messages info level --- R/and.R | 4 ++++ R/featureSelector.R | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/R/and.R b/R/and.R index 22f9b31..fc1afd0 100644 --- a/R/and.R +++ b/R/and.R @@ -254,6 +254,8 @@ and <- function(input, output, session, data, show.elements = NULL, element.grou on.exit(progress$close()) progress$set(0, message = "Apply Filter") + log_message(message = "Applying filter...", level = "INFO", token = session$token) + or.modules <- modules() step <- 0.9 / length(or.modules) @@ -280,6 +282,8 @@ and <- function(input, output, session, data, show.elements = NULL, element.grou progress$set(1) + log_message(message = "Done.", level = "INFO", token = session$token) + return(list(bool = and.selection.bool, text = unlist(or.selection.text))) }) diff --git a/R/featureSelector.R b/R/featureSelector.R index bb64b5d..80af4d4 100644 --- a/R/featureSelector.R +++ b/R/featureSelector.R @@ -143,6 +143,8 @@ featureSelector <- function(input, output, session, data, features = NULL, featu # reset row_selector shiny::observeEvent(input$reset, { + log_message(message = "Filter reset", level = "INFO", token = session$token) + value(NULL) row_selector <<- shiny::callModule(orNumeric, "row_selector", choices = choices, value = value_wrapper, label = "Select n features from the top and/or bottom of the list", stepsize = 1) }) @@ -238,6 +240,8 @@ featureSelector <- function(input, output, session, data, features = NULL, featu # first filter (and) whole set in table select <- shiny::eventReactive(eventExpr = input$select, { + log_message(message = "Filtering data", level = "INFO", token = session$token) + data <- data.r()[and_selected()$bool] }) From 1ddf004e81d4a0c36fbc5661dd72d6a935185cd3 Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:15:08 +0100 Subject: [PATCH 3/7] geneview: log messages info level --- R/geneView.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/geneView.R b/R/geneView.R index 3e71057..5d39c9f 100644 --- a/R/geneView.R +++ b/R/geneView.R @@ -159,6 +159,8 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. }) shiny::observeEvent(input$reset, { + log_message("GeneView: reset", "INFO", token = session$token) + shinyjs::reset("genes") shinyjs::reset("plotType") shinyjs::reset("groupby") @@ -253,6 +255,8 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. shinyjs::disable("download") plot <- shiny::eventReactive(input$plot, { + log_message("GeneView: computing plot...", "INFO", token = session$token) + # enable downloadButton shinyjs::enable("download") @@ -285,6 +289,7 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. ppi = size()$ppi ) + log_message("GeneView: done.", "INFO", token = session$token) progress$set(1, detail = "Return plot") return(plot) }) @@ -327,6 +332,8 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. if(plot.method == "interactive") { output$interactive <- plotly::renderPlotly({ + log_message("GeneView: render plot interactive", "INFO", token = session$token) + #progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -342,6 +349,7 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. width = shiny::reactive(plot()$width * (plot()$ppi / 2.54)), height = shiny::reactive(plot()$height * (plot()$ppi / 2.54)), { + log_message("GeneView: render plot static", "INFO", token = session$token) #progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -356,6 +364,8 @@ geneView <- function(input, output, session, data, metadata, level = NULL, plot. output$download <- shiny::downloadHandler(filename = "geneView.zip", content = function(file) { + log_message("GeneView: download", "INFO", token = session$token) + download(file = file, filename = "geneView.zip", plot = plot()$plot, width = plot()$width, height = plot()$height, ppi = plot()$ppi, ui = user_input()) }) From af109ef9e23e6311d0c94f6ce9efa06f24f57904 Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:41:57 +0100 Subject: [PATCH 4/7] global_cor_heatmap: log messages info level --- R/global_cor_heatmap.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/global_cor_heatmap.R b/R/global_cor_heatmap.R index 4ea4aea..c6c10ac 100644 --- a/R/global_cor_heatmap.R +++ b/R/global_cor_heatmap.R @@ -219,6 +219,8 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method # functionality ----------------------------------------------------------- # reset ui shiny::observeEvent(input$reset, { + log_message("Global correlation heatmap: reset", "INFO", token = session$token) + shinyjs::reset("calc") shinyjs::reset("calc_method") shinyjs::reset("distance") @@ -306,6 +308,8 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method # build plot object plot <- shiny::eventReactive(input$plot, { + log_message("Global correlation heatmap: computing plot...", "INFO", token = session$token) + # enable downloadButton shinyjs::enable("download") @@ -352,6 +356,7 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method # update progress indicator progress$set(1) + log_message("Global correlation heatmap: done.", "INFO", token = session$token) return(plot) }) @@ -361,6 +366,8 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method width = shiny::reactive(plot()$width * (plot()$ppi / 2.54)), height = shiny::reactive(plot()$height * (plot()$ppi / 2.54)), { + log_message("Global correlation heatmap: render plot static", "INFO", token = session$token) + # progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -378,6 +385,8 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method ) }else if(plot.method == "interactive") { output$interactive <- plotly::renderPlotly({ + log_message("Global correlation heatmap: render plot interactive", "INFO", token = session$token) + # progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -394,6 +403,8 @@ global_cor_heatmap <- function(input, output, session, data, types, plot.method output$download <- shiny::downloadHandler(filename = "global_correlation_heatmap.zip", content = function(file) { + log_message("Global correlation heatmap: download", "INFO", token = session$token) + download(file = file, filename = "global_correlation_heatmap.zip", plot = plot()$plot, width = plot()$width, height = plot()$height, ppi = plot()$ppi, ui = user_input()) }) From 68011f9fb8ed5024c579b21317aac4041cbf5f8e Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:42:43 +0100 Subject: [PATCH 5/7] heatmap: log messages info level --- R/heatmap.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/heatmap.R b/R/heatmap.R index c21cf92..b1454d2 100644 --- a/R/heatmap.R +++ b/R/heatmap.R @@ -207,6 +207,8 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", #reset ui shiny::observeEvent(input$reset, { + log_message("Heatmap: reset", "INFO", token = session$token) + shinyjs::reset("cluster.distance") shinyjs::reset("cluster.method") shinyjs::reset("clustering") @@ -247,6 +249,8 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", shinyjs::disable("download") plot <- shiny::eventReactive(input$plot, { + log_message("Heatmap: computing plot...", "INFO", token = session$token) + # enable downloadButton shinyjs::enable("download") @@ -282,6 +286,7 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", ) progress$set(1) + log_message("Heatmap: done.", "INFO", token = session$token) return(plot) }) @@ -292,6 +297,8 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", }) output$interactive <- plotly::renderPlotly({ + log_message("Heatmap: render plot interactive", "INFO", token = session$token) + #new progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -312,6 +319,8 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", width = shiny::reactive(plot()$width * (plot()$ppi / 2.54)), height = shiny::reactive(plot()$height * (plot()$ppi / 2.54)), { + log_message("Heatmap: render plot static", "INFO", token = session$token) + #new progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -326,6 +335,8 @@ heatmap <- function(input, output, session, data, types, plot.method = "static", output$download <- shiny::downloadHandler(filename = "heatmap.zip", content = function(file) { + log_message("Heatmap: download", "INFO", token = session$token) + download(file = file, filename = "heatmap.zip", plot = plot()$plot, width = plot()$width, height = plot()$height, ppi = plot()$ppi, ui = user_input()) }) From b5f6a3675edbca476b933e9f3d627392c977c5ca Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:42:53 +0100 Subject: [PATCH 6/7] pca: log messages info level --- R/pca.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/R/pca.R b/R/pca.R index b50a544..8a4ba43 100644 --- a/R/pca.R +++ b/R/pca.R @@ -148,6 +148,8 @@ pca <- function(input, output, session, data, types, levels = NULL, entryLabel = #reset ui shiny::observeEvent(input$reset, { + log_message("PCA: reset", "INFO", token = session$token) + shinyjs::reset("label") shinyjs::reset("dimA") shinyjs::reset("dimB") @@ -228,6 +230,8 @@ pca <- function(input, output, session, data, types, levels = NULL, entryLabel = shinyjs::disable("download") computed.data <- shiny::eventReactive(input$plot, { + log_message("PCA: computing plot...", "INFO", token = session$token) + # enable downloadButton shinyjs::enable("download") @@ -253,6 +257,8 @@ pca <- function(input, output, session, data, types, levels = NULL, entryLabel = progress$set(1) + log_message("PCA: done.", "INFO", token = session$token) + return(plot) }) @@ -272,6 +278,7 @@ pca <- function(input, output, session, data, types, levels = NULL, entryLabel = width = plot_width, height = plot_height, { + log_message("PCA: render plot", "INFO", token = session$token) computed.data()$plot }) @@ -286,6 +293,7 @@ pca <- function(input, output, session, data, types, levels = NULL, entryLabel = output$download <- shiny::downloadHandler(filename = "pca.zip", content = function(file) { + log_message("PCA: download", "INFO", token = session$token) download(file = file, filename = "pca.zip", plot = computed.data()$plot, width = plot_width() / (computed.data()$ppi / 2.54), height = plot_height() / (computed.data()$ppi / 2.54), ppi = computed.data()$ppi, ui = user_input()) }) From 79c8a8d91876899eb26d169e3176e12cdc35ce92 Mon Sep 17 00:00:00 2001 From: Schultheis Date: Wed, 24 Jan 2018 13:43:12 +0100 Subject: [PATCH 7/7] scatterplot: log messages info level --- R/scatterPlot.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/R/scatterPlot.R b/R/scatterPlot.R index 2637149..e08afcc 100644 --- a/R/scatterPlot.R +++ b/R/scatterPlot.R @@ -182,6 +182,8 @@ scatterPlot <- function(input, output, session, data, types, features = NULL, ma #reset ui shiny::observeEvent(input$reset, { + log_message("Scatterplot: reset", "INFO", token = session$token) + shinyjs::reset("density") shinyjs::reset("line") shinyjs::reset("pointsize") @@ -304,6 +306,8 @@ scatterPlot <- function(input, output, session, data, types, features = NULL, ma shinyjs::disable("download") plot <- shiny::eventReactive(input$plot, { + log_message("Scatterplot: computing plot...", "INFO", token = session$token) + #enable downloadbutton shinyjs::enable("download") @@ -349,6 +353,7 @@ scatterPlot <- function(input, output, session, data, types, features = NULL, ma ) progress$set(1) + log_message("Scatterplot: done.", "INFO", token = session$token) return(plot) }) @@ -370,11 +375,13 @@ scatterPlot <- function(input, output, session, data, types, features = NULL, ma width = shiny::reactive(plot()$width * (plot()$ppi / 2.54)), height = shiny::reactive(plot()$height * (plot()$ppi / 2.54)), { + log_message("Scatterplot: render plot static", "INFO", token = session$token) plot()$plot } ) } else if(plot.method == "interactive") { output$interactive <- plotly::renderPlotly({ + log_message("Scatterplot: render plot interactive", "INFO", token = session$token) #new progress indicator progress <- shiny::Progress$new() on.exit(progress$close()) @@ -390,6 +397,7 @@ scatterPlot <- function(input, output, session, data, types, features = NULL, ma output$download <- shiny::downloadHandler(filename = "scatterPlot.zip", content = function(file) { + log_message("Scatterplot: download", "INFO", token = session$token) download(file = file, filename = "scatterPlot.zip", plot = plot()$plot, width = plot()$width, height = plot()$height, ppi = plot()$ppi, ui = user_input()) } )