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

2024-04-26

在参照this http://shiny.rstudio.com/articles/html-ui.html关于如何完全用 HTML 构建闪亮的应用程序,我想知道是否有任何方法可以将这种方法与传统的 ui.r 方法结合使用。

Reason: This http://blog.ae.be/combining-the-power-of-r-and-d3-js/将 D3 与 R Shiny 一起使用的方法似乎需要将所有 D3 代码放入 index.html 文件中。但我也想要一些交互性(selectInputs、dateRangeInputs、多个选项卡等)。有什么建议吗?


您可以使用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).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以为我的 r 闪亮界面使用 index.html 和 ui.r 吗? 的相关文章

  • 在 R 中将文本文件拆分为段落文件

    我正在尝试将一个巨大的 text 文件拆分为多个 text 文件 每个文件仅包含一个段落 让我举个例子 我需要这样的文字 这是第一段 这没有任何意义 因为这只是一个例子 这是第二段 和前一段一样毫无意义 另存为两个独立的 txt 文件 其中
  • 在 Bookdown 中呈现附录图号

    Bookdown 是一个很棒的软件包 我期待看到它如何发展 但现在我在渲染数字方面遇到了麻烦pdf document2附录中的数字时的格式 具体来说 当带有标题的图形位于附录中时 图形编号应采用 A 1 A 2 B 1 B 2 等形式 但图
  • 将阿拉伯文本分配给 R 变量

    R 无法正确显示阿拉伯文本 当我使用阿拉伯语时 我得到了非常奇怪的东西 这是一个屏幕截图 问题是我想创建一个带有阿拉伯文本的词云 我需要首先解决这个问题 R 版本 R 2 15 2 GUI 1 53 Leopard 版本 64 位 6335
  • ggmap 错误:GeomRasterAnn 是使用不兼容版本的 ggproto 构建的

    我正在使用 ggmap 并收到以下错误 Error GeomRasterAnn was built with an incompatible version of ggproto Please reinstall the package t
  • 使用 ggplot_build 和 ggplot_gtable 后使用 ggsave 保存图形

    我正在通过更改 ggplot build 生成的数据来修改使用 ggplot 构建的图表 原因类似于包括 geom boxplot 中填充美学中使用的缺失因子水平的空间 https stackoverflow com questions 1
  • 在 r 中的 unique() 函数中使用管道不起作用

    我在使用管道运算符 gt 和 unique 函数时遇到一些麻烦 df data frame a c 1 2 3 1 b a unique df a no problem here df gt unique a not working her
  • 完全缺失列的 VaR 计算

    我需要计算股票收益的滚动 VaR 从这篇文章 使用rollapply函数使用R进行VaR计算 https stackoverflow com questions 25045612 using rollapply function for v
  • 在R中绘制3x3方形网格

    我得到了一个数字列表 n 9 想将它们画在一个 3 3 的正方形网格中 每个网格填充相应的数字 我如何在 R 中执行此操作而不安装额外的软件包 例如情节 非常感谢 这里有一个ggplot解决方案比我预期的要难一点 Setup the dat
  • (R 错误)错误:cons 内存耗尽(达到限制?)

    我正在处理大数据 并且有一个 70GB 的 JSON 文件 我正在使用 jsonlite 库将文件加载到内存中 我尝试过 AWS EC2 x1 16large 机器 976 GB RAM 来执行此负载 但 R 因错误而中断 Error co
  • 在 R 中打印绘图时第一页为空

    我正在尝试创建一个包含多个图的pdf 更具体地说 我想保存我的图 每页 4 个 因此 我在 r 中有以下代码 可以工作 但将页面留空 第一个 pdf Plots plots numeric four in page pdf paper a4
  • 如何对工作区中的元素运行循环?

    gt ls 1 A anorex 1 anorexia B byMonth C clotting 8 counts d D d AD DelayByDay delayed glm D93 15 glmout groups h housing
  • 为格子中的每个面板添加不同的独特标签

    很清楚如何在格子中标记面板 https stackoverflow com questions 8508269 how to label panels in lattice using panel text or ltext论据 但是 如果
  • R 中使用 randomForest 进行内存高效预测

    TL DR我想知道使用基于大型数据集 数百个特征 数十万行 构建的随机森林模型执行批量预测的内存有效方法 Details 我正在处理一个大型数据集 内存中超过 3GB 并且想要使用以下方法进行简单的二进制分类randomForest 由于我
  • 使用 plyr daply 将数据帧转换为矩阵

    我正在尝试使用daply函数在plyr包 但我无法让它正确输出 尽管组成矩阵的变量是数字 但矩阵的元素是列表 而不是变量本身 例如 以下是一小部分数据 Month Vehicle Samples 1 Oct 10 31057 256 2 O
  • R中的等值线绘图问题

    编辑 我已经意识到问题的根源 我只有我有数据的县的计数信息 该信息少于我正在绘制的区域中的县数量 按理说 问题代码行在这里 mapnames lt map county plot FALSE 4 names colorsmatched lt
  • 计算数据框中每一行的 R 条件运行总和

    我想创建一个等于 data Rating 的运行总和的列 假设第 3 列和第 4 列中有两个条件成立 特别是 data Year 换句话说 这应该计算直到上一年为止每个 id 的评分累积总和 它应该对数据框中的每一行 大约 50 000 行
  • 访问动态创建的 Shiny 模块的返回值

    我正在寻找构建一个闪亮的应用程序 它动态创建返回简单表单的模块 通过 callmodule 我有两个未解决的问题 希望得到一些指导 首先 当向用户提供多个表单 通过单击按钮 时 先前呈现的表单上的值将恢复为默认值 如何停止这种行为 以便值保
  • 如何缩放(标准化)每列内的 ggplot2 stat_bin2d 值(按 X 轴)

    我有一个 ggplot stat bin2d 热图 library ggplot2 value lt rep 1 5 1000 df lt as data frame value df group lt rep 1 7 len 5000 d
  • 如何在没有 DOM 的情况下将 javascript 作为 node.js 脚本运行?

    https github com jasondavies d3 cloud https github com jasondavies d3 cloud是一个使用 D3 库的 javascript 文字云 这是一个交互式演示 http www
  • 将任何 D3.js 图表与 DC.js 集成

    我试图弄清楚如何轻松地将任何 D3 js 图表与 DC js 库集成 以便在所有图表之间使用交叉过滤功能 我也确实理解以前曾问过这种性质的问题 添加链接以供参考 DC js 监听图表组渲染 https stackoverflow com q

