【问题标题】:Can I use index.html and ui.r for my r shiny interface?我可以将 index.html 和 ui.r 用于我的 r 闪亮界面吗?
【发布时间】:2015-04-29 14:09:48
【问题描述】:

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

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

【问题讨论】:

    标签: r d3.js shiny


    【解决方案1】:

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

    对于更复杂的输出,您可以在使用带有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");
    

    要使应用程序正常工作,您需要在与ui.Rserver.R 相同的目录中创建一个www 文件夹,并将HierarchicalEdgeBundling.js 文件放入其中,该css 在@987654341 中找到here @ 文件和 json 数据文件(例如 data1.jsondata2.json)。

    【讨论】:

    • 正是我需要的。非常感谢@NicE!
    • 很好的答案,但在最后一步 - css 中磕磕绊绊。我提取了您链接的 CSS 文件 (bl.ocks.org/style.css?20120730),但可视化样式不正确:i.imgur.com/nvpD0Pb.png。感谢您提供的任何建议,谢谢。
    • 您粘贴链接的 CSS 文件没有图表的节点和链接的 CSS。为了获得图表,我从 mbostock 的 this code 复制了 CSS,并将其粘贴到 graph_style.css 文件中。
    • @NicE 谢谢你,我犯了一个愚蠢的错误!你已经有了我的 +1,因为我已经完成了 80% 的工作。这对我来说已经非常有用了。
    【解决方案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 代码,它就相当简单了。您提供的链接很好地总结了如何连接到您的服务器文件。

    【讨论】:

    • 谢谢@Chris。我理解这种做法。实际上,ui.R 文件旨在生成浏览器将解释的 HTML。我当然可以那样做。但是,由于我与一组开发人员一起工作,我只是想知道是否有办法将两者结合使用。以某种方式将一些 D3 HTML 输出注入到 R Shiny HTML 中
    • 您要生成什么类型​​的 D3 html 输出?可以举个例子吗?
    • 嗨@Picarus。感谢您的回复。请看上面NicE提供的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2016-06-05
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多