在 JavaScript 中检索 R 对象属性
Retrieving R object attributes in JavaScript
我有一个包含 100 个观测值的双变量数据集。我使用了六边形分箱,最后得到了 26 个六边形分箱。为了保存 26 个六边形箱中每一个中的 100 个观测值的行,我在 R 中使用了 base::attr
函数。在下面的代码中,这是在
处完成的
attr(hexdf, "cID") <- h@cID
我正在尝试创建六边形分箱的交互式 R Plotly
对象,这样如果用户单击给定的六边形分箱,他们将获得 100 个观察值的行,这些观察值被分组到那个垃圾桶。我已经完成了这个目标的一部分。我的 MWE 如下:
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 浏览器中打开它时,我获得了如下交互式图(绿色框不在图中;为了解释目的而将其叠加):
如果我单击绿色框内的六边形,则会将正确的 hexID
of 40 和 counts
of 3 打印到控制台。此时,我想获取原始数据框的3行被放入那个六边形bin中。
我知道如何在 htmlwidgets
包的 onRender()
函数之外通过使用 base::attr
函数在 R 中执行此操作。例如,我可以执行以下操作:
hexID=40
obsns <- which(attr(pS$data, "cID")==hexID)
dat <- bindata[obsns,]
并收到以下正确的 3 个数据点,这些数据点被放入我点击的那个容器中:
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 代码中,但我仍然卡住了。
如有任何建议,我们将不胜感激!
您可以添加一列,其中每一行都有它在您的 bindata 中所属的 hexbin 的 ID:
bindata$hex <- h@cID
然后您可以将其传递给 onRender
函数并在用户单击六边形时过滤行:
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)
我有一个包含 100 个观测值的双变量数据集。我使用了六边形分箱,最后得到了 26 个六边形分箱。为了保存 26 个六边形箱中每一个中的 100 个观测值的行,我在 R 中使用了 base::attr
函数。在下面的代码中,这是在
attr(hexdf, "cID") <- h@cID
我正在尝试创建六边形分箱的交互式 R Plotly
对象,这样如果用户单击给定的六边形分箱,他们将获得 100 个观察值的行,这些观察值被分组到那个垃圾桶。我已经完成了这个目标的一部分。我的 MWE 如下:
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 浏览器中打开它时,我获得了如下交互式图(绿色框不在图中;为了解释目的而将其叠加):
如果我单击绿色框内的六边形,则会将正确的 hexID
of 40 和 counts
of 3 打印到控制台。此时,我想获取原始数据框的3行被放入那个六边形bin中。
我知道如何在 htmlwidgets
包的 onRender()
函数之外通过使用 base::attr
函数在 R 中执行此操作。例如,我可以执行以下操作:
hexID=40
obsns <- which(attr(pS$data, "cID")==hexID)
dat <- bindata[obsns,]
并收到以下正确的 3 个数据点,这些数据点被放入我点击的那个容器中:
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 代码中,但我仍然卡住了。
如有任何建议,我们将不胜感激!
您可以添加一列,其中每一行都有它在您的 bindata 中所属的 hexbin 的 ID:
bindata$hex <- h@cID
然后您可以将其传递给 onRender
函数并在用户单击六边形时过滤行:
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)