我可以将index.html和ui.r用于我的r闪亮界面吗?

时间:2015-04-29 14:09:48

标签: r d3.js shiny

关于如何在HTML中完全构建闪亮的应用程序的this,我想知道是否有任何方法可以将这种方法与传统的ui.r方法结合使用。

原因:使用带有R Shiny的D3的This方法似乎需要将所有D3代码放入index.html文件中。但我也想要一些交互性(selectInputs,dateRangeInputs,多个标签等)。有什么建议吗?

2 个答案:

答案 0 :(得分:5)

您可以使用tags向您的ui.R添加自定义HTML。

对于更复杂的输出,您可以在使用ui.Rserver.R的应用时构建自定义闪亮输出。 This page有关于如何为任何代码执行此操作的信息。

以下是使用您发布的D3示例中的javascript代码的示例。 该应用程序只生成绘图,我添加了selectInput,您可以在其中选择要绘制的数据以显示事物的集成方式。所有的javascript代码都是由mbostock完成的。 (可以找到here)。

我添加了闪亮的装订部分并更改了几行以使其适应应用程序,我在更改上方进行了评论,以便您可以跟踪它们。

<强> 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");

要使应用运行起来,您需要在与wwwui.R相同的目录中创建server.R文件夹,并在其中输入HierarchicalEdgeBundling.js文件,即CSS在graph_style.css文件和json数据文件(例如heredata1.json)中找到了data2.json

答案 1 :(得分:2)

我的理解是使用index.html是ui.R的替代品,以需要使用HTML代码的“成本”提供更多功能(相对于“开箱即用”的闪亮功能,如selectInputs等......这些实际上只是将一系列输入转换为HTML代码。)

要在index.html中找到等效函数,一个提示是创建您的ui.R,然后在浏览器中查看页面源。这将为您提供正确的HTML代码,以便在您的闪亮应用中实现。例如,选项卡可能如下所示:

<div class="container-fluid">
    <div class="tabbable tabs-above">
      <ul class="nav nav-tabs">
        <li class="active">
          <a href="#tab-5303-1" data-toggle="tab" data-value="Graphs">Graphs</a>
        </li>
        <li>
          <a href="#tab-5303-2" data-toggle="tab" data-value="Size Data">Size Data</a>
        </li>
        <li>
          <a href="#tab-5303-3" data-toggle="tab" data-value="Data Bins">Data Bins</a>
        </li>
      </ul>

...

<div class="tab-content">
        <div class="tab-pane active" data-value="Graphs" id="tab-5303-1">

...

    </div>
  </div>

一旦弄清楚如何将UI参数转换为HTML代码,就会非常简单。您提供的链接可以很好地总结如何连接到您的服务器文件。