Trying to make movie of 3D PCA plot (rgl) using movie3d - r

I have made a rgl 3D PCA plot in R using the pca3d package, and I am trying to make a gif file containing a movie of the rotating plot using movie3d. There is a function in the pca3d package (makeMoviePCA), that passes its arguments to movie3d. Here is the command I am using:
pca3d(pc, components = 1:3, title=TRUE, col=col_conditions, radius=2)
makeMoviePCA(dir="/Users/workdir", clean=TRUE, type = "gif", movie = "movie", convert=TRUE)
This plays the movie like I want to, but does not save it into a gif file, and displays the following error:
Writing 'movie100.png'
...
Writing 'movie120.png'
Error in system("convert --version", intern = TRUE) :
error in running command
sh: convert: command not found
EDIT: I fixed this by telling R where "convert" is by using
Sys.setenv(PATH=paste("/opt/local/bin", Sys.getenv("PATH"), sep=":"))
Thanks!

I fixed this by telling R where "convert" is by using
Sys.setenv(PATH=paste("/opt/local/bin", Sys.getenv("PATH"), sep=":"))

Related

How to export sf object to GDB using RPyGeo in R (Windows)?

I have a bunch of sf objects I'd like to export to GDB from R. I'm running R 4.0.2 on Windows 10. In this case the sf objects are all vector point data. The main reasons to export to GDB are to keep longer field names (the shapefile truncation is very annoying), and because GDBs are more desirable storage locations for our workflows.
Yes, I know about the ArcGisBinding package. I've got it to work in a test script but it's pretty unstable - often crashing and requiring a restart of R. This is a problem, because the sf objects I'd like to export come after an already long Rmd that reads in, formats and cleans the data. So it's not a simple manner of re-running the script until arc.write doesn't break. I could break up the script, but then I'd still have to read in a bunch of shapefiles. One option I haven't yet explored is using reticulate to call a python script instead of trying to do everything in R, but we're trying to do our analysis all in one place, if possible.
I'm pretty sure I've managed to set up RPyGeo appropriately, first setting my python path using the reticulate package. I'm doing it this way because IT restrictions means I can't edit PATH variables on my machine.
#package calls
library(sf)
library(spData)
library(reticulate)
#set python version in reticulate
py_path <- "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
reticulate::use_python(python = py_path, required = TRUE)
#call RPyGeo
library(RPyGeo) # for potential point export
#output gdb
out.gdb <- "C:/LOCAL_PROJECTS/Output/Output.gdb"
#RPyGeo Parameters
# Note that, in order to use RPyGeo you need a working ArcMap or ArcGIS Pro installation on your computer.
# python path - note that this will change depending on which version of Arc one is using
# py_path <- "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
arcpy <- rpygeo_build_env(workspace = out.gdb,
overwrite = TRUE,
extensions = c("Spatial","DataInteroperability"),
path = py_path)
I've tried a bunch of different tools to export an sf object, here using dummy data also used in the RPyGeo vignette
data(nz, package = "spData")
arcpy$Copy_management(in_data = nz,out_data = "nz_test")
arcpy$Copy_management(in_data = nz,out_data = file.path(out.gdb,"nz"))
arcpy$FeatureClassToGeodatabase_conversion(Input_Features = nz,Output_Geodatabase = out.gdb)
arcpy$FeatureClassToFeatureClass_conversion(in_features = nz,out_path = out.gdb,out_name = "nz")
arcpy$QuickExport_interop(Input = nz,Output = file.path(out.gdb,"nz"))
arcpy$CopyFeatures_management(in_features = nz,out_feature_class = file.path(out.gdb,"nz"))
arcpy$CopyFeatures_management(in_features = nz,out_feature_class = "nz")
Each time I get an error, for example:
Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: Object: Error in executing tool
Detailed traceback:
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3232, in CopyFeatures
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3229, in CopyFeatures
retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
I'm not an expert in ArcPy by any means. Nor am I an expert in tracing errors inside packages. Am I making a simple syntax mistake? Is there something else that I'm missing? Any help would be much appreciated!

Converting ImageJ ROI to R spatstat data doesn't work

