Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

How to open a dynamic URL based on the clicked bar of a bar chart?

Hi

To be honest, I am totally new to JavaScript or plotly.js.
I am trying to create a function based on the click event of R’s Plotly chart to open up a new browser window with URL depending on bar’s value.

The following is the R script that I am trying to adapt.
I can created the click event function to open up a web browser with constant URL but not able to do that for dynamic URL.

library(plotly)
library(htmlwidgets)

df_mtcars <- as.data.frame(mtcars)
df_mtcars$brand <- rownames(mtcars)

Able to open a constant URL upon clicking a bar on the chart

plot_ly(df_mtcars, x = ~brand, y = ~mpg) %>%
onRender("
function(el) {
el.on(‘plotly_click’, function(d) {
var websitelink = ‘http://en.wikipedia.org/wiki/
window.open(websitelink);
});
}
")

Not able to open a dynamic URL

plot_ly(df_mtcars, x = ~brand, y = ~mpg) %>%
onRender("
function(el) {
el.on(‘plotly_click’, function(d) {
var websitelink = ‘http://en.wikipedia.org/wiki/’ + df_mtcars$brand
window.open(websitelink);
});
}
")