Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using R and plot.ly - how do I script saving my output as a webpage

Tags:

r

plotly

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.

like image 201
Slavatron Avatar asked Jan 03 '16 18:01

Slavatron


People also ask

How do I save an interactive graph in R?

The simplest method is to use the export function from the plotly package. You simply specify which graph to save (called fig in our example) and specify the file location, name, and type with the file argument. This function can save plotly graphs as . png, .

How do you save Plotly plots?

Save Your Plot Once you have your data and plot ready to go, click on SAVE on the left-hand side. Give your PLOT and DATA a filename and select the privacy setting. For more information on how sharing works, including the difference between private, public, and secret sharing, visit this page.

Can you make interactive plots in R?

Both Python and R have packages to build interactive plots. One of the popular python packages for interactive visualization is Bokeh. The Bokeh library allows us to create different plots with very few lines of code with an added flexibility for advanced customization.

How do you share an interactive Plotly chart?

To share a plot from the Chart Studio Workspace, click 'Share' button on the left-hand side after saving the plot. The Share modal will pop-up and display a link under the 'Embed' tab. You can then copy and paste this link to your website. You have the option of embedding your plot as an HTML snippet or iframe.


2 Answers

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") 
like image 165
Andrew Avatar answered Sep 24 '22 15:09

Andrew


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.

like image 27
irritable_phd_syndrome Avatar answered Sep 25 '22 15:09

irritable_phd_syndrome