I am new to the world of combining Javascript with R and my Javascript knowledge is quite limited. I am trying to make a plotly
plot of chess openings and when you click on the opening it shows a board with the initial moves of the opening.
library(plotly)
library(htmlwidgets)
library(data.table)
library(rchess)
data("chessopenings")
setDT(chessopenings)
mychess = chessopenings[1:10]
mychess[, Pop := sample(c(1,2,4), nrow(mychess), replace = T)]
mychess[, fens := sapply(pgn, function(x) {chsspgn <- Chess$new(); chsspgn$load_pgn(x); chsspgn$fen()})]
The code above just creates a sample dataset of 10 openings. I then plot those using plotly and based on the guidance I found here https://plotly-r.com/supplying-custom-data.html#fig:hover-annotate I tried to create a new function that shows the chessboard when you click on the points. The chessboard function is the one specified here https://chessboardjs.com/examples#1002 and if I understand it correctly (please correct me if I am wrong) it returns a 'div' element which is the one I have to show.
q = plot_ly(mychess, x = ~eco, y = ~Pop) %>%
add_markers(text = ~name,
customdata = ~fens)
onRender(
q, "
function(el) {
el.on('plotly_click', function(d) {
var pt = d.points[0];
var fen = d.points[0].customdata
var newboard = Chessboard('myBoard', fen);
newboard.style.visibility = 'visible';
newboard.style.display = 'block';
})}")
Unfortunately nothing shows. I used the information provided here Show/hide 'div' using JavaScript but it seems I am missing something.
Update: I tried the suggestion by @Bas to convert the images by using the following code:
png(pic1 <- tempfile(fileext = ".png"));chessboardjs(fen = mychess$fens[1]); dev.off()
text = knitr::image_uri(pic1)
text = sub(".*?,", "", text)
html <- sprintf('<html><body><img src="data:image/png;base64,%s"></body></html>', text)
cat(html, file = tf2 <- tempfile(fileext = ".html"))
browseURL(tf2)
But this doesn't work either.