首先,我对javascript及其库d3相当不熟悉.js,但我熟悉R.使用Shiny创建仪表盘既有趣又简单(多亏了stackoverflow).现在我想通过连接d3元素来扩展它.

我正在寻找关于如何将javascript绑定到Shiny(R dashboard)并解释实际情况的信息源.

背景:

http://shiny.rstudio.com/articles/building-inputs.html

但不幸的是,我没有,而且我似乎找不到任何在最小工作代码中的示例.github上的许多例子对我来说太复杂了,很可能是因为我对javascript的经验太少.下面是一个使用javascript自定义输入绑定的示例:

https://github.com/jcheng5/shiny-js-examples/tree/master/input

下面是一个输入&我try 展开的输出绑定:

<script src="http://d3js.org/d3.v3.js"></script>
<script type="text/javascript">
(function(){
  // Probably not idiomatic javascript.

  this.countValue=0;

  // BEGIN: FUNCTION
  updateView = function(message) {

    var svg = d3.select(".d3io").select("svg")

    svg.append("text")
      .transition()
      .attr("x",message[0])
      .attr("y",message[1])
      .text(countValue)
      .each("end",function(){
        if(countValue<100) {
          countValue+=1;
          $(".d3io").trigger("change");
        }
      })
  }
  // END: FUNCTION

  //BEGIN: OUTPUT BINDING
  var d3OutputBinding = new Shiny.OutputBinding();
  $.extend(d3OutputBinding, {
    find: function(scope) {
      return $(scope).find(".d3io");
    },
    renderError: function(el,error) {
      console.log("Foe");
    },
    renderValue: function(el,data) {
      updateView(data);
      console.log("Friend");
    }
  });
  Shiny.outputBindings.register(d3OutputBinding);
  //END: OUTPUT BINDING

  //BEGIN: INPUT BINDING
  var d3InputBinding = new Shiny.InputBinding();
  $.extend(d3InputBinding, {
    find: function(scope) {
      return $(scope).find(".d3io");
    },
    getValue: function(el) {
      return countValue;
    },
    subscribe: function(el, callback) {
      $(el).on("change.d3InputBinding", function(e) {
        callback();
      });
    }
  });
  Shiny.inputBindings.register(d3InputBinding);
 //END: OUTPUT BINDING

})()
</script>

其中"d3io"是ui中的div元素,updateView()是一个函数.以下是用户界面:

#UI
library(shiny)

d3IO <- function(inputoutputID) {
  div(id=inputoutputID,class=inputoutputID,tag("svg","")) #; eerst zat ; erbij, maar werkt blijkbaar ook zonder
}

# Define UI for shiny d3 chatter application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("D3 Javascript chatter",
              "Demo of how to create D3 I/O and cumulative data transfer"),

  sidebarPanel(
    tags$p("This widget is a demonstration of how to wire shiny direct to javascript, without any input elements."),
    tags$p("Each time a transition ends, the client asks the server for another packet of information, and adds it
            to the existing set"),
    tags$p("I can't claim this is likely to be idiomatic javascript, because I'm a novice, but it allows d3 apps
            to do progressive rendering.  In real use, a more complex request/response protocol will probably be
            required.  -AlexBBrown")
  ),

  mainPanel(
    includeHTML("d3widget.js"),
    d3IO("d3io") #Creates div element that d3 selects
    )
))

以下是服务器文件:

# SERVER
library(shiny)
# Define server logic required to respond to d3 requests
shinyServer(function(input, output) {

  # Generate a plot of the requested variable against mpg and only 
  # include outliers if requested
  output$d3io <- reactive(function() {
    if (is.null(input$d3io)) {
      0;
    } else {
      list(rnorm(1)*400+200,rnorm(1)*400+200);
    }
  })
})

具体问题:

1) 服务器.由于"d3io"似乎未在"输入"中定义($d3io).r、 我推断它一定来自javascript文件.它实际上指的是哪个元素?

2) 我无法理解定制绑定部分:

var d3OutputBinding = new Shiny.OutputBinding();
  $.extend(d3OutputBinding, {
    find: function(scope) {
      return $(scope).find(".d3io");
    },
    renderError: function(el,error) {
      console.log("Foe");
    },
    renderValue: function(el,data) {
      updateView(data);
      console.log("Friend");
    }
  });
  Shiny.outputBindings.register(d3OutputBinding);

我的理解是:

创建一个新的输出绑定,首先找到类.d3io(div元素),如果出现错误,则写入控制台"Foe"(这是特殊代码吗?),如果没有错误,则使用函数updateView using data呈现值(它从哪里接收该值?)写信安慰"朋友".最后注册输出.

希望你们能帮忙!我正在创建一个文档,其中包含"学习如何在不懂任何javascript的情况下将javascript实现到shiny中的必要步骤",我很乐意!)

干杯

推荐答案

嗨宝贝耶稣(说起来很有趣).你有两个问题:

1) 服务器.由于"d3io"似乎未在"输入"中定义($d3io).r、 我推断它一定来自javascript文件.它实际上指的是哪个元素?

短语input$d3io包含以下组成部分:

  • input是一个传递到函数中的参数——它是一个
  • $是成员 Select 器.
  • d3io指具有该id的div元素的内容

2) 我无法理解定制绑定部分:

var d3OutputBinding = new Shiny.OutputBinding();

没错,这会创建一个shiny 的实例.OutputBinding并将其分配给变量d3OutputBinding.

$.extend(d3OutputBinding, {
  find: function(scope) {
    return $(scope).find(".d3io");
  },
  renderError: function(el,error) {
    console.log("Foe");
  },
  renderValue: function(el,data) {
    updateView(data);
    console.log("Friend");
  }
});

这段代码使用名为findrenderErrorrenderValue的三个函数扩展了d3OutputBinding的行为.这三个功能对于一个有光泽的系统来说是必需的.输出绑定.

find是键,因为它返回一个元素列表,这些元素应该通过el参数传递到两个渲染函数中.注意,它返回的是css类为"d3io"的元素——与前面提到的div相同.

请注意,extend()是jQuery javascript库的一个函数,而本文中的$是jQuery对象的别名.

Shiny.outputBindings.register(d3OutputBinding);

让Shiny知道这个新配置的对象现在应该投入使用.

干杯,尼克

R相关问答推荐

在R中,将一个函数作为输入传递给另一个函数时进行参数判断

Select 与特定列中最大值对应的数据帧行

self_函数无法工作--无法子集结束后的列

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

是否可以 Select 安装不带文档的R包以更有效地存储?

无法运行通过R中的Auto.arima获得的ARIMA模型

如何使用R中的dhrr函数将李克特量表的因子列从长转换为宽?

查找图下的面积

次级y轴R gggplot2

R Highcharts与两个位置关联的注释

通过使用str_detect对具有相似字符串的组进行分组

当两个图层映射到相同的美学时,隐藏一个图层的图例值

如何将移除事件分配给动态创建的按钮?

从一个列表的框架中移除列表包装器

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

为什么在写入CSV文件时Purrr::Pwalk不起作用

有没有办法将不等长的列表转换为R中的数据帧

如何将宽格式的患者信息数据高效地转换为患者计数的时间序列?

将日期列从字符转换为日期得到的结果是NAS

R:改进实现简单模型