From 08b939ee9f457f15b3c44ee4a2350536bea90274 Mon Sep 17 00:00:00 2001 From: Carter Thunes Date: Fri, 19 Jul 2024 13:42:18 -0400 Subject: [PATCH 1/2] 215 updated tcplPlot to return ggplot object if only 1 plot --- R/tcplPlot.R | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/R/tcplPlot.R b/R/tcplPlot.R index 7942f9ad..167f7f01 100644 --- a/R/tcplPlot.R +++ b/R/tcplPlot.R @@ -58,7 +58,7 @@ #' #' ## Reset configuration #' options(conf_store) -tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.val = NULL, by = NULL, output = c("console", "pdf", "png", "jpg", "svg", "tiff"), fileprefix = paste0("tcplPlot_", Sys.Date()), multi = NULL, verbose = FALSE, nrow = NULL, ncol = NULL, dpi = 600, flags = FALSE, yuniform = FALSE, yrange=c(NA,NA)) { +tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.val = NULL, by = NULL, output = c("ggplot", "console", "pdf", "png", "jpg", "svg", "tiff"), fileprefix = paste0("tcplPlot_", Sys.Date()), multi = NULL, verbose = FALSE, nrow = NULL, ncol = NULL, dpi = 600, flags = FALSE, yuniform = FALSE, yrange=c(NA,NA)) { #variable binding conc_unit <- bmd <- resp <- compare.dat <- NULL @@ -123,18 +123,22 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare. } - - if (nrow(dat[compare == FALSE]) == 1) { - # plot single graph - # this needs to be fixed to be more succinct about users selected option - ifelse(output[1] == "console", - # tcplPlotlyplot is the user-defined function found in tcplPlot.R file used to connect tcpl and plotly packages - # tcplggplot is the user-defined function found in tcplPlot.R file used to connect tcpl and ggplot2 packages - return(tcplPlotlyPlot(dat, lvl)), - return(ggsave(filename=paste0(fileprefix,"_",paste0(ifelse(type=="mc",dat$m4id,dat$s2id), collapse = "_"),".",output), - plot= if(is.null(compare.val)) tcplggplot(dat,verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) else tcplggplotCompare(dat[compare == FALSE],dat[compare == TRUE],verbose = verbose, lvl = lvl, flags = flags, yrange = yrange), width = 7, height = 5, dpi=dpi)) - ) + if (output[1] == "console") { + # tcplPlotlyplot is the user-defined function found in tcplPlot.R file used to connect tcpl and plotly packages + return(tcplPlotlyPlot(dat, lvl)) + } else { + # tcplggplot is the user-defined function found in tcplPlot.R file used to connect tcpl and ggplot2 packages + ggplot <- if(is.null(compare.val)) tcplggplot(dat,verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) else + tcplggplotCompare(dat[compare == FALSE],dat[compare == TRUE],verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) + if (output[1] == "ggplot") { + if (verbose) message("ggplot object and verbose table arranged into gtable object. To work with a simple ggplot object, set `verbose = FALSE`.") + return(ggplot) + } + ggsave(filename = paste0(fileprefix, "_", paste0(ifelse(type=="mc", dat$m4id, dat$s2id), collapse = "_"), ".", output), + plot = ggplot, width = 7, height = 5, dpi=dpi) + return(ggplot) + } } else { split_dat <- list(dat) if(!is.null(by)){ @@ -1441,7 +1445,7 @@ tcplggplotCompare <- function(dat, compare.dat, lvl = 5, verbose = FALSE, flags t <- tableGrob(details, rows = c("A", "B")) ifelse(verbose, return(arrangeGrob(gg, t, nrow = 1, widths = 2:1)), - return(arrangeGrob(gg)) + return(gg) ) } else { details <- tibble(Hitcall = c(dat$hitc, compare.dat$hitc)) From 9bd02085978faddaadd6a2619abcea2dfc3b1e8c Mon Sep 17 00:00:00 2001 From: Carter Thunes Date: Fri, 19 Jul 2024 14:21:54 -0400 Subject: [PATCH 2/2] 215 add more than 1 concentration-series check for new "ggplot" output --- R/tcplPlot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tcplPlot.R b/R/tcplPlot.R index 167f7f01..bbca9d28 100644 --- a/R/tcplPlot.R +++ b/R/tcplPlot.R @@ -114,7 +114,7 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare. nrow = ncol = 1 } # error message for output="console" and multi=FALSE to avoid multiple plots in console - if(nrow(dat[compare == FALSE]) != 1 && output == "console" && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(dat)) + if(nrow(dat[compare == FALSE]) != 1 && output %in% c("ggplot", "console") && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(dat)) if(is.null(nrow)){ nrow <- ifelse(verbose,2,2) }