R/AdmBuildReport.R

Defines functions AdmBuildReport

Documented in AdmBuildReport

#' Generate VDC build Statistics
#'
#' Pulls entire VDC build counts with labels
#' @param code input string of admin code
#' @param flag string either vdcmun, district, or ward 
#' @param build object from dbReadTable
#' @return list of dataframes containing counts and labels
#' @export
#' @examples
#' AdmBuildReport("524 1 02 07 3 015", vdcmun, build)

AdmBuildReport <- function(code, flag, build) {

  if (flag == "vdcmun") {
    group <- colnames(build)
    binf <- lapply(group, function(x, vc) {
      build %>%
      filter(hrrp_vcode == vc)  %>%
      group_by_("hrrp_vcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(binf) <- group
  }

  else if (flag == "district") {
    group <- colnames(build)
    binf <- lapply(group, function(x, vc) {
      build %>%
      filter(hrrp_dcode == vc)  %>%
      group_by_("hrrp_dcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(binf) <- group
  }

  else {
    group <- colnames(build)
    binf <- lapply(group, function(x, vc) {
      build %>%
      filter(hrrp_wcode == vc)  %>%
      group_by_("hrrp_wcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(binf) <- group
  }


  #Getting labels
	ovals <- lapply(c(1:length(group)), function(x) binf[[x]][[2]])
	bldg_info <- mapply(labelValue, group, ovals, "building")

  #Label handling, messy but working
  bldgi <- lapply(c(1:length(group)), function(x, b, bi) {
      if (anyNA(binf[[x]][2]) == TRUE & (nrow(binf[[x]][2]) > 1) == TRUE) 
          append(bldg_info[[x]], "Not Present")
        else if (length(bldg_info[[x]] > 0))
          bldg_info[[x]]
        else 
          bldg_info[[x]] <- ""
      }, b = binf, bi = bldg_info)
  
  #Add labels, adds blank string if not relevant
  binf.out <- lapply(c(1:length(group)), function(x, b, bi) {
      mutate(b[[x]], label = bi[[x]])}, b = binf, bi = bldgi)
  names(binf.out) <- group

	return(binf.out)
}





#bldg_group <- build %>% group_by(hrrp_vcode, foundatn, roof) %>% filter(hrrp_vcode == "524 1 02 07 3 015")
# Produces sequential counts - i.e. counts both variables together
#wardbldgs <- bldg_group %>% summarize(count =  n())
bradleyswilson/hrrp documentation built on May 28, 2019, 7:13 p.m.