Skip to content

Commit 3a8625c

Browse files
m7praverissimo
andauthored
wrap set_plot_dims (#914)
Signed-off-by: Marcin <[email protected]> Co-authored-by: André Veríssimo <[email protected]>
1 parent 6a4a669 commit 3a8625c

File tree

10 files changed

+48
-101
lines changed

10 files changed

+48
-101
lines changed

R/tm_a_pca.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,14 +1080,7 @@ srv_a_pca <- function(id, data, dat, plot_height, plot_width, ggplot2_args, deco
10801080
graph_align = "center"
10811081
)
10821082

1083-
decorated_output_dims_q <- reactive({
1084-
dims <- req(pws$dim())
1085-
q <- req(decorated_output_q())
1086-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1087-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1088-
)
1089-
q
1090-
})
1083+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_q)
10911084

10921085
# tables ----
10931086
output$tbl_importance <- renderTable(

R/tm_a_regression.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,14 +1021,7 @@ srv_a_regression <- function(id,
10211021
width = plot_width
10221022
)
10231023

1024-
decorated_output_dims_q <- reactive({
1025-
dims <- req(pws$dim())
1026-
q <- req(decorated_output_q())
1027-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1028-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1029-
)
1030-
q
1031-
})
1024+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_q)
10321025

10331026
output$text <- renderText({
10341027
req(iv_r()$is_valid())

R/tm_g_association.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,7 @@ srv_tm_g_association <- function(id,
542542
width = plot_width
543543
)
544544

545-
decorated_output_dims_q <- reactive({
546-
dims <- req(pws$dim())
547-
q <- req(decorated_output_grob_q())
548-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
549-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
550-
)
551-
q
552-
})
545+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_grob_q)
553546

554547
output$title <- renderText(output_q()[["title"]])
555548

R/tm_g_bivariate.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,7 @@ srv_g_bivariate <- function(id,
741741
width = plot_width
742742
)
743743

744-
decorated_output_dims_q <- reactive({
745-
dims <- req(pws$dim())
746-
q <- req(decorated_output_q_facets())
747-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
748-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
749-
)
750-
q
751-
})
744+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_q_facets)
752745

753746
# Render R code.
754747

R/tm_g_distribution.R

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,23 +1385,9 @@ srv_distribution <- function(id,
13851385
brushing = FALSE
13861386
)
13871387

1388-
decorated_output_dist_dims_q <- reactive({
1389-
dims <- req(pws1$dim())
1390-
q <- req(decorated_output_dist_q())
1391-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1392-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1393-
)
1394-
q
1395-
})
1388+
decorated_output_dist_dims_q <- set_plot_dims(pws1, decorated_output_dist_q)
13961389

1397-
decorated_output_qq_dims_q <- reactive({
1398-
dims <- req(pws2$dim())
1399-
q <- req(decorated_output_qq_q())
1400-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1401-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1402-
)
1403-
q
1404-
})
1390+
decorated_output_qq_dims_q <- set_plot_dims(pws2, decorated_output_qq_q)
14051391

14061392
decorated_output_q <- reactive({
14071393
tab <- req(input$tabs) # tab is NULL upon app launch, hence will crash without this statement

R/tm_g_response.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,7 @@ srv_g_response <- function(id,
594594
width = plot_width
595595
)
596596

597-
decorated_output_dims_q <- reactive({
598-
dims <- req(pws$dim())
599-
q <- req(decorated_output_plot_q())
600-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
601-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
602-
)
603-
q
604-
})
597+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_plot_q)
605598

606599
# Render R code.
607600
source_code_r <- reactive(teal.code::get_code(req(decorated_output_dims_q())))

R/tm_g_scatterplot.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,14 +1037,7 @@ srv_g_scatterplot <- function(id,
10371037
brushing = TRUE
10381038
)
10391039

1040-
decorated_output_dims_q <- reactive({
1041-
dims <- req(pws$dim())
1042-
q <- req(decorated_output_plot_q())
1043-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1044-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1045-
)
1046-
q
1047-
})
1040+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_plot_q)
10481041

