Why is my 3D plot not showing up in R Studio plot viewer?

13.6k views Asked by At

I normally don't have a problem viewing plots in RStudio (version 1.0.44), but when I try to view a 3D plot created with the "rgl" package, nothing appears in my RStudio plot viewer.

I am able to plot without a problem the plot(cars) scatterplot.

This is the code I am using:

install.packages("rgl")
library(rgl)

plot3d(iris[1:3])  # Nothing appears in RStudio plot viewer

plot(cars)  # The cars scatterplot appears in my RStudio plot viewer
dev.off()

Any ideas why this might be so and what else I should try? I am not receiving any errors when I run plot3d(iris[1:3]). I checked with head(iris[1:3]) to make sure I was loading in the iris data correctly. I've also tried changing the plot margins:

par(mar=c(4,4,4,4)

Thank you

3

There are 3 answers

1
hrbrmstr On BEST ANSWER
  • "Output may be on screen using OpenGL, or to various standard 3D file formats including WebGL, PLY, OBJ, STL" ;
  • "There are two ways in which rgl scenes are normally displayed within R. The older one is in a dedicated window. In Unix-alikes this is an X11 window; it is a native window in Microsoft Windows. On MacOS, the XQuartz system (see http://xquartz.org) needs to be installed to support this."
  • "The newer way to display a scene is by using WebGL in a browser window or in the Viewer pane in RStudio. To select this, set options(rgl.printRglwidget = TRUE). Each operation that would change the scene will return a value which triggers a new WebGL display when printed."

Documentation FTW

0
thistleknot On

library(rgl)

pca3d(set.final.pca, group=set.final$Groups,show.labels = TRUE,show.centroids = TRUE,show.ellipses=FALSE, show.group.labels=FALSE) rglwidget()

0
Grec001 On

maybe a little bit late, but you can try this, the key would be to tell rgl there is no display and turn it to the widget device.

I tried it on my ubuntu rstudio server, and it works.

rglwidget: An htmlwidget to hold an rgl scene

save <- getOption("rgl.useNULL")
options(rgl.useNULL=TRUE)
example("plot3d", "rgl")
widget <- rglwidget()
if (interactive())
  widget
  

# Save it to a file.  This requires pandoc
filename <- tempfile(fileext = ".html")
htmlwidgets::saveWidget(rglwidget(), filename)
browseURL(filename)