11# ' HTML Widgets for graphical comparison
22# '
3+ # ' These widgets can be used at the console and embedded in a R
4+ # ' Markdown document or Shiny application.
5+ # '
6+ # ' The regular versions take plots or functions as \code{before} and
7+ # ' \code{after} arguments (see \code{\link{expect_doppelganger}()}
8+ # ' for details). The versions suffixed with underscores take HTML image
9+ # ' sources. These can be paths to SVG files or inlined SVG
10+ # ' images. Currently, \code{widget_diff_()} is compatible only with
11+ # ' inlined images.
12+ # '
313# ' @inheritParams htmlwidgets::createWidget
4- # ' @param before Path to the picture that is taken as reference.
5- # ' @param after Path to the picture against which the reference is
6- # ' compared.
14+ # ' @param before The picture that is taken as reference.
15+ # ' @param after The picture against which the reference is compared.
716# ' @name htmlwidgets
17+ # ' @examples
18+ # ' p1 <- function() hist(mtcars$disp)
19+ # ' p2 <- function() hist(mtcars$drat)
20+ # '
21+ # ' # You can also call these functions in a R Markdown document or
22+ # ' # in a Shiny application:
23+ # ' widget_toggle(p1, p2)
24+ # ' widget_slide(p1, p2)
25+ # ' widget_diff(p1, p2)
826NULL
927
1028# ' @rdname htmlwidgets
1129# ' @export
12- widget_toggle <- function (before , after , width = NULL , height = NULL ) {
30+ widget_toggle_ <- function (before , after , width = NULL , height = NULL ) {
1331 sources <- list (files = list (before = before , after = after ))
1432
1533 htmlwidgets :: createWidget(" vdiffr-toggle" ,
@@ -22,7 +40,7 @@ widget_toggle <- function(before, after, width = NULL, height = NULL) {
2240
2341# ' @rdname htmlwidgets
2442# ' @export
25- widget_slide <- function (before , after , width = NULL , height = NULL ) {
43+ widget_slide_ <- function (before , after , width = NULL , height = NULL ) {
2644 # Drawing a SVG into a canvas requires that the svg node has 'width'
2745 # and 'height' attributes set. Otherwise the result is oddly cropped.
2846 sources <- list (before = before , after = after )
@@ -38,7 +56,7 @@ widget_slide <- function(before, after, width = NULL, height = NULL) {
3856
3957# ' @rdname htmlwidgets
4058# ' @export
41- widget_diff <- function (before , after , width = NULL , height = NULL ) {
59+ widget_diff_ <- function (before , after , width = NULL , height = NULL ) {
4260 sources <- list (before = before , after = after )
4361 sources <- list (sources = map(sources , svg_add_dims ))
4462
@@ -49,3 +67,39 @@ widget_diff <- function(before, after, width = NULL, height = NULL) {
4967 package = " vdiffr"
5068 )
5169}
70+
71+ # ' @rdname htmlwidgets
72+ # ' @export
73+ widget_toggle <- function (before , after , width = NULL , height = NULL ) {
74+ files <- widget_svgs(before , after )
75+ widget_toggle_(files $ before , files $ after , width , height )
76+ }
77+
78+ # ' @rdname htmlwidgets
79+ # ' @export
80+ widget_slide <- function (before , after , width = NULL , height = NULL ) {
81+ files <- widget_svgs(before , after )
82+ widget_slide_(files $ before , files $ after , width , height )
83+ }
84+
85+ # ' @rdname htmlwidgets
86+ # ' @export
87+ widget_diff <- function (before , after , width = NULL , height = NULL ) {
88+ files <- widget_svgs(before , after )
89+ widget_diff_(files $ before , files $ after , width , height )
90+ }
91+
92+ widget_svgs <- function (before , after ) {
93+ out <- suppressMessages(list (
94+ before = svglite :: stringSVG(print_plot(before , " " )),
95+ after = svglite :: stringSVG(print_plot(after , " " ))
96+ ))
97+
98+ # widget_diff() does not work if SVG doesn't finish with newline
99+ out <- map(out , paste0 , " \n " )
100+
101+ # Inline SVGs so the widget can be easily embedded anywhere
102+ out <- map(out , as_inline_svg )
103+
104+ out
105+ }
0 commit comments