Saving plot.ly image to RData file - r

I create a plot_ly image using:
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram")
I then want to save it to an RData file to simply open it later (hence pre-compute)
save(MilesPlotly, file = "my/path/here/myPlot.RData")
Later on I want to simply plot it by doing
load(my/path/here/myPlot.RData)
MilesPlotly
Now, this works on Mac. This does not work on my Ubuntu server on AWS.
Does anyone have any ideas why the discrepancy? The plotly version on both is 3.6.0.

Your code doesn't work on my windows environment and plotly_build() solves it (I'm not sure this code works on your env).
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram")
MilesPlotly <- plotly_build(MilesPlotly)
save(MilesPlotly, file = "my/path/here/myPlot.RData")
load("my/path/here/myPlot.RData")
MilesPlotly

After some tweaking, I realized I needed to do two things:
1) I updated R to 3.3.1
2) You need to "build" the plot before saving it. That means:
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram") %>%
build()

Related

saving file in databricks r language

In notebooks with databricks/azure r language, I create output files. How I can save these output files to local directory outside of databricks or open these files in databricks?
Example - I create plotly html plot
library(plotly)
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig
In r studio, when I type "fig", the file automatically opens by itselfs. In databricks notebook I configure and set up cluster compute cores and permissions, then run r code which works. But in databricks notebook, when I type "fig", nothing happen.
I also try to save to my computer - like
list.files(path = getwd(), recursive = TRUE,full.names=TRUE)
saveWidget(fig, file = "dbfs:/path/to/fig.html")
save_html(fig, file = "dbfs:/path/to/mfig.html")
saveWidget(fig, file = paste0(getwd(),"/fig.html"))
I also try but not work
dbutils.fs.cp("dbfs:/path/to/fig.html", "file:/path/to/local/directory/gif.html")
local_path <- paste0(getwd(), "/fig.html")
dbutils.fs.cp("dbfs:/path/to/fig.html", paste0("file:", local_path))
How to automatically get databricks to save and view?

RStudio : what is the code behind the export>save as image button?

I try to find a way to save a plotly graph as png without installing orca (I'm not allowed to).
It appears that if I plot my graphics, lets say :
library(dplyr)
library(plotly)
p <- plot_ly(z = ~volcano) %>% add_surface()
p
and then clic on export>save as image>Save as png, the resulting static plot is available on my computer
But If I try to use classical png() like this :
png("myvolcano.png")
plot_ly(z = ~volcano) %>% add_surface()
dev.off()
I get a blank png...
(while it's working for a classical plot(1) )
How to reproduce by code what I get from the menu bar ?
Thank you!
Hi this is more a comment than an answer but my reputation does not allow me to comment.
You can use 2 strategies to export plotly graphs:
use orca() function in the plotly pkg (if you ?orca will find all the explanations)
use htmlwidgets::saveWidget(p, file = "myvolcano.html") which downloads in html format
In the past there was a function export() but it is deprecated now.
Hope this helps.

rgl.postscript when rgl.useNULL = TRUE

Shouldn't rgl.postscript() work for a headless server, i.e. when options(rgl.useNULL = TRUE)? I know that rgl.snapshot() won't work.
library(rgl)
options(rgl.useNULL = TRUE)
open3d()
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x, y)
plot3d(x, y, z, col = rainbow(1000))
rgl.postscript("test.pdf",fmt="pdf")
This gives me "In rgl.postscript("test.pdf", fmt = "pdf") : Postscript conversion failed".
It could do so in some cases, but currently it doesn't. One issue is that if rgl is started with the null device, it won't even link in the OpenGL functions, and rgl.postscript() uses some of them.
Edit: Sorry, the "no linking" is what I'd like. Currently it does need to link, but it won't run the initialization code, so it should work in contexts (e.g. a headless server) where no display is available.
On a headless server you could use Xvfb for a "virtual frame buffer". I don't have a lot of experience with it, but I think I've heard that it doesn't handle rgl.snapshot properly. I'd expect rgl.postscript to work.
In principle, you could also render in WebGL, and then use some other tool to convert the output to your desired format. I don't know if any such tools exist.

Exporting PNG files from Plotly in R

How can I export a Plotly chart as a image from R using code? (Not using the export button on the chart).
For example, this code from the Plotly site, create this chart:
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)
How can I save it as a image?
The official site has this material in python, but I didn't find something similar in R.
There is a export function which allows you to save images without the need to connect to plotly servers. You can find it in the plotly package doc:
p <- plot_ly(...)
export(p, file = "image.png")
You can even change the file type of output by selecting the extension as .png, jpeg or .pdf.
You can also save the image in html file which allows you the plotly experience like zooming or showing annotations by using htmlwidgets::saveWidget:
p <- plot_ly(...)
htmlwidgets::saveWidget(p, file = "image.html")
In the Plotly docs in CRAN I discovered the function plotly_IMAGE.
Here is a example:
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)
plotly_IMAGE(p, width = 500, height = 500, format = "png", scale = 2,
out_file = "~/desktop/test.png")
UPDATE
plotly_IMAGE works using Plotly server. A local solution is welcome.
Since I am new, I'm unable to comment so I am posting this as a response. The export function is deprecated as of plotly v4.9.0. Instead, the orca function is the suggested method to export plotly objects as static images. Learn more here: orca function R documentation
According to plotly the following should work :
1) Install Orca as described here : https://github.com/plotly/orca
2) You might need to restart your PC
Then run the following code :
There is a export function which allows you to save images without the need to connect to plotly servers. You can find it in the plotly package doc:
p <- plot_ly(...)
orca(p, file = "image.png")
Note : I found giving full path is raising a javascript error. So I just gave filename. The files are being saved in "C:/Users/user/Documents" in Windows.
I wanted to warn other users, that orca changes text when saved.
from my terminal, I have:
I specified Corbal (Body) in my plot.
When I saved using orca(z,width = 8*96, height = 3*96, save_path), I got
when I saved using export(z, file = save_path ), I got:
You can tell that the font family is different. Very annoying

Exporting PNG files from Plotly in R without internet

In this question, Exporting PNG files from Plotly in R I asked how to export Plotly plots to disk.
I used the function plotly_IMAGE, but later discovered that the function uses the Plotly internet servers.
The question is, now that Plotly JavaScript is local, how can I create a png local file without internet?
I tried this code, without success:
library(plotly)
png(filename = "test.png")
plot_ly(x = 1:10)
dev.off()
The idea is to make it programaticaly, without click on export button over the chart.
They've added a new export function to the plotly package. But to my knowledge it does the same thing as #MLavoie's answer suggests. Usage:
p <- plot_ly(...)
export(p, file = "test.png")
You will to need install Phantom (http://phantomjs.org/download.html) which is quite easy and you can try this:
library(plotly)
library(webshot)
library(htmlwidgets)
m <- plot_ly(x = 1:10)
saveWidget(as.widget(m), "temp.html")
webshot("temp.html", file = "test.png",
cliprect = "viewport")
you will find temp.html and temp.png in your working directory.

Resources