Skip to content

Commit cc6504a

Browse files
committed
Add fill arguments to shinyWidgetOutput() and sizingPolicy()
1 parent 9827b5f commit cc6504a

File tree

7 files changed

+132
-154
lines changed

7 files changed

+132
-154
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
# NOTE: This workflow is overkill for most R packages
2-
# check-standard.yaml is likely a better choice
3-
# usethis::use_github_action("check-standard") will install it.
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
43
#
5-
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
6-
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
4+
# NOTE: This workflow is overkill for most R packages and
5+
# check-standard.yaml is likely a better choice.
6+
# usethis::use_github_action("check-standard") will install it.
77
on:
88
push:
9-
branches:
10-
- master
9+
branches: [main, master]
1110
pull_request:
12-
branches:
13-
- master
11+
branches: [main, master]
1412

1513
name: R-CMD-check
1614

@@ -26,79 +24,33 @@ jobs:
2624
config:
2725
- {os: macOS-latest, r: 'release'}
2826
- {os: windows-latest, r: 'release'}
29-
- {os: windows-latest, r: '3.6'}
30-
- {os: ubuntu-16.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest", http-user-agent: "R/4.0.0 (ubuntu-16.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
31-
- {os: ubuntu-16.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
32-
- {os: ubuntu-16.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
33-
- {os: ubuntu-16.04, r: '3.5', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
34-
- {os: ubuntu-16.04, r: '3.4', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
35-
- {os: ubuntu-16.04, r: '3.3', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
27+
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
28+
- {os: ubuntu-latest, r: 'release'}
29+
- {os: ubuntu-latest, r: 'oldrel-1'}
30+
- {os: ubuntu-latest, r: 'oldrel-2'}
31+
- {os: ubuntu-latest, r: 'oldrel-3'}
32+
- {os: ubuntu-latest, r: 'oldrel-4'}
3633

3734
env:
38-
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
39-
RSPM: ${{ matrix.config.rspm }}
4035
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
36+
R_KEEP_PKG_SOURCE: yes
4137

4238
steps:
4339
- uses: actions/checkout@v2
4440

45-
- uses: r-lib/actions/setup-r@master
41+
- uses: r-lib/actions/setup-pandoc@v2
42+
43+
- uses: r-lib/actions/setup-r@v2
4644
with:
4745
r-version: ${{ matrix.config.r }}
4846
http-user-agent: ${{ matrix.config.http-user-agent }}
47+
use-public-rspm: true
4948

50-
- uses: r-lib/actions/setup-pandoc@master
51-
52-
- name: Query dependencies
53-
run: |
54-
install.packages('remotes')
55-
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
56-
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
57-
shell: Rscript {0}
58-
59-
- name: Cache R packages
60-
if: runner.os != 'Windows'
61-
uses: actions/cache@v2
49+
- uses: r-lib/actions/setup-r-dependencies@v2
6250
with:
63-
path: ${{ env.R_LIBS_USER }}
64-
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
65-
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
66-
67-
- name: Install system dependencies
68-
if: runner.os == 'Linux'
69-
run: |
70-
while read -r cmd
71-
do
72-
eval sudo $cmd
73-
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "16.04"))')
74-
75-
- name: Install dependencies
76-
run: |
77-
remotes::install_deps(dependencies = TRUE)
78-
remotes::install_cran("rcmdcheck")
79-
shell: Rscript {0}
80-
81-
- name: Session info
82-
run: |
83-
options(width = 100)
84-
pkgs <- installed.packages()[, "Package"]
85-
sessioninfo::session_info(pkgs, include_base = TRUE)
86-
shell: Rscript {0}
87-
88-
- name: Check
89-
env:
90-
_R_CHECK_CRAN_INCOMING_: false
91-
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
92-
shell: Rscript {0}
93-
94-
- name: Show testthat output
95-
if: always()
96-
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
97-
shell: bash
51+
extra-packages: any::rcmdcheck
52+
needs: check
9853

99-
- name: Upload check results
100-
if: failure()
101-
uses: actions/upload-artifact@main
54+
- uses: r-lib/actions/check-r-package@v2
10255
with:
103-
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
104-
path: check
56+
upload-snapshots: true

DESCRIPTION

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: htmlwidgets
22
Type: Package
33
Title: HTML Widgets for R
4-
Version: 1.5.4.9000
4+
Version: 1.5.4.9001
55
Authors@R: c(
66
person("Ramnath", "Vaidyanathan", role = c("aut", "cph")),
77
person("Yihui", "Xie", role = c("aut")),
@@ -19,7 +19,7 @@ License: MIT + file LICENSE
1919
VignetteBuilder: knitr
2020
Imports:
2121
grDevices,
22-
htmltools (>= 0.3),
22+
htmltools (>= 0.5.3.9001),
2323
jsonlite (>= 0.9.16),
2424
yaml
2525
Suggests:
@@ -30,3 +30,6 @@ Enhances: shiny (>= 1.1)
3030
URL: https://github.com/ramnathv/htmlwidgets
3131
BugReports: https://github.com/ramnathv/htmlwidgets/issues
3232
RoxygenNote: 7.2.1
33+
Encoding: UTF-8
34+
Remotes:
35+
rstudio/htmltools

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ htmlwidgets 1.5.4.9000
33

44
### Breaking change
55

6-
* `shinyWidgetOutput()`'s `reportSize` argument now defaults to `TRUE`. This way, calling `shiny::getCurrentOutputInfo()` inside a `shinyRenderWidget()` context will report the current height and width of the widget.
6+
* `shinyWidgetOutput()`'s `reportSize` argument now defaults to `TRUE`. This way, calling `shiny::getCurrentOutputInfo()` inside a `shinyRenderWidget()` context will report the current height and width of the widget.
7+
* `shinyWidgetOutput()` (and `as.tags.htmlwidget()`) no longer unnecessarily wraps the return value in an additional `tagList()`. (#442)
78

89
### Improvements
910

11+
* `shinyWidgetOutput()` and `sizingPolicy()` both gain a new `fill` parameter. When `TRUE`, the widget is allowed to grow/shrink to fit it's parent container so long as that parent has a defined height and has been marked with `htmltools::asFillContainer()` (https://github.com/rstudio/htmltools/pull/343). The primary motivation for this is to allow widgets to grow and shrink (by default) when used [inside `bslib::card_body_fill()`](https://rstudio.github.io/bslib/articles/cards.html#responsive-sizing) (#442)
1012
* Closed #433: `saveWidget()` no longer throw deprecation warning when pandoc 2.19 or higher is used.
1113

1214
htmlwidgets 1.5.4

R/htmlwidgets.R

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -170,62 +170,53 @@ addHook <- function(x, hookName, jsCode, data = NULL) {
170170

171171
toHTML <- function(x, standalone = FALSE, knitrOptions = NULL) {
172172

173+
x$id <- x$elementId %||% paste("htmlwidget", createWidgetId(), sep = "-")
174+
173175
sizeInfo <- resolveSizing(x, x$sizingPolicy, standalone = standalone, knitrOptions = knitrOptions)
174176

175-
if (!is.null(x$elementId))
176-
id <- x$elementId
177-
else
178-
id <- paste("htmlwidget", createWidgetId(), sep="-")
177+
name <- class(x)[1]
178+
package <- attr(x, "package")
179179

180-
w <- validateCssUnit(sizeInfo$width)
181-
h <- validateCssUnit(sizeInfo$height)
180+
html <- widget_html(
181+
name, package, id = x$id,
182+
style = css(
183+
width = validateCssUnit(sizeInfo$width),
184+
height = validateCssUnit(sizeInfo$height)
185+
),
186+
class = paste(name, "html-widget"),
187+
width = sizeInfo$width,
188+
height = sizeInfo$height
189+
)
182190

183-
# create a style attribute for the width and height
184-
style <- paste(
185-
"width:", w, ";",
186-
"height:", h, ";",
187-
sep = "")
191+
if (sizeInfo$fill) {
192+
html <- asFillItem(html)
193+
}
188194

189-
x$id <- id
195+
if (!is.null(x$prepend) || !is.null(x$append)) {
196+
html <- tagList(x$append, html, x$prepend)
197+
}
190198

191-
container <- if (isTRUE(standalone)) {
192-
function(x) {
193-
div(id="htmlwidget_container", x)
194-
}
195-
} else {
196-
identity
199+
if (isTRUE(standalone)) {
200+
html <- div(id = "htmlwidget_container", html)
197201
}
198202

199-
html <- htmltools::tagList(
200-
container(
201-
htmltools::tagList(
202-
x$prepend,
203-
widget_html(
204-
name = class(x)[1],
205-
package = attr(x, "package"),
206-
id = id,
207-
style = style,
208-
class = paste(class(x)[1], "html-widget"),
209-
width = sizeInfo$width,
210-
height = sizeInfo$height
211-
),
212-
x$append
213-
)
214-
),
215-
widget_data(x, id),
203+
html <- tagList(
204+
html, widget_data(x, x$id),
216205
if (!is.null(sizeInfo$runtime)) {
217-
tags$script(type="application/htmlwidget-sizing", `data-for` = id,
206+
tags$script(
207+
type = "application/htmlwidget-sizing",
208+
`data-for` = x$id,
218209
toJSON(sizeInfo$runtime)
219210
)
220211
}
221212
)
222-
html <- htmltools::attachDependencies(html,
223-
c(widget_dependencies(class(x)[1], attr(x, 'package')),
224-
x$dependencies)
225-
)
226213

227-
htmltools::browsable(html)
214+
deps <- c(
215+
widget_dependencies(name, package),
216+
x$dependencies
217+
)
228218

219+
browsable(attachDependencies(html, deps, append = TRUE))
229220
}
230221

231222
lookup_func <- function(name, package) {
@@ -263,7 +254,7 @@ lookup_widget_html_method <- function(name, package) {
263254
list(fn = widget_html.default, name = "widget_html.default", legacy = FALSE)
264255
}
265256

266-
widget_html <- function (name, package, id, style, class, inline = FALSE, ...) {
257+
widget_html <- function(name, package, id, style, class, inline = FALSE, ...) {
267258

268259
fn_info <- lookup_widget_html_method(name, package)
269260

@@ -451,6 +442,10 @@ createWidget <- function(name,
451442
#' is useful if you want to save an expression in a variable.
452443
#' @param cacheHint Extra information to use for optional caching using
453444
#' \code{shiny::bindCache()}.
445+
#' @param fill whether or not the returned tag should be wrapped
446+
#' [htmltools::asFillItem()] so that it's `height` is allowed to grow/shrink
447+
#' inside a tag wrapped with [htmltools::asFillContainer()] (e.g.,
448+
#' [bslib::card_body_fill()]).
454449
#'
455450
#' @return An output or render function that enables the use of the widget
456451
#' within Shiny applications.
@@ -474,34 +469,36 @@ createWidget <- function(name,
474469
#'
475470
#' @export
476471
shinyWidgetOutput <- function(outputId, name, width, height, package = name,
477-
inline = FALSE, reportSize = TRUE, reportTheme = FALSE) {
472+
inline = FALSE, reportSize = TRUE, reportTheme = FALSE,
473+
fill = TRUE) {
478474

479475
# Theme reporting requires this shiny feature
480476
# https://github.com/rstudio/shiny/pull/2740/files
481477
if (reportTheme && !is_installed("shiny", "1.4.0.9003")) {
482478
message("`reportTheme = TRUE` requires shiny v.1.4.0.9003 or higher. Consider upgrading shiny.")
483479
}
484480

485-
# generate html
486-
html <- htmltools::tagList(
487-
widget_html(
488-
name, package, id = outputId,
489-
class = paste0(
490-
name, " html-widget html-widget-output",
491-
if (reportSize) " shiny-report-size",
492-
if (reportTheme) " shiny-report-theme"
493-
),
494-
style = sprintf("width:%s; height:%s; %s",
495-
htmltools::validateCssUnit(width),
496-
htmltools::validateCssUnit(height),
497-
if (inline) "display: inline-block;" else ""
498-
), width = width, height = height
481+
tag <- widget_html(
482+
name, package, id = outputId,
483+
class = paste0(
484+
name, " html-widget html-widget-output",
485+
if (reportSize) " shiny-report-size",
486+
if (reportTheme) " shiny-report-theme"
487+
),
488+
style = css(
489+
width = validateCssUnit(width),
490+
height = validateCssUnit(height),
491+
display = if (inline) "inline-block"
499492
)
500493
)
501494

502-
# attach dependencies
503-
dependencies = widget_dependencies(name, package)
504-
htmltools::attachDependencies(html, dependencies)
495+
if (fill) {
496+
tag <- asFillItem(tag)
497+
}
498+
499+
attachDependencies(
500+
tag, widget_dependencies(name, package), append = TRUE
501+
)
505502
}
506503

507504

0 commit comments

Comments
 (0)