From 461867c0dbcf0a3331f2b00b05da3bf90448ebb4 Mon Sep 17 00:00:00 2001 From: Russ Hyde Date: Mon, 11 Mar 2024 14:29:22 +0000 Subject: [PATCH] refac: width/height of tree-views is specified in treeview() rather than in (non-exported) save_trees() This makes the function call for save_trees() more similar to save_sina_plot(), where the width and height are specified as function arguments. --- R/plot_tree.R | 11 +++++------ R/treeview.R | 13 +++++++++---- inst/WORDLIST | 3 +++ man/save_trees.Rd | 10 ++++++---- tests/testthat/test-plot_tree.R | 8 ++++---- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/R/plot_tree.R b/R/plot_tree.R index 736f391..e162582 100644 --- a/R/plot_tree.R +++ b/R/plot_tree.R @@ -94,25 +94,24 @@ create_trees <- function(ggtree_data, #' object will be placed in an \code{rds} file. For \code{html}, a \code{htmlwidget} will be #' placed in a \code{html} file. #' @param include_date Boolean. Should the file-paths include the current date? -#' @inheritParams create_trees +#' @param height_svg,width_svg Scalar numeric. Height/width of the generated plots. Passed on to +#' `ggplot2::ggsave(..., height, width)` and `ggiraph::girafe(..., height_svg, width_svg)`. #' #' @return A named vector containing the file paths that were generated. save_trees <- function(tree_list, branch_col, - n_leaves, output_dir, output_format = c("rds", "html"), - include_date = FALSE) { + include_date = FALSE, + height_svg = NULL, + width_svg = NULL) { output_format <- match.arg(output_format, several.ok = TRUE) required_filetypes <- c( "noninteractive", c(rds = "interactive_rds", html = "interactive_html")[output_format] ) - height_svg <- max(14, floor(n_leaves / 10)) - width_svg <- 16 - basename_prefix <- if (include_date) { glue::glue("tree-{branch_col}-{Sys.Date()}") } else { diff --git a/R/treeview.R b/R/treeview.R index bf4b1d1..609d429 100644 --- a/R/treeview.R +++ b/R/treeview.R @@ -250,6 +250,9 @@ treeview <- function(e0, } suppressWarnings({ + height_svg <- max(14, floor(n_leaves / 10)) + width_svg <- 16 + lgr_trees <- create_trees_curried( branch_col = "logistic_growth_rate", colour_limits = c(-.5, .5) @@ -257,9 +260,10 @@ treeview <- function(e0, save_trees( lgr_trees, branch_col = "logistic_growth_rate", - n_leaves = n_leaves, output_dir = output_dir, - output_format = output_format + output_format = output_format, + height_svg = height_svg, + width_svg = width_svg ) for (branch_col in setdiff(branch_cols, c("logistic_growth_rate"))) { @@ -271,9 +275,10 @@ treeview <- function(e0, save_trees( tree_list, branch_col = branch_col, - n_leaves = n_leaves, output_dir = output_dir, - output_format = output_format + output_format = output_format, + height_svg = height_svg, + width_svg = width_svg ) } }) diff --git a/inst/WORDLIST b/inst/WORDLIST index 3853463..d629804 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -22,7 +22,9 @@ foreach generalised ggiraph ggplot +ggsave ggtree +girafe grey htmlwidget htmlwidgets @@ -61,6 +63,7 @@ stringr styler sublicense summarised +svg svglite SystemRequirements testthat diff --git a/man/save_trees.Rd b/man/save_trees.Rd index a140cf7..2790177 100644 --- a/man/save_trees.Rd +++ b/man/save_trees.Rd @@ -7,10 +7,11 @@ save_trees( tree_list, branch_col, - n_leaves, output_dir, output_format = c("rds", "html"), - include_date = FALSE + include_date = FALSE, + height_svg = NULL, + width_svg = NULL ) } \arguments{ @@ -20,8 +21,6 @@ entries will be saved to an external file. As generated by \code{create_trees}.} \item{branch_col}{Scalar string. The name of the statistic that is under study here (e.g., "logistic_growth_rate" or "clock_outlier"). This name will be used in the output file paths.} -\item{n_leaves}{Scalar integer. The number of leaves in the tree.} - \item{output_dir}{Scalar string. The directory where the plots will be saved.} \item{output_format}{String (either \code{rds} or \code{html} or both). Default: both. Which @@ -30,6 +29,9 @@ object will be placed in an \code{rds} file. For \code{html}, a \code{htmlwidget placed in a \code{html} file.} \item{include_date}{Boolean. Should the file-paths include the current date?} + +\item{height_svg, width_svg}{Scalar numeric. Height/width of the generated plots. Passed on to +`ggplot2::ggsave(..., height, width)` and `ggiraph::girafe(..., height_svg, width_svg)`.} } \value{ A named vector containing the file paths that were generated. diff --git a/tests/testthat/test-plot_tree.R b/tests/testthat/test-plot_tree.R index 9ed7596..1c7eca7 100644 --- a/tests/testthat/test-plot_tree.R +++ b/tests/testthat/test-plot_tree.R @@ -33,7 +33,7 @@ describe("save_trees", { created_files <- save_trees( tree_list, - branch_col = branch_col, output_dir = td, n_leaves = 100 + branch_col = branch_col, output_dir = td, width_svg = 16, height_svg = 14 ) expect_mapequal(created_files, expected_files) @@ -54,7 +54,7 @@ describe("save_trees", { created_files <- save_trees( tree_list, - branch_col = branch_col, output_dir = td, n_leaves = 100, + branch_col = branch_col, output_dir = td, width_svg = 16, height_svg = 14, output_format = "html" ) @@ -77,7 +77,7 @@ describe("save_trees", { created_files <- save_trees( tree_list, - branch_col = branch_col, output_dir = td, n_leaves = 100, + branch_col = branch_col, output_dir = td, width_svg = 16, height_svg = 14, output_format = "rds" ) @@ -100,7 +100,7 @@ describe("save_trees", { created_files <- save_trees( tree_list, - branch_col = branch_col, output_dir = td, n_leaves = 100, include_date = TRUE + branch_col = branch_col, output_dir = td, width_svg = 16, height_svg = 14, include_date = TRUE ) expect_mapequal(created_files, expected_files)