Skip to content

Commit 5738a4b

Browse files
committed
Add createWebDependency and renderDependencies functions
1 parent 38cfa46 commit 5738a4b

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export(checkboxInput)
3939
export(code)
4040
export(column)
4141
export(conditionalPanel)
42+
export(createWebDependency)
4243
export(dataTableOutput)
4344
export(dateInput)
4445
export(dateRangeInput)

R/html-deps.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
#' Create a web dependency
2+
#'
3+
#' Ensure that a file-based HTML dependency (from the htmltools package) can be
4+
#' served over Shiny's HTTP server. This function works by using
5+
#' \code{\link{addResourcePath}} to map the HTML dependency's directory to a
6+
#' URL.
7+
#'
8+
#' @param dependency A single HTML dependency object, created using
9+
#' \code{\link{htmlDependency}}. If the \code{src} value is named, then
10+
#' \code{href} and/or \code{file} names must be present.
11+
#'
12+
#' @return A single HTML dependency object that has an \code{href}-named element
13+
#' in its \code{src}.
14+
#' @export
115
createWebDependency <- function(dependency) {
216
if (is.null(dependency))
317
return(NULL)

inst/www/shared/shiny.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
else if (window.getComputedStyle) {
3838
// getComputedStyle can return null when we're inside a hidden iframe on
3939
// Firefox; don't attempt to retrieve style props in this case.
40-
// https://bugzilla.mozilla.org/show_bug.cgi?id=548397
40+
// https://bugzilla.mozilla.org/show_bug.cgi?id=548397
4141
var style = document.defaultView.getComputedStyle(el, null);
4242
if (style)
4343
x = style.getPropertyValue(styleProp);
@@ -1282,13 +1282,17 @@
12821282
});
12831283
outputBindings.register(htmlOutputBinding, 'shiny.htmlOutput');
12841284

1285-
// Render HTML in a DOM element, inserting singletons into head as needed
1286-
exports.renderHtml = function(html, el, dependencies) {
1285+
var renderDependencies = exports.renderDependencies = function(dependencies) {
12871286
if (dependencies) {
12881287
$.each(dependencies, function(i, dep) {
12891288
renderDependency(dep);
12901289
});
12911290
}
1291+
};
1292+
1293+
// Render HTML in a DOM element, inserting singletons into head as needed
1294+
exports.renderHtml = function(html, el, dependencies) {
1295+
renderDependencies(dependencies)
12921296
return singletons.renderHtml(html, el);
12931297
};
12941298

@@ -1326,18 +1330,32 @@
13261330
if (dep.stylesheet) {
13271331
var stylesheets = $.map(asArray(dep.stylesheet), function(stylesheet) {
13281332
return $("<link rel='stylesheet' type='text/css'>")
1329-
.attr("href", href + "/" + stylesheet);
1333+
.attr("href", href + "/" + escape(stylesheet));
13301334
});
13311335
$head.append(stylesheets);
13321336
}
13331337

13341338
if (dep.script) {
13351339
var scripts = $.map(asArray(dep.script), function(scriptName) {
1336-
return $("<script>").attr("src", href + "/" + scriptName);
1340+
return $("<script>").attr("src", href + "/" + escape(scriptName));
13371341
});
13381342
$head.append(scripts);
13391343
}
13401344

1345+
if (dep.attachment) {
1346+
// dep.attachment might be a single string, an array, or an object.
1347+
var attachments = dep.attachment;
1348+
if (typeof(attachments) === "string")
1349+
attachments = [attachments];
1350+
1351+
var attach = $.map(attachments, function(attachment, key) {
1352+
return $("<link rel='attachment'>")
1353+
.attr("id", dep.name + "-" + key + "-attachment")
1354+
.attr("href", href + "/" + escape(attachment));
1355+
});
1356+
$head.append(attach);
1357+
}
1358+
13411359
if (dep.head) {
13421360
var $newHead = $("<head></head>");
13431361
$newHead.html(dep.head);

man/createWebDependency.Rd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
% Generated by roxygen2 (4.0.1): do not edit by hand
2+
\name{createWebDependency}
3+
\alias{createWebDependency}
4+
\title{Create a web dependency}
5+
\usage{
6+
createWebDependency(dependency)
7+
}
8+
\arguments{
9+
\item{dependency}{A single HTML dependency object, created using
10+
\code{\link{htmlDependency}}. If the \code{src} value is named, then
11+
\code{href} and/or \code{file} names must be present.}
12+
}
13+
\value{
14+
A single HTML dependency object that has an \code{href}-named element
15+
in its \code{src}.
16+
}
17+
\description{
18+
Ensure that a file-based HTML dependency (from the htmltools package) can be
19+
served over Shiny's HTTP server. This function works by using
20+
\code{\link{addResourcePath}} to map the HTML dependency's directory to a
21+
URL.
22+
}
23+

0 commit comments

Comments
 (0)