Search code examples
rgraphicsshinyr-highcharter

Highcharter - Deselect variables in the legend in the code On Rstudio


I'm currently working on a database with lots of variables, the problem is that it's horrible in a graphic. But with highcharter, I founded that I can click in the legend to select/deselect a variable. The problem is that I don't found anything to do this directly with R code.

I don't want to hide it totally in the legend like it can be done with showInLegend or hideInLegend.

Picture that show before deselecting:

Picture that show before deselecting

Picture that show after deselecting:

[Picture that show after deselecting2


Solution

  • Finally, I used the javascript code :

    jsCode <- JS("function(event) {
    if (!this.visible )
    return true;
    
    var seriesIndex = this.index;
    var series = this.chart.series;
    
    for (var i = 0; i < series.length; i++)
    {
    if (series[i].index != seriesIndex)
    {
    series[i].visible ? series[i].hide() : series[i].show();
    } 
    }
    return false;
    }")
    

    To use it on your highcharts graphics, add :

      hc_plotOptions(series = list(events = list(legendItemClick = jsCode)))