Skip to content

Commit

Permalink
Make htmlwidgets optionally resizeable
Browse files Browse the repository at this point in the history
  • Loading branch information
melff committed Dec 7, 2024
1 parent 2636d80 commit a444371
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
7 changes: 2 additions & 5 deletions pkg/R/display.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ display_data.htmlwidget <- function(x,...,
selfcontained <- getOption("htmlwidgets_selfcontained",TRUE) || embed
assets <- getOption("htmlwigets_assets",NULL)
show_button <- getOption("htmlwidgets_showbutton",FALSE)
resize <- getOption("htmlwidgets_resizeable",FALSE)

if(use_tmpdir) {
htmlfile <- tempfile(pattern = "widget",fileext = ".html")
Expand Down Expand Up @@ -138,15 +139,11 @@ display_data.htmlwidget <- function(x,...,
libdir=assets
)
if(iframe){
if_tmpl <- "
<iframe src=\"%s\" width=\"%s\" height=\"%s\" class='html-widget-iframe' style=\"border-style:none\">
</iframe>
"
width <- getOption("htmlwidget_iframe_width","100%")
height <- getOption("htmlwidget_iframe_height",600L)
if(embed)
url <- dataURI(mime="text/html",file=htmlfile)
iframe <- sprintf(if_tmpl,url,width,height)
iframe <- url2iframe(url,resize,width,height,class="htmlwidget-iframe")
res <- iframe
}
else {
Expand Down
26 changes: 21 additions & 5 deletions pkg/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,42 @@ url2iframe <- function(url,

resize_style <-"<style>
.resizer { display:flex; margin:0; padding:0; resize:both; overflow:hidden }
.resizer > .resized { flex-grow:1; margin:0; padding:0; border:0 }
.ugly { background:red; border:4px dashed black; }
.vresizer { display:flex; margin:0; padding:0; resize:vertical; overflow:hidden }
.hresizer { display:flex; margin:0; padding:0; resize:horizontal; overflow:hidden }
.resizer > .resized,
.hresizer > .resized,
.vresizer > .resized { flex-grow:1; margin:0; padding:0; border:0 }
</style>
"


if(resize) {
if(isTRUE(resize) || resize == "both") {
if_tmpl <- "<div class=\"resizer\">
<iframe src=\"%s\" width=\"%s\" height=\"%s\" class=\"%s resized\" style=\"%s\">
</iframe>
</div>
"
iframe <- sprintf(if_tmpl,url,width,height,class,style)
}
else if(resize == "vertical") {
if_tmpl <- "<div class=\"vresizer\">
<iframe src=\"%s\" width=\"%s\" height=\"%s\" class=\"%s resized\" style=\"%s\">
</iframe>
</div>
"
}
else if(resize == "horizontal") {
if_tmpl <- "<div class=\"hresizer\">
<iframe src=\"%s\" width=\"%s\" height=\"%s\" class=\"%s resized\" style=\"%s\">
</iframe>
</div>
"
}
else {
if_tmpl <- "<iframe src=\"%s\" width=\"%s\" height=\"%s\" class=\"%s\" style=\"%s\">
</iframe>
"
iframe <- sprintf(if_tmpl,url,width,height,class,style)
}
iframe <- sprintf(if_tmpl,url,width,height,class,style)

iframe
}
Expand Down

0 comments on commit a444371

Please sign in to comment.