Skip to content

Commit

Permalink
refac: width/height of tree-views is specified in treeview() rather t…
Browse files Browse the repository at this point in the history
…han 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.
  • Loading branch information
russHyde committed Apr 15, 2024
1 parent a0ff2bd commit 193227d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
11 changes: 5 additions & 6 deletions R/plot_tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions R/treeview.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,20 @@ 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)
)
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"))) {
Expand All @@ -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
)
}
})
Expand Down
3 changes: 3 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ foreach
generalised
ggiraph
ggplot
ggsave
ggtree
girafe
grey
htmlwidget
htmlwidgets
Expand Down Expand Up @@ -61,6 +63,7 @@ stringr
styler
sublicense
summarised
svg
svglite
SystemRequirements
testthat
Expand Down
10 changes: 6 additions & 4 deletions man/save_trees.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-plot_tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)

Expand All @@ -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"
)

Expand All @@ -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)
Expand Down

0 comments on commit 193227d

Please sign in to comment.