33

I want to make some interactive graphs using R and plot.ly. When I run the following code in R-Studio, it produces an interactive graph.

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
    mode = "markers", color = carat, size = carat)

After producing this graph, when I click on the "Export" button in the Plot window of R-Studio, it gives me the option to save the plot as a webpage. How can I script the process of saving produced plots as webpages? My ultimate goal is to run Rscripts iteratively from inside a bash script to produce multiple webpages.

Slavatron
  • 2,278
  • 5
  • 29
  • 40

2 Answers2

69

Assign the plot_ly object to a variable and then use htmlwidgets::saveWidget() to save the actual file, like so:

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
             mode = "markers", color = carat, size = carat)
htmlwidgets::saveWidget(as_widget(p), "index.html")
PatrickT
  • 10,037
  • 9
  • 76
  • 111
Andrew
  • 36,541
  • 13
  • 67
  • 93
  • 1
    This looks very encouraging. What packages are you using besides plotly and htmlwidgets? I'm getting an error message saying R can't find the function "as.widget". I am running R with the following packages loaded: plotly, htmlwidgets, htmltools, knitr. Should I have another package loaded? Here is the full error message: Error in resolveSizing(x, x$sizingPolicy, standalone = standalone, knitrOptions = knitrOptions) : could not find function "as.widget" – Slavatron Jan 06 '16 at 17:06
  • 2
    `as.widget` is [part of plotly](https://github.com/ropensci/plotly/blob/d16be22f284ad36fba3c14f2b3cc015064e74cf3/man/as.widget.Rd), but it was only added at the end of December, so you might be running an older version of the library. – Andrew Jan 06 '16 at 18:41
  • Hmmm, just updated plotly (now running version 2.0.16) and I'm still getting the same error... – Slavatron Jan 06 '16 at 20:40
  • 3
    CRAN's version of plotly is 2.0.16 and was built on Dec. 20. `as.widget` was added on Dec. 23, so it's not on CRAN yet. Install the development version from GitHub with devtools: `devtools::install_github("ropensci/plotly")` and it should work. – Andrew Jan 06 '16 at 21:12
  • how to save the index.htmlfile locally – sanmoy paul Jan 22 '16 at 10:16
  • 2
    The function will save it to the working directory by default. You can choose whatever path you want, though: `htmlwidgets::saveWidget(as.widget(p), "/path/to/wherever/blah.html")` – Andrew Jan 23 '16 at 04:53
4

Updating Andrew's answer for R-3.5.1 and plotly-4.8.0, i.e. :

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(d, x = ~carat, y = ~price, text=~paste("Clarity : ", clarity))
htmlwidgets::saveWidget(as_widget(p), "index.html")

In order to get this to work, you'll also need to install pandoc. On CentOS/RedHat do yum install pandoc pandoc-citeproc. On Mac OSX, using homebrew, do brew install pandoc.

This solution was tested and works on OSX 10.13.6 works in R-3.5.1.

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60