R/buildBoxes.R

#' Build Boxes In A Plot Space
#'
#' This function builds boxes based on corner vectors given by x and y.
#'
#' @param x A vector of x values representing the corners of boxes along the
#'   X-axis. If the x and y-values are identical, only include the first corner
#'   vector, and the function will use it for the y-values as well.
#' @param y A vector of y values representing the corners of boxes along the
#'   Y-axis. See above for more detail about symmetrical plots.
#'
#' @return The parameters should be generated by getSubplotCoords, or you can
#'   enter them manually. This function returns a data.frame listing (x,y) of
#'   the corners of each subplot, arbitrarily numbered. The columns are
#'   "POINT_X", "POINT_Y", and "Subplot", reflecting the standard format taken
#'   by TrapUTM.
#' @export
#'
#' @examples
#' xAndY <- getSubplotCoords()
#' head(buildBoxes(xAndY, xAndY))

buildBoxes <- function(x, y=NULL){

    if(!is.data.frame(y)){
      y <- x
    }

  ## get the number of subplots by row and column
  numxbox <- length(x)-1
  numybox <- length(y)-1
  ## total number of subplots needed
  boxes <- numxbox * numybox
  ## number of rows for corners
  rows <- boxes * 4

  ## rep x appropriately
  pointx <- sort(c(rep(x[2:numxbox], 4),
                   rep(x[1], 2),
                   rep(x[numxbox+1], 2)))

  ## dummy value for pointy
  pointy <- vector()
  ## get pointy values, varied appropriately to match the x vals
  for(i in 1:numybox){
    pointy <- c(pointy,rep(c(y[i], y[i+1]),numxbox*2))
  }

  response <- data.frame(POINT_X=pointx,
                         POINT_Y=pointy,
                         Subplot=sort(rep(1:(rows/4), 4)),
                         stringsAsFactors = FALSE)
  ##for each X, up to the second to last one...

  return(response)
}
ecology-rocks/disperseR documentation built on May 15, 2019, 7:58 p.m.