10491042
output$data_table <- DT::renderDataTable({
10501043
plot_brush <- pws$brush()

R/tm_g_scatterplotmatrix.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,7 @@ srv_g_scatterplotmatrix <- function(id,
479479
width = plot_width
480480
)
481481

482-
decorated_output_dims_q <- reactive({
483-
dims <- req(pws$dim())
484-
q <- req(decorated_output_q())
485-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
486-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
487-
)
488-
q
489-
})
482+
decorated_output_dims_q <- set_plot_dims(pws, decorated_output_q)
490483

491484
# show a message if conversion to factors took place
492485
output$message <- renderText({

R/tm_missing_data.R

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,32 +1373,11 @@ srv_missing_data <- function(id,
13731373
width = plot_width
13741374
)
13751375

1376-
decorated_summary_plot_dims_q <- reactive({
1377-
dims <- req(pws1$dim())
1378-
q <- req(decorated_summary_plot_q())
1379-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1380-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1381-
)
1382-
q
1383-
})
1376+
decorated_summary_plot_dims_q <- set_plot_dims(pws1, decorated_summary_plot_q)
13841377

1385-
decorated_combination_plot_dims_q <- reactive({
1386-
dims <- req(pws2$dim())
1387-
q <- req(decorated_combination_plot_q())
1388-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1389-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1390-
)
1391-
q
1392-
})
1378+
decorated_combination_plot_dims_q <- set_plot_dims(pws2, decorated_combination_plot_q)
13931379

1394-
decorated_by_subject_plot_dims_q <- reactive({
1395-
dims <- req(pws3$dim())
1396-
q <- req(decorated_by_subject_plot_q())
1397-
teal.reporter::teal_card(q) <- modify_last_chunk_outputs_attributes(
1398-
teal.reporter::teal_card(q), list(dev.width = dims[[1]], dev.height = dims[[2]])
1399-
)
1400-
q
1401-
})
1380+
decorated_by_subject_plot_dims_q <- set_plot_dims(pws3, decorated_by_subject_plot_q)
14021381

14031382
decorated_final_q <- reactive({
14041383
sum_type <- req(input$summary_type)

R/utils.R

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ select_decorators <- function(decorators, scope) {
373373
#' This can be used to only change `recordedplot`, `ggplot2` or other type of objects.
374374
#' @importFrom utils modifyList
375375
#' @keywords internal
376-
modify_last_chunk_outputs_attributes <- function(teal_card,
377-
attributes,
378-
n = 1,
379-
inner_classes = NULL,
380-
quiet = FALSE) {
376+
set_chunk_attrs <- function(teal_card,
377+
attributes,
378+
n = 1,
379+
inner_classes = NULL,
380+
quiet = FALSE) {
381381
checkmate::assert_class(teal_card, "teal_card")
382382
checkmate::assert_list(attributes, names = "unique")
383383
checkmate::assert_int(n, lower = 1)
@@ -415,3 +415,34 @@ modify_last_chunk_outputs_attributes <- function(teal_card,
415415

416416
teal_card
417417
}
418+
419+
#' Create a reactive that sets plot dimensions on a teal_card
420+
#'
421+
#' This is a convenience function that creates a reactive expression that
422+
#' automatically sets the dev.width and dev.height attributes on the last
423+
#' chunk outputs of a teal_card based on plot dimensions from a plot widget.
424+
#'
425+
#' @param pws (`plot_widget`) plot widget that provides dimensions via `dim()` method
426+
#' @param decorated_output_q (`reactive`) reactive expression that returns a teal_card
427+
#' @param inner_classes (`character`) classes within `chunk_output` that should be modified.
428+
#' This can be used to only change `recordedplot`, `ggplot2` or other type of objects.
429+
#'
430+
#' @return A reactive expression that returns the teal_card with updated dimensions
431+
#'
432+
#' @keywords internal
433+
set_chunk_dims <- function(pws, decorated_output_q, inner_classes = NULL) {
434+
checkmate::assert_class(pws, "plot_widget")
435+
checkmate::assert_class(decorated_output_q, "reactive")
436+
checkmate::assert_character(inner_classes, null.ok = TRUE)
437+
438+
reactive({
439+
dims <- req(pws$dim())
440+
q <- req(decorated_output_q())
441+
teal.reporter::teal_card(q) <- set_chunkt_attrs(
442+
teal.reporter::teal_card(q),
443+
list(dev.width = dims[[1]], dev.height = dims[[2]]),
444+
inner_classes = inner_classes
445+
)
446+
q
447+
})
448+
}

0 commit comments

Comments
 (0)