37

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.

2 Answers 2

77

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")
Sign up to request clarification or add additional context in comments.

6 Comments

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"
as.widget is part of plotly, but it was only added at the end of December, so you might be running an older version of the library.
Hmmm, just updated plotly (now running version 2.0.16) and I'm still getting the same error...
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.
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")
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.