earthlink earthlink - 1 month ago 20
R Question

Displaying datatable in highcharter tooltip

Using the first block of code in this post I want to create a tooltip that would display the list of doctors visiting a clinic on a particular day.I tried the following code which displays nothing

library(DT)
tltp = DT:: datatable(data.frame(Doctors = x[x$Clinic=="{point.series}"&x$VisitDate == "{point.x}",2]))
hc%>%hc_tooltip(pointFormat = tltp)


I also tried using the
tooltip_table
which gives error

tltp = tooltip_table(x = NULL, y = x[x$Clinic=="{point.series}"&x$VisitDate == "{point.x}",2]
hc%>%hc_tooltip(pointFormat = tltp)

Error: unexpected symbol in:
"tltp = tooltip_table(x = NULL, y = x[x$Clinic=="{point.series}"&x$VisitDate == "{point.x}",2]
tltp"


Apologies I am not fluent in writing javascript.

Answer

As the official page recommend, to use highcharter is good alternative read how highchartsjs works. So, see this example with a simple custom tooltip.

hc <- hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>% 
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal",
    enableMouseTracking = TRUE)
  ) 

Adding the simple tooltip using the column names: Clinic and freq

hc %>% 
  hc_tooltip(pointFormat = "this is and clinic {point.Clinic} and freq {point.freq}")

The tooltip_table function is to make tables in the tooltip:

tt <- tooltip_table(c("Clinic", "Freq"), c("{point.series.name}", "{point.y}"))

hc %>% 
   hc_tooltip(pointFormat = tt, useHTML = TRUE)

If you need other data to show in the tooltip you can create the columun:

visits$doctors <- sample(letters, size = nrow(visits))

And then create the chart again (using the new data) and use this column in the tooltip:

hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>% 
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal",
    enableMouseTracking = TRUE)
  ) %>% 
  hc_tooltip(pointFormat = "Here is the doctor {point.doctors}")