Skip to content

Commit 98babae

Browse files
committed
refac: move creation of interactive ggiraph graphics into a function
1 parent 3ee4517 commit 98babae

File tree

3 files changed

+101
-40
lines changed

3 files changed

+101
-40
lines changed

R/app_server.R

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,21 @@ app_server = function(input, output, session) {
3636
# create ggiraph output from saved ggplot2 outputs
3737
output$treeview = ggiraph::renderGirafe({
3838
shiny::req(input$widgetChoice)
39-
# define tooltip
40-
tooltip_css = paste0(
41-
"background-color:black;",
42-
"color:grey;",
43-
"padding:14px;",
44-
"border-radius:8px;",
45-
"font-family:\"Courier New\",monospace;"
46-
)
47-
# set options
48-
if (input$widgetChoice == "tree-mutations.rds") {
49-
girafe_options = list(
50-
ggiraph::opts_selection(css = "fill:red;"),
51-
ggiraph::opts_selection_inv(css = "fill:grey;"),
52-
ggiraph::opts_sizing(rescale = FALSE),
53-
ggiraph::opts_zoom(max = 5),
54-
ggiraph::opts_tooltip(
55-
css = tooltip_css,
56-
use_fill = FALSE
57-
)
58-
)
59-
} else {
60-
girafe_options = list(
61-
ggiraph::opts_selection(type = "single"),
62-
ggiraph::opts_sizing(rescale = FALSE),
63-
ggiraph::opts_zoom(max = 5),
64-
ggiraph::opts_tooltip(
65-
css = tooltip_css,
66-
use_fill = FALSE
67-
)
68-
)
69-
}
39+
7040
# set size
7141
w = shinybrowser::get_width() / 72
7242
h = (1800 - 40) / 72
73-
# make tree
74-
suppressWarnings(
75-
ggiraph::girafe(
76-
ggobj = imported_ggtree(),
77-
width_svg = w,
78-
height_svg = h,
79-
options = girafe_options
80-
)
43+
44+
create_girafe(
45+
ggobj = imported_ggtree(),
46+
widget_choice = input$widgetChoice,
47+
width_svg = w,
48+
height_svg = h,
49+
suppress_warnings = TRUE
8150
)
8251
}) %>%
8352
shiny::bindCache(input$widgetChoice)
8453

85-
8654
# Mutation colouring ------------------------------------------------------
8755

8856
# disable dropdown unless mutation treeview

R/ggiraph.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#' Convert a ggplot object into an interactive {ggiraph} object
2+
#'
3+
#' @param ggobj The ggplot2/ggtree object.
4+
#' @param widget_choice Scalar character. Describes the type of plot that is contained in
5+
#' `ggobj`. Typically a file basename that includes the file extension, of the form "tree-XXX.rds"
6+
#' (for `{ggtree}` objects) or "sina-XXX.rds" (for `{ggplot2}` scatter plots).
7+
#' @param width_svg,height_svg Scalar numeric. The width/height of the output plot.
8+
#' @param suppress_warnings Scalar logical. Should warnings from `ggiraph::girafe()` be printed
9+
#' to the console?
10+
create_girafe = function(
11+
ggobj,
12+
widget_choice,
13+
width_svg,
14+
height_svg,
15+
suppress_warnings = FALSE) {
16+
# define tooltip
17+
tooltip_css = paste0(
18+
"background-color:black;",
19+
"color:grey;",
20+
"padding:14px;",
21+
"border-radius:8px;",
22+
"font-family:\"Courier New\",monospace;"
23+
)
24+
25+
# set options
26+
if (widget_choice == "tree-mutations.rds") {
27+
girafe_options = list(
28+
ggiraph::opts_selection(css = "fill:red;"),
29+
ggiraph::opts_selection_inv(css = "fill:grey;"),
30+
ggiraph::opts_sizing(rescale = FALSE),
31+
ggiraph::opts_zoom(max = 5),
32+
ggiraph::opts_tooltip(
33+
css = tooltip_css,
34+
use_fill = FALSE
35+
)
36+
)
37+
} else {
38+
girafe_options = list(
39+
ggiraph::opts_selection(type = "single"),
40+
ggiraph::opts_sizing(rescale = FALSE),
41+
ggiraph::opts_zoom(max = 5),
42+
ggiraph::opts_tooltip(
43+
css = tooltip_css,
44+
use_fill = FALSE
45+
)
46+
)
47+
}
48+
49+
create_widget = function() {
50+
ggiraph::girafe(
51+
ggobj = ggobj,
52+
width_svg = width_svg,
53+
height_svg = height_svg,
54+
options = girafe_options
55+
)
56+
}
57+
58+
# make tree
59+
if (suppress_warnings) {
60+
suppressWarnings(create_widget())
61+
} else {
62+
create_widget()
63+
}
64+
}

man/create_girafe.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)