google colab with R no rendering plotly figures - r

Im trying to use the plotly library on google colab using R, i created a new colaboratory notebook with R kernel , tried this simple code from plotly's documentation and result is white cell (seems size of histogram but its not rendering) anyone did experience this issue?
p <- plot_ly(x = ~rnorm(50), type = "histogram")
p

Related

Rstudio viewer pane is not working, for plotly and leaflet visualizations?

I'm using latest version of rstudio, I wrote this code:
library(tidyverse)
library(plotly)
data("BOD")
head(BOD)
ggplotly(ggplot(BOD, aes(x = Time, y = demand)) +
geom_point())
and my rstudio viewer tab showed this not any plotly graph!!!!
/session/viewhtml17e429a6510a/index.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A12450 not found
I searched about it everywhere but did not got anything, I'll be so thankful if you can help me in that.

Plotly error "WebGL is not supported by your browser"

I am trying to create some 3D plots using plotly package.
The code that I am using is their tutorial example
library(plotly)
# volcano is a numeric matrix that ships with R
fig <- plot_ly(z = ~volcano)
fig <- fig %>% add_surface()
fig
However, I get the following output. I already followed the guideline in https://www.thewindowsclub.com/webgl-is-not-supported-on-chrome-firefox but still did not work. Any idea why?
I solved it forcing RStudio to use 'Desktop OpenGL'. In RStudio, I went to [Tools/Global Options.../Advanced] and choosing 'Desktop OpenGL' in Rendering engine.
Source:
community.rstudio.com: WebGL is not supported by your browser - Plotly
Related Plotly GitHub Issue 3573

R visuals not producing graphs in power bi

I am trying to create some R visuals in Power BI using the googleVis and/or plotly libraries, but no matter what I do, I can’t get Power BI to display anything. It always just says “No image was created. The R code didn’t result in creation of any visuals. Make sure your R script results in a plot to the R default device.” The issue occurs with plotly and googleVis libraries, so I think it may have something to do with the fact that they’re both browser-based outputs. Per Microsoft, plotly is supported in Power BI. I was hoping someone could tell me why I can’t get any of these example scripts to work in Power BI.
Example code which works in R, but not pbi.
plotly
library(plotly)
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p
googleVis
df=data.frame(country=c("US", "GB", "BR"),
val1=c(10,13,14),
val2=c(23,12,32))
Line <- gvisLineChart(df)
plot(Line)
Now, we can create RHTML custom visual in power BI
1) we can use Ploty - to use it you have load you "midwest" data in power BI table
2) then drag it to R script so it will available to R Script .
3) then run R script , it will work
You can render charts created by plotly as PNG:
p <- plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))
plotly_IMAGE(p, format = "png", out_file = "out.png")
But the problem with this is that, though rendered by plotly, the visualizations will not be interactive since it's just a PNG image.
If you want to create interactive visualizations using plotly. The only way you can do so far is to create a custom Power BI visualization and import it to your report. See this post for a good introduction.

Export the graph generated by rCharts in shiny application

I want to be able to export the charts generated in my shiny application using rCharts to Image and PDF formats. Is there any provision in the rCharts library for that?
I have earlier used ggvis, It gives an option for resizing the chart in the browser and also an option to download the chart in HTML or PNG format. Anything similar to that?
Edit 1:
I'm using nvd3 and polyCharts as my charting libraries currently.
To download as image or pdf you can use a$exporting(enabled = T), assuming your chart is called a.
library(rCharts)
a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "scatter", group = "Exer")
a$exporting(enabled = T)
a
To follow up on my comment above, I was a bit too quick to respond, as the htmlwidget::saveWidget() function is meant for widgets developed under the htmlwidgets.org framework. However, rCharts has a similar function:
library(rCharts)
a <- nPlot(Pulse ~ Height, data = MASS::survey, type = "scatterChart", group = "Exer")
a$save("demo.html", standalone=TRUE)
Where 'demo.html' is standalone html file. Creating a png is as simple as taking a screenshot. Note that you can also call this function in a shiny app.

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

Resources