在JavaScript中检索R对象属性 [英] Retrieving R object attributes in JavaScript

查看:126
本文介绍了在JavaScript中检索R对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含100个观察值的双变量数据集.我使用了六边形合并,最后得到了26个六边形合并.为了保存在26个六边形箱中的每一个中的100个观察值的行,我在R中使用了base::attr函数.在下面的代码中,此操作在以下位置完成:

I have a bivariate dataset with 100 observations. I used hexagon binning and ended up with 26 hexagon bins. In order to save the rows of the 100 observations that are in each of the 26 hexagon bins, I used the base::attr function in R. In the code below, this is done at:

attr(hexdf, "cID") <- h@cID

我正在尝试创建一个六边形合并的交互式R Plotly对象,这样,如果用户单击给定的六边形合并,他们将获得分组到该合并中的100个观测值的行.我已经完成了部分目标.我的MWE在下面:

I am trying to create an interactive R Plotly object of the hexagon binning so that if a user were to click on a given hexagon bin, they would obtain the rows of the 100 observations that were grouped into that bin. I have part of this goal completed. My MWE is below:

library(plotly)
library(data.table)
library(GGally)
library(hexbin)
library(htmlwidgets)

set.seed(1)
bindata <- data.frame(ID = paste0("ID",1:100), A=rnorm(100), B=rnorm(100))
bindata$ID <- as.character(bindata$ID)

x = bindata[,c("A")]
y = bindata[,c("B")]
h <- hexbin(x=x, y=y, xbins=5, shape=1, IDs=TRUE)
hexdf <- data.frame (hcell2xy (h),  hexID = h@cell, counts = h@count)
attr(hexdf, "cID") <- h@cID
pS <- ggplot(hexdf, aes(x=x, y=y, fill = counts, hexID=hexID)) + geom_hex(stat="identity")

ggPS <- ggplotly(pS)

myLength <- length(ggPS[["x"]][["data"]])
for (i in 1:myLength){
  item =ggPS[["x"]][["data"]][[i]]$text[1]
  if (!is.null(item))
    if (!startsWith(item, "co")){
      ggPS[["x"]][["data"]][[i]]$hoverinfo <- "none"
    }
}

ggPS %>% onRender("
          function(el, x, data) {
            //console.log(el)
            //console.log(x)
            //console.log(data)

            myGraph = document.getElementById(el.id);
            el.on('plotly_click', function(e) {

            cN = e.points[0].curveNumber
            split1 = (x.data[cN].text).split(' ')
            hexID = (x.data[cN].text).split(' ')[2]
            counts = split1[1].split('<')[0]
            console.log(hexID)
            console.log(counts)

           })}
           ", data = pS$data)

运行此代码并在Web浏览器中将其打开时,我将获得如下所示的交互式图(图中没有绿色框;为了说明目的将其叠加):

When I run this code and open it in the Web Browser, I obtain an interactive plot like below (green box is not in plot; it's superimposed for explanatory purposes):

如果我单击绿色框内的六边形,则会在控制台上打印正确的hexID(40)和counts(3).此时,我想获取放入该六角形仓中的原始数据帧的3行.

If I click on the hexagon inside the green box, the correct hexID of 40 and counts of 3 are printed to the console. At this point, I would like to obtain the 3 rows of the original data frame that were put into that hexagon bin.

我知道如何在htmlwidgets包的onRender()函数之外的R中使用base::attr函数来执行此操作.例如,我可以执行以下操作:

I know how to do this in R outside of the onRender() function of the htmlwidgets package by using the base::attr function. For instance, I can do the following:

hexID=40
obsns <- which(attr(pS$data, "cID")==hexID)
dat <- bindata[obsns,]

并收到以下正确的3个数据点,这些数据点已放入我单击的那个容器中:

And receive the following correct 3 data points that were put into that bin I clicked on:

     ID         A        B
47 ID47 0.3645820 2.087167
66 ID66 0.1887923 2.206102
71 ID71 0.4755095 2.307978

与这个MWE相比,我正在处理更大的数据集.因此,我使用base:attr函数的目的是为了防止更大的数据帧浮动.但是,我不确定如何转换base::attr函数的功能,以便可以访问onRender() JavaScript代码中单击的六边形框中出现的相应数据点行.我确实将pS$data对象包含在onRender() JavaScript代码中,但仍然卡住了.

I am working with much larger datasets than in this MWE. For that reason, my intention of using the base:attr function was to prevent ever larger data frame floating around. However, I am unsure how to translate the functionality of the base::attr function so that I can access the appropriate data point rows that occur in a clicked hexagon bin in the onRender() JavaScript code. I did include the pS$data object into the onRender() JavaScript code, but am still stuck.

任何建议将由衷地感谢!

Any advice would be sincerely appreciated!

推荐答案

您可以添加一列,该列的每一行都具有其在binata中所属的十六进制bin的ID:

You could add a column which for each row has the ID of the hexbin it belongs to in your bindata:

bindata$hex <- h@cID

然后,您可以将其传递给onRender函数,并在用户单击六边形时过滤行:

You can then pass it to the onRender function and filter rows when the users clicks on a hexagon:

ggPS %>% onRender("
                  function(el, x, data) {
                  myGraph = document.getElementById(el.id);
                  el.on('plotly_click', function(e) {

                  cN = e.points[0].curveNumber
                  split1 = (x.data[cN].text).split(' ')
                  hexID = (x.data[cN].text).split(' ')[2]
                  counts = split1[1].split('<')[0]

                  var selected_rows = [];

                  data.forEach(function(row){
                    if(row.hex==hexID) selected_rows.push(row);
                  });
                  console.log(selected_rows);

                  })}
                  ", data = bindata)

这篇关于在JavaScript中检索R对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