@@ -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