-2

I am using the rLiDAR package to plot a forest stand using the LiDARForestStand function, my goal for today is to create an interactive .html file for the forest stand using the rgl.widget function, but I am not sure how to embed the for loop and the LiDARForestFunction into the process for creating the HTML file. The specific code lines for this purpose is as follows and contains the example code from rgl documentation, so it has not been modified as such. When I use the plot3d function, it just plots the scatter-plot, so I guess the rgl widget will also just create a HTML of this scatter-plot rather than the forest stand. enter image description here

**Update (minimal reproducible code) **

   #=======================================================================#
#=======================================================================#
#Plotting a forest plantation stand in virtual 3-D space
#=======================================================================#
# Setup the forest stand dimensions 
xlength<-30 # x length
ylength<-20 # y length
# Set the space between trees
sx<-3 # x space length
sy<-2 # y space length
# Tree location grid
XYgrid <- expand.grid(x = seq(1,xlength,sx), y = seq(1,ylength,sy))
# Getting the number of trees
N_Trees<-nrow(XYgrid)
# Plot a virtual Eucalyptus forest plantation stand using the halfellipsoid tree crown shape
# Setup the stand tree parameters
meanHCB<-5 # mean height at canopy base
sdHCB<-0.1 # standard deviation of the height at canopy base
HCB<-rnorm(N_Trees, mean=meanHCB, sd=sdHCB) # height at canopy base
CL<-HCB # tree crown height
CW<-HCB*0.6 # tree crown diameter
library(rgl)
library(raster)
library(rLiDAR)
open3d() # open a rgl window
# Plotting the stand
for( i in 1:N_Trees){
  LiDARForestStand(crownshape = "halfellipsoid", CL = CL[i], CW = CW[i],
                   HCB = HCB[i], X = XYgrid[i,1], Y = XYgrid[i,2], dbh = 0.4,
                   crowncolor = "forestgreen", stemcolor = "chocolate4",
                   resolution="high", mesh=TRUE)
}
plot3d(x = XYgrid[i,1], Y = XYgrid[i,2], xlab = "X Coord", ylab = " Y Coord", zlab = "Height")
scene3d()
#Creating an interactive HTML window
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)

1 Answers1

-1

I finally worked it out using the following the code. I had to make some changes. Thank you everyone for your help.

#=======================================================================#
#=======================================================================#
#Plotting a forest plantation stand in virtual 3-D space
#=======================================================================#
# Setup the forest stand dimensions 
xlength<-30 # x length
ylength<-20 # y length
# Set the space between trees
sx<-3 # x space length
sy<-2 # y space length
# Tree location grid
XYgrid <- expand.grid(x = seq(1,xlength,sx), y = seq(1,ylength,sy))
# Getting the number of trees
N_Trees<-nrow(XYgrid)
# Plot a virtual Eucalyptus forest plantation stand using the halfellipsoid tree crown shape
# Setup the stand tree parameters
meanHCB<-5 # mean height at canopy base
sdHCB<-0.1 # standard deviation of the height at canopy base
HCB<-rnorm(N_Trees, mean=meanHCB, sd=sdHCB) # height at canopy base
CL<-HCB # tree crown height
CW<-HCB*0.6 # tree crown diameter
library(rgl)
library(raster)
library(rLiDAR)
library(rglwidget)
#open3d() # open a rgl window
# Plotting the stand
for( i in 1:N_Trees){
  LiDARForestStand(crownshape = "halfellipsoid", CL = CL[i], CW = CW[i],
                   HCB = HCB[i], X = XYgrid[i,1], Y = XYgrid[i,2], dbh = 0.4,
                   crowncolor = "forestgreen", stemcolor = "chocolate4",
                   resolution="high", mesh=TRUE)
}
HTML <- rglwidget(elementId = "Plot3D",width=500, height=300)
# Exporting HTML file
htmlwidgets::saveWidget(rglwidget(), "D:/Summer_Work/Test.html")
  • You should not use `library(rglwidget)`. As the message says when you load it, "The functions in the `rglwidget` package have been moved to `rgl`." I'd also suggest using an R Markdown document; then you can add explanatory text, and simply run `rglwidget()` to insert the plot where you want it. – user2554330 Jun 14 '20 at 15:07