I tried to use the RImageJROI package from David C Sterratt to transfer multiple ROIs into R and convert them to Spatstat.
This is a minimal example of my R-code
library(RImageJROI)
ROI = read.ijzip("path/ROI.zip")
spat.ROI = ij2spatstat(ROI)
Reading the zip-file works well but when I run the converting command I get the following error:
Error in conv.fun(k, window = window, unitname = unitname, scale =
scale, : object 'out' not found
Attached there is a file for creating a ROI.zip -file which causes the error after processing it in ImageJ with:
run("Analyze Particles...", "add");
roiManager("Save", "Path\\Roi.zip");
Is there a way to get RImageJROI working, am I missing something obvious, or does anyone know another solution for my project?
You can use Bio7 for that with has special methods to transfer different ImageJ ROI's and image data to R.
Some methods where especially designed for spatstat after a great spatstat tutorial on the R conference in 2015.
Here a link to some ImageJ spatstat notes and simple scripts:
http://bio7.org/?p=2618
https://github.com/Bio7/Bio7_Workshop
Here some video tutorials to transfer ROI data:
3D Point Pattern:
https://youtu.be/DmfSASgJa_g
Line Segments:
https://youtu.be/EPan7kibYpo
Polygons:
https://youtu.be/bS_2ejOt7Tg
Point Patterns:
https://youtu.be/7t5V2o8jFJw
Particle Measurments:
https://youtu.be/7t5V2o8jFJw
Georeferenced Polygons (which can be converted to spatstat objects):
https://youtu.be/P2NflfBB2Tg
I got the same error message. I found this to work after looking inside the ij2spatstat function.
library(RImageJROI)
library(spatstat)
roi <- read.ijroi("RoiSet/0071-0081.roi") # path to single ROI
poly <- list(x = roi$coords[,2], y = roi$coords[,1])
out <- owin(poly = poly)
plot(out)
You can put this inside a loop to transform every ROI in a directory.

How to display Markov Chain plots in Rstudio (markovchain package)

Latest Rstudio and latest R ( in Ubuntu Linux 12.04 - 32bit ).
Most plots will display OK in my Rstudio [Plots] pane.
But some Markov Chain plots throw an Error and don't display.
Here's a very simple example code:
library(markovchain) #loading the package
myMatr<-matrix(c(0,.2,.8,.1,.8,.1,.3,0,.7),byrow=TRUE,nrow = 3)
## defining a transition matrix
rownames(myMatr)<-colnames(myMatr)<-c("a","b","c")
myMc<-as(myMatr, "markovchain")
plot(myMc) # <== Error message starts after this...
gives this Rstudio Console Error:
(and no plot is displayed in Rstudio)
"Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE, : 'x' must be numeric"
Yet, exactly the same R commands (as above)
work fine in "stand-alone" R 3.2.3! (outside of Rstudio).
And the Markov Chain plot is displayed ok in a new R-window...
Q:
- Is there an easy way/command to tell Rstudio
that I want certain plots
to open in a new R window (just like in pure R),
and NOT in the Rstudio Plot pane?
- Or some other easy solution?

R not producing a figure in jupyter (IPython notebook)

I am very excited about using python and R together and tried using R in Jupyter (ipython notebbok), however, I could not generate figures in the R kernel.
The error message shown below is displayed when I try to plot figures. I highly appreciate your help.
I am using Anaconda and windows 8.
x<-1:10
plot(x)
Error in png(tf, width, height, "in", pointsize, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
Error in jpeg(tf, width, height, "in", pointsize, quality, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
The following is displayed in the Anaconda command prompt:
Unable to load 'C:/Users/aa/Anaconda/R/library/grDevices/libs/x64/winCairo.dll'
I found a solution from the post on this group.
Solution I did:
I had the most release of R (R 3.2.0) and following the discussion in the above link, I installed R 3.1.3 and copied winCairo.dll from C:\Program Files\R\R-3.1.3\library\grDevices\libs\x64 to C:\Anaconda\R\library\grDevices\libs\x64.
Copying winCairo.dll from R 3.2.0 does not work for my case.
With this, it plots a figure but with an error message that says "Error in replayPlot(obj): invalid graphics state"
Then, thanks to this link,
I added 'options(jupyter.plot_mimetypes = 'image/png')' and it works fine.
options(jupyter.plot_mimetypes = 'image/png')
x=1:10
plot(x)

Points3D Function in R

I have the scatterplot3d package installed in R. When I load it with library(scatterplot3d) or require(scatterplot3d) I am able to create a 3d scatter plot. However, when I try to use the points3d function I get the following error:
Error: could not find function "points3d"
I tried reinstalling the package to no avail (both inside R and as a tarball using R CMD INSTALL in the command line). I am running Xubuntu 12.10 kernel 3.8.7-030807-generic and R version 2.15.3 (2013-03-01).
Entering locate points3d in the command line gave me no results.
I also tried the par.mar default settings command as explained in the manual.
scatterplot3d does an interesting object-oriented twist on the usual R practice. The object returned from the function call includes the points3d function as built-in part of the object but it is not in the Global environment. It is intended that you add to the existing plot-object using that "attached" function that is not a free-living organism but rather a domesticated animal that only exists in the object corral, so you would use this as your syntax:
object$point3d(x,y,z)
I do "feel your pain" but can show you how to overcome the frustration, since I created a working example yesterday: Using scatterplot3d to plot a sphere
You need to intall the package plot3D in the usual way via
install.packages("plot3D")
Then you just need to import, generate the dataset and use the function points3D()
library(plot3D)
x = rnorm(100)
y = rnorm(100)
z = x + y + rnorm(100,0,1)
points3D(x, y, z, col = rainbow(1000))
This is the plot generated by the code above

Resources