Search code examples
javascriptrhighchartsshinyr-highcharter

How to use images/icons as labels in highcharter? R shiny


I am using highcharter to make a bar plot, in a shiny app, but I am having troubles displaying icons as labels on the x-axis. The goal is to use svg images from a file placed in the working directory.

I have attached an example below and I have searched inspiration in the following two links but I am having troubles getting the JS-function into R shiny - setup. https://www.highcharts.com/demo/column-comparison https://www.highcharts.com/forum/viewtopic.php?t=16609

I though the problem was the backslach before quotation marks, but when I display the icon in my app just using tags$div(HTML("<img src = \"logoA.svg\">")), it displays perfectly (logoA.svg is placed in the www folder)

Any suggestions how to solve this?

  
library(shiny)
library(highcharter)
library(dplyr)


## app.R ##
server <- function(input, output) {
  output$plot<- renderHighchart({
    
    Label1<- c("A","A","B","B")
    Label2<- c("1","2","1","2")
    Val<- runif(4,0,100)
    col<-c("#d21e1e","#009beb","#ff5a1a","#009beb")
    Data<-data.frame(Label1,Label2,Val,col)
 
    highchart(type="chart") %>%
      hc_add_series(data = Data,type = "column",
                    hcaes(x = Label1,
                          y = Val,
                          group = Label2,
                          color = col),
                    dataLabels = list(enabled = TRUE, format='{point.mean}'))%>%
      hc_legend(enabled = F)%>% 
      hc_xAxis(type= 'category', useHTML=T, labels=list(formatter = JS("function(){
                                                          if(this.value == 'A'){
                                                            return '<img src=\"logoA.svg\"></img>';
                                                          }else if(this.value == 'B')
                                                            return '<img src=\"logoB.svg\"></img>';
                                                          }")) )

  })
}

ui <- fluidPage(
  tags$div(
    HTML("<img src=\"logoA.svg\"></img>")
  ),
  
  highchartOutput("plot")
  
)

shinyApp(ui = ui, server = server)

Solution

  • You were almost there, just move the useHTML=T parameter from the hc_xAxis function to the labels list:

        highchart(type = "chart") %>%
            hc_add_series(
                data = Data,
                type = "column",
                hcaes(
                    x = Label1,
                    y = Val,
                    group = Label2,
                    color = col
                ),
                dataLabels = list(enabled = TRUE, format = '{point.mean}')
            ) %>%
            hc_legend(enabled = F) %>%
            hc_xAxis(type = 'category',
                     labels = list(
                         formatter = JS(
                             "function(){
                                  if(this.value == 'A'){
                                    return '<img src=\"logoA.svg\"></img>';
                                  }else if(this.value == 'B')
                                    return '<img src=\"logoB.svg\"></img>';
                                  }"
                         ),
                         useHTML = T
                     ))