Search code examples
rplotly

Change plotly html plot title in browser tab


I use plotly to visualize my data by saving it as an html widget and open it in the browser as I find the native viewer to be quite slow.

CODE FOR OFFLINE PLOT

library(plotly)
libraray(htmlwidgets)

offlinePlot <- function(pname, fname, browse = T){
 saveWidget(as_widget(x = pname), file = fname)
 if(browse){
 browseURL(fname)
 }
}

offlinePlot(pname = sampleplot, fname = sampleplot.html, browse = T)

Generally, I have a few plots open in the browser at the same time and they render fine but all plots have the same title in the browser tabs, is there a way to change this and set the tab title to something custom?

There was a post on something similar linking to this page: https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js but I don't quite get it.

enter image description here


Solution

  • You need to specify the title for the tab in htmlwidgets::saveWidget by adding title argument

    library(plotly)
    
    x <- c(1:100)
    random_y <- rnorm(100, mean = 0)
    data <- data.frame(x, random_y)
    
    p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
    
    
    htmlwidgets::saveWidget(as_widget(p), "graph.html",title = "my title")
    

    enter image description here