您可以使用tags http://shiny.rstudio.com/articles/tag-glossary.html将自定义 HTML 添加到您的ui.R
.
对于更复杂的输出,您可以在使用具有以下功能的应用程序时构建自定义闪亮输出ui.R
and server.R
. 这一页 http://shiny.rstudio.com/articles/building-outputs.html有关于如何对任何代码执行此操作的信息。
以下是使用您发布的 D3 示例中的 javascript 代码的示例。
该应用程序只是生成绘图,我添加了一个selectInput
您可以在其中选择它绘制的数据以显示事物如何集成。所有 javascript 代码均由 mbostock 提供。 (可以被找寻到here http://bl.ocks.org/mbostock/7607999).
我添加了闪亮的绑定部分并更改了几行以使其适应应用程序,我在上面评论了更改,以便您可以跟踪它们。
ui.R
library(shiny)
shinyUI(
fluidPage(singleton(tags$head(
#adds the d3 library needed to draw the plot
tags$script(src="http://d3js.org/d3.v3.min.js"),
#the js script holding the code to make the custom output
tags$script(src="HierarchicalEdgeBundling.js"),
#the stylesheet, paste all that was between the <style> tags from your example in the graph_style.css file
tags$link(rel = "stylesheet", type = "text/css", href = "graph_style.css")
)),
mainPanel(
#this select input allows the user to choose json files in the www directory
selectInput("data_files", "JSON files:" , as.matrix(list.files(path="www",pattern="json"))),
#this div will hold the final graph
div(id="graph", class="HierarchicalEdgeBundling")
)
)
)
server.R
shinyServer(function(input, output, session) {
#output to the graph div
output$graph <- reactive({
#get the selected file
input$data_files
})
})
HierarchicalEdgeBundling.js
//shiny output binding
var binding = new Shiny.OutputBinding();
binding.find = function(scope) {
return $(scope).find(".HierarchicalEdgeBundling");
};
binding.renderValue = function(el, data) {
//empty the div so that it removes the graph when you change data
$(el).empty()
if(data!=null){
var diameter = 960,
radius = diameter / 2,
innerRadius = radius - 120;
var cluster = d3.layout.cluster()
.size([360, innerRadius])
.sort(null)
.value(function(d) { return d.size; });
var bundle = d3.layout.bundle();
var line = d3.svg.line.radial()
.interpolate("bundle")
.tension(.85)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
//select the div that has the same id as the el id
var svg = d3.select("#" + $(el).attr('id')).append("svg")
.attr("width", diameter+300)
.attr("height", diameter+300)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
//add the data from the user input
d3.json(data, function(error, classes) {
var nodes = cluster.nodes(packageHierarchy(classes)),
links = packageImports(nodes);
link = link
.data(bundle(links))
.enter().append("path")
.each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
.attr("class", "link")
.attr("d", line);
node = node
.data(nodes.filter(function(n) { return !n.children; }))
.enter().append("text")
.attr("class", "node")
.attr("dy", ".31em")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
.style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.text(function(d) { return d.key; })
.on("mouseover", mouseovered)
.on("mouseout", mouseouted);
});
function mouseovered(d) {
node
.each(function(n) { n.target = n.source = false; });
link
.classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
.classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
.filter(function(l) { return l.target === d || l.source === d; })
.each(function() { this.parentNode.appendChild(this); });
node
.classed("node--target", function(n) { return n.target; })
.classed("node--source", function(n) { return n.source; });
}
function mouseouted(d) {
link
.classed("link--target", false)
.classed("link--source", false);
node
.classed("node--target", false)
.classed("node--source", false);
}
d3.select(self.frameElement).style("height", diameter + "px");
// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
var map = {};
function find(name, data) {
var node = map[name], i;
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
node.parent.children.push(node);
node.key = name.substring(i + 1);
}
}
return node;
}
classes.forEach(function(d) {
find(d.name, d);
});
return map[""];
}
// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
var map = {},
imports = [];
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
// For each import, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.imports) d.imports.forEach(function(i) {
imports.push({source: map[d.name], target: map[i]});
});
});
return imports;
}
}
};
//register the output binding
Shiny.outputBindings.register(binding, "HierarchicalEdgeBundling");
要使应用程序正常工作,您需要创建一个www
与您的文件夹位于同一目录中ui.R
and server.R
并放入其中HierarchicalEdgeBundling.js
文件,找到的csshere http://bl.ocks.org/mbostock/7607999 in a graph_style.css
文件和 json 数据文件(例如数据1.json http://vanhumbeecka.github.io/R-and-D3/blogdata.json or 数据2.json https://gist.githubusercontent.com/mbostock/1044242/raw/7b3997f1556f1ea862d462b3d73d70ed6bc68ea3/readme-flare-imports.json).