Skip to content

Commit d0614ae

Browse files
committed
feat: adds internal function to change dimensions of chunk_outputs
1 parent a19ef37 commit d0614ae

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

R/tm_a_pca.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,15 @@ 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+
})
1091+
10831092
# tables ----
10841093
output$tbl_importance <- renderTable(
10851094
expr = {
@@ -1141,6 +1150,6 @@ srv_a_pca <- function(id, data, dat, plot_height, plot_width, ggplot2_args, deco
11411150
verbatim_content = source_code_r,
11421151
title = "R Code for PCA"
11431152
)
1144-
decorated_output_q
1153+
decorated_output_dims_q
11451154
})
11461155
}

R/utils.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,54 @@ select_decorators <- function(decorators, scope) {
363363
list()
364364
}
365365
}
366+
367+
#' Set the attributes of the last chunk outputs
368+
#' @param data (`teal_card`) object to modify.
369+
#' @param attributes (`list`) of attributes to set on the last chunk outputs.
370+
#' @param n (`integer(1)`) number of the last element of `teal_card` to modify.
371+
#' it will only change `chunk_output` objects.
372+
#' @param inner_classes (`character`) classes within `chunk_output` that should be modified.
373+
#' This can be used to only change `recordedplot`, `ggplot2` or other type of objects.
374+
#' @keywords internal
375+
modify_last_chunk_outputs_attributes <- function(teal_card,
376+
attributes,
377+
n = 1,
378+
inner_classes = NULL,
379+
quiet = FALSE) {
380+
checkmate::assert_class(teal_card, "teal_card")
381+
checkmate::assert_list(attributes, names = "unique")
382+
checkmate::assert_int(n, lower = 1)
383+
checkmate::assert_character(inner_classes, null.ok = TRUE)
384+
checkmate::assert_flag(quiet)
385+
386+
if (!inherits(teal_card[[length(teal_card)]], "chunk_output")) {
387+
if (!quiet) {
388+
warning("The last element of the `teal_card` is not a `chunk_output` object. No attributes were modified.")
389+
}
390+
return(teal_card)
391+
}
392+
393+
for (ix in seq_len(length(teal_card))) {
394+
if (ix > n) {
395+
break
396+
}
397+
current_ix <- length(teal_card) + 1 - ix
398+
if (!inherits(teal_card[[current_ix]], "chunk_output")) {
399+
if (!quiet) {
400+
warning("The ", ix, " to last element of the `teal_card` is not a `chunk_output` object. Skipping any further modifications.")
401+
}
402+
return(teal_card)
403+
}
404+
405+
if (length(inner_classes) > 0 && !checkmate::test_multi_class(teal_card[[current_ix]][[1]], inner_classes)) {
406+
next
407+
}
408+
409+
attributes(teal_card[[current_ix]]) <- modifyList(
410+
attributes(teal_card[[current_ix]]),
411+
attributes
412+
)
413+
}
414+
415+
teal_card
416+
}

man/modify_last_chunk_outputs_attributes.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.

tests/testthat/test-utils.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
testthat::describe("modify_last_chunk_outputs_attributes", {
2+
card <- teal.reporter::teal_card(
3+
"# Header",
4+
"Some text",
5+
structure(list(2), class = "chunk_output"),
6+
structure(list("1"), class = "chunk_output")
7+
)
8+
9+
it("changes last chunk output with default parameters", {
10+
new_card <- modify_last_chunk_outputs_attributes(teal_card, list(dev.height = 200))
11+
testthat::expect_equal(attributes(new_card[[4]]), list(class = "chunk_output", dev.height = 200))
12+
testthat::expect_equal(attributes(new_card[[3]]), list(class = "chunk_output"))
13+
})
14+
15+
it("changes last 2 chunks", {
16+
new_card <- modify_last_chunk_outputs_attributes(card, list(dev.height = 200), n = 2)
17+
testthat::expect_equal(attributes(new_card[[4]]), list(class = "chunk_output", dev.height = 200))
18+
testthat::expect_equal(attributes(new_card[[3]]), list(class = "chunk_output", dev.height = 200))
19+
})
20+
21+
it("only changes the numeric chunk_outputs", {
22+
new_card <- modify_last_chunk_outputs_attributes(card, list(dev.height = 200), n = 2, inner_classes = "numeric")
23+
testthat::expect_equal(attributes(new_card[[3]]), list(class = "chunk_output", dev.height = 200))
24+
testthat::expect_equal(attributes(new_card[[4]]), list(class = "chunk_output"))
25+
})
26+
27+
it("throws warning when last chunk is not chunk_output", {
28+
testthat::expect_warning(
29+
modify_last_chunk_outputs_attributes(c(card, "yada"), list(new_attr = TRUE)),
30+
"The last element of the `teal_card` is not a `chunk_output` object. No attributes were modified."
31+
)
32+
})
33+
34+
it("throws warning when second to last chunk is not chunk_output", {
35+
card_modified <- c(card[c(1, 2, 3)], "bla", card[[4]])
36+
testthat::expect_warning(
37+
modify_last_chunk_outputs_attributes(card_modified, n = 3, list(new_attr = TRUE)),
38+
"The 2 to last element of the `teal_card` is not a `chunk_output` object. Skipping any further modifications."
39+
)
40+
})
41+
42+
it("modifies all elements up until the first non-chunk output", {
43+
card_modified <- c(card[c(1, 2, 3)], "bla", card[[4]])
44+
new_card <- modify_last_chunk_outputs_attributes(card_modified, n = 3, list(new_attr = TRUE), quiet = TRUE)
45+
testthat::expect_equal(attributes(new_card[[5]]), list(class = "chunk_output", new_attr = TRUE))
46+
testthat::expect_null(attributes(new_card[[4]]))
47+
testthat::expect_equal(attributes(new_card[[3]]), list(class = "chunk_output"))
48+
})
49+
})

0 commit comments

Comments
 (0)