随机推荐

  • 如何防止在 Google Chrome 中选择文本?

    没有 oEvent preventDefault 在GC工作 我需要防止在触发 onmove 事件时选择文本 EDIT 事实证明这很容易 function disableSelection document onselectstart fu
  • ‘access’参数模式有什么用处?

    Ada 中有三种传递参数的 正常 模式 in out and in out 但还有第四种模式 access 有什么需要它们的吗 即 否则不可能实现的事情 现在 我确实知道 GNAT JVM Ada 编译器在导入的 库 规范中大量使用了它们
  • 角度样式未应用于组件(尽管使用了主机选择器)

    我的角度项目中的组件样式存在问题 我基本上无法让它工作 为了解释我的问题 我使用 Angular CLI CLI 6 0 8 Angular 6 1 0 创建了一个新项目 我立即创建了一个新的测试组件 在其中声明了以下内容 COMPONEN
  • 最喜欢的开源 Google App Engine 应用程序(Java 或 Python)[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 你能阻止 jQuery focusout 在失去焦点时触发吗?

    我有一个输入框 我想在它失去焦点时保存它的值 非常简单的事情 我可以通过 jQuery 来完成focusout event 然而问题是我不想解雇focusout当用户单击输入框旁边的 X 图标时发生的事件 示例如下所示 因此 当用户跳出此输
  • 在 tomcat 上部署 AngularJS 应用

    我使用 yeoman 角度生成器创建了一个 AngularJS 应用程序 现在我想使用 Apache tomcat 托管它 我已经运行了 grunt build 并将所有资产准备在 dist 文件夹中 有没有办法创建 war 文件 或者我可
  • react-native limit 列表项

    我在用Flatlist https facebook github io react native docs flatlist html来自反应本机和ListItem https react native training github i
  • 在 Mobile Safari 中禁用同源策略

    我有一个 HTML5 JavaScript 应用程序 最初是为在某些汽车上运行而编写的 基本上 我需要将我的应用程序设置为在浏览器中运行 以便向客户进行简单的演示 我正在使用 jQuery ajax 它由于同源策略而导致问题 我发现了很多在
  • 有没有办法以特定顺序存储 PyTable 列?

    当使用字典或类进行模式定义以调用 createTable 时 PyTable 列似乎按字母顺序排列 我的需要是建立一个特定的顺序 然后使用 numpy genfromtxt 从文本中读取和存储我的数据 我的文本文件没有像 PyTable 那
  • IBM Worklight Server 6.2 - 在 Apache Tomcat 上运行? “等待管理服务启动超时”

    我一直在尝试设置 IBM Worklight 环境 我想使用 Windows服务器 Apache Tomcat 7 撰写本文时最新 IBM Worklight 6 2 撰写本文时最新版本 MySQL Server 5 6 撰写本文时最新 一
  • TypeScript 函数声明在接口中的区别

    TypeScript 接口中这两种函数声明有什么区别 interface IExample myFunction str string void and interface IExample myFunction str string gt
  • 如何在 Gremlin 中进行分页

    Tinkerpop 3中如何进行分页 我想获取查询的前 10 个元素 然后获取接下来的 10 个元素 而不必将它们全部加载到内存中 例如 下面的查询返回 1000 000 条记录 我想以 10 x 10 的形式获取它们 而不是一次加载所有
  • 如何在 Kivy 中设置小部件/布局的最小允许宽度/高度?

    我有包含 3 个元素的 BoxLayout 我需要第一个和最后一个元素占据最小的可用空间 中间元素具有固定比例 1 1 因此当我调整窗口大小时 侧面元素变得太小并且内容会超出其中 我需要例如标签 或按钮 甚至不同元素的集合 文本始终insi
  • 防止 iOS 在进入后台之前截取应用程序的屏幕截图

    大家可能都知道 iOS 在将应用程序放入后台之前会对其进行屏幕截图 这通常是为了更好的用户体验 例如快速动画以恢复应用程序等 我不希望我的应用程序屏幕截图存储在设备上 但我希望多任务处理仍然存在 我想出了一个解决方案 但我不确定我是否朝着正
  • Docker LABEL 用于在多阶段构建中访问构建镜像

    在 CI 构建期间 我通过添加以下内容从多阶段 Docker 构建 测试报告 中的构建映像中提取构建工件 LABEL构建步骤 然后使用带标签的图像创建一个容器以提取工件 这似乎在 Mac 版 Docker Desktop 中被破坏了 或者我
  • Android 上的 SQLite 如何处理长字符串?

    我想知道 Android 的 SQLite 实现如何处理长字符串 从 sqlite 的在线文档中读取 它说 sqlite 中的字符串限制为 100 万个字符 我的弦肯定更小 我正在创建一个简单的 RSS 应用程序 在解析 html 文档并提
  • Python 中的静态变量继承

    我正在为 Blender 的一个项目编写 Python 脚本 但我对该语言还很陌生 我感到困惑的是静态变量的使用 这是我目前正在处理的代码片段 class panelToggle bpy types Operator active Fals
  • 使用二进制遮罩遮罩 RGB 图像

    我在 MATLAB 中读入了一个 RGB 图像 M x N x 3 矩阵 我还有一个图像的二进制掩码 M x N 矩阵 对于某些感兴趣的区域来说它只是 0 而其他地方都是 1 我正在尝试找出如何使用该二进制掩码来掩蔽 RGB 图像 我尝试过
  • Android 将位图保存到 SD 卡

    我有一个按钮 我希望当我单击它时 图像会保存到 SD 卡中 或内部存储 如 htc one x 我们没有像 SD 卡这样的外部存储 这是我的代码 sd setOnClickListener new View OnClickListener
  • 我可以为我的 r 闪亮界面使用 index.html 和 ui.r 吗?

    在参照this http shiny rstudio com articles html ui html关于如何完全用 HTML 构建闪亮的应用程序 我想知道是否有任何方法可以将这种方法与传统的 ui r 方法结合使用 Reason Thi