Skip to content

Commit

Permalink
chore: move code around
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed Aug 15, 2024
1 parent 55badc7 commit 1be673b
Showing 1 changed file with 134 additions and 122 deletions.
256 changes: 134 additions & 122 deletions R/aux_save.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ NULL



#' @describeIn plot_save (internal) ggplot saving. ...
#' passes to cowplot::save_plot
#' @keywords internal
.ggplot_save_function <- function(gobject,


#' @rdname plot_save
#' @examples
#' g <- GiottoClass::createGiottoInstructions(save_plot = TRUE)
#' df <- data.frame(x = rnorm(5), y = rnorm(5))
#' g_plot <- ggplot2::ggplot(df, ggplot2::aes(x,y)) + ggplot2::geom_point()
#' all_plots_save_function(g, g_plot)
#'
#' @export
all_plots_save_function <- function(gobject,
plot_object,
save_dir = NULL,
save_folder = NULL,
Expand All @@ -56,6 +63,127 @@ NULL
limitsize = TRUE,
plot_count = NULL,
...) {
if (any("ggplot" %in% class(plot_object))) {
.ggplot_save_function(
gobject = gobject,
plot_object = plot_object,
save_dir = save_dir,
save_folder = save_folder,
save_name = save_name,
default_save_name = default_save_name,
save_format = save_format,
show_saved_plot = show_saved_plot,
ncol = ncol,
nrow = nrow,
scale = scale,
base_width = base_width,
base_height = base_height,
base_aspect_ratio = base_aspect_ratio,
units = units,
dpi = dpi,
limitsize = limitsize,
plot_count = plot_count,
...
)
} else {
.general_save_function(
gobject = gobject,
plot_object = plot_object,
save_dir = save_dir,
save_folder = save_folder,
save_name = save_name,
default_save_name = default_save_name,
save_format = save_format,
show_saved_plot = show_saved_plot,
base_width = base_width,
base_height = base_height,
base_aspect_ratio = base_aspect_ratio,
units = units,
dpi = dpi,
plot_count = plot_count,
...
)
}
}




#' @title showSaveParameters
#' @name showSaveParameters
#' @description Description of Giotto saving options,
#' links to \code{\link{all_plots_save_function}}
#' @returns Instructions on how to use the automatic plot saving options
#' within Giotto
#' @export
#' @examples
#' showSaveParameters()
showSaveParameters <- function() {
message("This is a simple guide to help you automatically saving plots. \n")
message("Importantly, defaults for all these parameters can be set at
the beginning with createGiottoInstructions() \n")
message("See
https://rubd.github.io/Giotto/articles/instructions_and_plotting.html
for more information and examples \n \n")

message("Each plotting function in Giotto has 4 important parameters for
showing and/or saving a plot: \n
- show_plot: TRUE or FALSE, show the plot to the console
- return_plot: TRUE or FALSE, return the plot to the console
(e.g. to further modify or save the plot
- save_plot: TRUE or FALSE, automatically save the plot
- save_param: a list of parameters that can be set \n")

message("The following list of parameters can be provided to save_param: \n
- save_dir: directory to save the plot to
- save_folder: if not NULL, a subfolder within save_dir that will
be created to save the plot to
- save_name: name of the plot (no extension needed, see save_format)
- save_format: picture format to use, default is .png
- ncol: number of columns for multiplots
- nrow: number of rows for multiplot
- scale: scale of plots
- base_width: width of plot
- base_height: height of plot
- base_aspect_ratio: ratio of plot
- units: plotting units (e.g. in)
- dpi: dpi for each plot if plot is in raster format\n")

message("Example: \n
plotfunction(...,
save_plot = TRUE,
save_param = list(save_name = 'favorite_name',
units = 'png'))")
}




# internals ####


#' @describeIn plot_save (internal) ggplot saving. ...
#' passes to cowplot::save_plot
#' @keywords internal
.ggplot_save_function <- function(gobject,
plot_object,
save_dir = NULL,
save_folder = NULL,
save_name = NULL,
default_save_name = "giotto_plot",
save_format = NULL,
show_saved_plot = FALSE,
ncol = 1,
nrow = 1,
scale = 1,
base_width = NULL,
base_height = NULL,
base_aspect_ratio = NULL,
units = NULL,
dpi = NULL,
limitsize = TRUE,
plot_count = NULL,
...) {
if (is.null(plot_object)) {
stop("\t there is no object to plot \t")
}
Expand Down Expand Up @@ -159,7 +287,7 @@ NULL
stop("\t there is no object to plot \t")
}
save_format <- match.arg(save_format,
choices = c("png", "tiff", "pdf", "svg")
choices = c("png", "tiff", "pdf", "svg")
)

if (any("plotly" %in% class(plot_object))) {
Expand Down Expand Up @@ -202,7 +330,7 @@ NULL

if (any("plotly" %in% class(plot_object))) {
htmlwidgets::saveWidget(plotly::as_widget(plot_object),
file = full_location
file = full_location
)
} else {
if (save_format == "png") {
Expand Down Expand Up @@ -267,122 +395,6 @@ NULL
}
}

#' @rdname plot_save
#' @examples
#' g <- GiottoClass::createGiottoInstructions(save_plot = TRUE)
#' df <- data.frame(x = rnorm(5), y = rnorm(5))
#' g_plot <- ggplot2::ggplot(df, ggplot2::aes(x,y)) + ggplot2::geom_point()
#' all_plots_save_function(g, g_plot)
#'
#' @export
all_plots_save_function <- function(gobject,
plot_object,
save_dir = NULL,
save_folder = NULL,
save_name = NULL,
default_save_name = "giotto_plot",
save_format = NULL,
show_saved_plot = FALSE,
ncol = 1,
nrow = 1,
scale = 1,
base_width = NULL,
base_height = NULL,
base_aspect_ratio = NULL,
units = NULL,
dpi = NULL,
limitsize = TRUE,
plot_count = NULL,
...) {
if (any("ggplot" %in% class(plot_object))) {
.ggplot_save_function(
gobject = gobject,
plot_object = plot_object,
save_dir = save_dir,
save_folder = save_folder,
save_name = save_name,
default_save_name = default_save_name,
save_format = save_format,
show_saved_plot = show_saved_plot,
ncol = ncol,
nrow = nrow,
scale = scale,
base_width = base_width,
base_height = base_height,
base_aspect_ratio = base_aspect_ratio,
units = units,
dpi = dpi,
limitsize = limitsize,
plot_count = plot_count,
...
)
} else {
.general_save_function(
gobject = gobject,
plot_object = plot_object,
save_dir = save_dir,
save_folder = save_folder,
save_name = save_name,
default_save_name = default_save_name,
save_format = save_format,
show_saved_plot = show_saved_plot,
base_width = base_width,
base_height = base_height,
base_aspect_ratio = base_aspect_ratio,
units = units,
dpi = dpi,
plot_count = plot_count,
...
)
}
}




#' @title showSaveParameters
#' @name showSaveParameters
#' @description Description of Giotto saving options,
#' links to \code{\link{all_plots_save_function}}
#' @returns Instructions on how to use the automatic plot saving options
#' within Giotto
#' @export
#' @examples
#' showSaveParameters()
showSaveParameters <- function() {
message("This is a simple guide to help you automatically saving plots. \n")
message("Importantly, defaults for all these parameters can be set at
the beginning with createGiottoInstructions() \n")
message("See
https://rubd.github.io/Giotto/articles/instructions_and_plotting.html
for more information and examples \n \n")

message("Each plotting function in Giotto has 4 important parameters for
showing and/or saving a plot: \n
- show_plot: TRUE or FALSE, show the plot to the console
- return_plot: TRUE or FALSE, return the plot to the console
(e.g. to further modify or save the plot
- save_plot: TRUE or FALSE, automatically save the plot
- save_param: a list of parameters that can be set \n")

message("The following list of parameters can be provided to save_param: \n
- save_dir: directory to save the plot to
- save_folder: if not NULL, a subfolder within save_dir that will
be created to save the plot to
- save_name: name of the plot (no extension needed, see save_format)
- save_format: picture format to use, default is .png
- ncol: number of columns for multiplots
- nrow: number of rows for multiplot
- scale: scale of plots
- base_width: width of plot
- base_height: height of plot
- base_aspect_ratio: ratio of plot
- units: plotting units (e.g. in)
- dpi: dpi for each plot if plot is in raster format\n")

message("Example: \n
plotfunction(...,
save_plot = TRUE,
save_param = list(save_name = 'favorite_name',
units = 'png'))")
}

0 comments on commit 1be673b

Please sign in to comment.