In a shiny context you can do the following, note that I made link be just a url rather than a html tag
library(plotly)
library(shiny)
dtd7 <- structure(
list(
topic = structure(
c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L,
5L, 10L, 13L, 11L, 12L),
.Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango","Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"),
class = "factor"
),
n = structure(
c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L,
7L, 8L, 1L),
.Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"),
class = "factor"
),
link = structure(c("https://www.google.co.in/", "https://www.google.co.in/", "https://www.google.co.in/",
"https://www.google.co.in/", "https://www.google.co.in/", "https://www.google.co.in/",
"https://www.google.co.in/", "https://www.google.co.in/", "https://www.amazon.co.in/",
"https://www.google.co.in/", "https://www.google.co.in/", "https://www.google.co.in/",
"https://www.google.co.in/"))
),
class = "data.frame",
row.names = c(NA,-13L)
)
library(shiny)
ui <- fluidPage(
plotlyOutput("treeout")
)
server <- function(input, output, session) {
p_obj <- reactive(
plot_ly(
dtd7,
labels = ~ topic,
parents = NA,
values = ~ n,
type = 'treemap',
customdata = ~link,
source = "treemap_click",
hovertemplate = "<extra></extra>"
) %>% event_register("plotly_click")
)
output$treeout <- renderPlotly(
req(p_obj()))
observe({
d <- event_data(event = "plotly_click",
source = "treemap_click")
req(d)
browseURL(d$customdata)
})
}
shinyApp(ui, server)