Previously, I shared a question with similarities (Retrieving R object attributes in JavaScript). Unfortunately, the answer received did not directly address my real problem due to an oversimplified example. Here, I am exploring the need to retrieve R object attributes in JavaScript and looking for alternative approaches.
My dataset consists of 5 variables and 100 observations. Using hexagon binning, I created a scatterplot matrix with each plot containing 12-18 hexagons. To retain the rows of observations grouped within these hexagon bins across all plots, I utilized the base::attr function in R as shown in the code below:
attr(hexdf, "cID") <- h@cID
The objective is to build an interactive R Plotly object representing the hexagon binning. When a user clicks on any hexagon bin (from any scatterplot), they should obtain the associated 100 observation rows. While progress has been made towards this, there's still work to be done. Below is a minimal working example of my current setup:
Library(plotly)
...
myLength = length(ggPS[["x"]][["data"]])
...
ggPS %>% onRender("
function(el, x, data) {
...
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(myX)
console.log(myY)
console.log(hexID)
console.log(counts)
})}
", data = pS[5,2]$data)
Upon execution, an image like the one shown here is generated: https://i.sstatic.net/jskyp.png
If I click on a particular hexagon, I can identify its position in the subplot ("myX" and "myY"), the clicked ID ("hexID"), and the number of data points binned ("counts"). For instance, clicking on a specific hexagon may reveal that myX=5, myY=2, hexID=39, and counts=1, indicating the coordinates of the bin where one data point resides.
To fetch the row corresponding to the clicked hexagon, running the following code in R yields the desired output:
myX <- 5
myY <- 2
hexID <- 39
obsns <- which(attr(pS[myX,myY]$data, "cID")==hexID)
dat <- bindata[obsns,]
This retrieves the row from the data frame containing the single data observation residing in the selected hexagon:
> dat
ID A B C D E
95 ID95 1.586833 -1.208083 1.778429 -0.1101588 3.810277
The challenge lies in integrating the base::attr() function within the onRender() function to access the "obsns" object. Any insights or alternate strategies to resolve this issue would be greatly appreciated. Thank you for your suggestions!