Skip to content

Commit bc6ff58

Browse files
committed
fix: problem when dimensions is not yet known
1 parent b135b03 commit bc6ff58

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

R/tm_outliers.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,10 @@ srv_outliers <- function(id, data, outlier_var,
11011101

11021102
pws_list <- list(box_plot = box_pws, density_plot = density_pws, cumulative_plot = cum_density_pws)
11031103
decorated_final_q <- reactive({
1104-
set_chunk_dims(pws_list[[req(current_tab_r())]], decorated_q[[current_tab_r()]])()
1104+
pws <- pws_list[[req(current_tab_r())]]
1105+
req(pws$dim())
1106+
req(decorated_q[[current_tab_r()]]())
1107+
set_chunk_dims(pws, decorated_q[[current_tab_r()]])()
11051108
})
11061109

11071110
summary_table_r <- reactive({

R/utils.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,26 +425,32 @@ set_chunk_attrs <- function(teal_card,
425425
#' chunk outputs of a `teal_card` based on plot dimensions from a plot widget.
426426
#'
427427
#' @param pws (`plot_widget`) plot widget that provides dimensions via `dim()` method
428-
#' @param decorated_output_q (`reactive`) reactive expression that returns a `teal_card`
428+
#' @param q_r (`reactive`) reactive expression that returns a `teal_reporter`
429429
#' @param inner_classes (`character`) classes within `chunk_output` that should be modified.
430430
#' This can be used to only change `recordedplot`, `ggplot2` or other type of objects.
431431
#'
432432
#' @return A reactive expression that returns the `teal_card` with updated dimensions
433433
#'
434434
#' @keywords internal
435-
set_chunk_dims <- function(pws, decorated_output_q, inner_classes = NULL) {
435+
set_chunk_dims <- function(pws, q_r, inner_classes = NULL) {
436436
checkmate::assert_list(pws)
437437
checkmate::assert_names(names(pws), must.include = "dim")
438438
checkmate::assert_class(pws$dim, "reactive")
439-
checkmate::assert_class(decorated_output_q, "reactive")
439+
checkmate::assert_class(q_r, "reactive")
440440
checkmate::assert_character(inner_classes, null.ok = TRUE)
441441

442442
reactive({
443-
dims <- req(pws$dim())
444-
q <- req(decorated_output_q())
443+
pws_dim <- stats::setNames(as.list(req(pws$dim())), c("width", "height"))
444+
if (identical(pws_dim$width, "auto")) { # ignore non-numeric values (such as "auto")
445+
pws_dim$width <- NULL
446+
}
447+
if (identical(pws_dim$height, "auto")) { # ignore non-numeric values (such as "auto")
448+
pws_dim$height <- NULL
449+
}
450+
q <- req(q_r())
445451
teal.reporter::teal_card(q) <- set_chunk_attrs(
446452
teal.reporter::teal_card(q),
447-
list(dev.width = dims[[1]], dev.height = dims[[2]]),
453+
list(dev.width = pws_dim$width, dev.height = pws_dim$height),
448454
inner_classes = inner_classes
449455
)
450456
q

tests/testthat/test-utils.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,18 @@ testthat::describe("set_chunk_attrs", {
4747
testthat::expect_equal(attributes(new_card[[3]]), list(class = "chunk_output"))
4848
})
4949
})
50+
51+
testthat::describe("set_chunk_dims", {
52+
it("skips when one of the dimensions is auto string", {
53+
pws <- list(dim = shiny::reactive(list("auto", 200)))
54+
q <- teal.reporter::teal_report()
55+
teal.reporter::teal_card(q) <- teal.reporter::teal_card("## Header", structure(list(2), class = "chunk_output"))
56+
q_r <- shiny::reactive(q)
57+
58+
q_dims_r <- set_chunk_dims(pws, q_r)
59+
testthat::expect_equal(
60+
attributes(teal.reporter::teal_card(shiny::isolate(q_dims_r()))[[2]]),
61+
list(class = "chunk_output", dev.height = 200)
62+
)
63+
})
64+
})

0 commit comments

Comments
 (0)