USDAForestService / FIESTA

FIESTA (Forest Inventory ESTimation and Analysis) is a research estimation tool for analysts that work with sample-based inventory data from the U.S. Department of Agriculture, Forest Service, Forest Inventory and Analysis (FIA) Program. Follow the link below for more information:
https://usdaforestservice.github.io/FIESTA/
24 stars 11 forks source link

sqldf function calls in datSumTree.R #26

Open radt0005 opened 11 months ago

radt0005 commented 11 months ago

This should be reproducible but I'm not sure it qualifies as a bug. It seems like an unanticipated interaction between FIESTA and the RpostgreSQL package. Here are some details:

In a fresh R session run the first modGBpop example from the FIESTA - Green-book Estimators vignette POP1: FIADB POPULATION - Get population data for area and tree estimates for Wyoming, using post-stratification

library(FIESTA)
  GBpopdat <- modGBpop(
  popTabs = list(cond = FIESTA::WYcond,          # FIA plot/condition data
                 tree = FIESTA::WYtree,          # FIA tree data
                 seed = FIESTA::WYseed),         # FIA seedling data
  popTabIDs = list(cond = "PLT_CN"),             # unique ID of plot in cond
  pltassgn = FIESTA::WYpltassgn,  # plot assignments
  pltassgnid = "CN",              # unique ID of plot in pltassgn
  pjoinid = "PLT_CN",             # plot id to join to pltassgn
  unitarea = WYunitarea,          # area by estimation units
  unitvar = "ESTN_UNIT",          # name of estimation unit
  strata = TRUE,                  # if using post-stratification
  stratalut = WYstratalut,        # strata classes and pixels counts
  strata_opts = strata_options(getwt = TRUE)              # strata options
)

next run the first modGBtree example from the same vignette POP1: 1.1 Net cubic-foot volume of live trees, Wyoming, 2011-2013

## Return raw data and titles
## Total net cubic-foot volume of live trees (at least 5 inches diameter), Wyoming, 2011-2013 
tree1.1 <- modGBtree(
    GBpopdat = GBpopdat,         # pop - population calculations
    landarea = "FOREST",         # est - forest land filter
    sumunits = TRUE,             # est - sum estimation units to population
    estvar = "VOLCFNET",               # est - net cubic-foot volume
    estvar.filter = "STATUSCD == 1",   # est - live trees only
    returntitle = TRUE           # out - return title information
    )

Everything works fine to here. But if you load the RpostgreSQL package and run the modGBtree example again it causes an error library(RPostgreSQL)

## Return raw data and titles
## Total net cubic-foot volume of live trees (at least 5 inches diameter), Wyoming, 2011-2013 
tree1.1 <- modGBtree(
    GBpopdat = GBpopdat,         # pop - population calculations
    landarea = "FOREST",         # est - forest land filter
    sumunits = TRUE,             # est - sum estimation units to population
    estvar = "VOLCFNET",               # est - net cubic-foot volume
    estvar.filter = "STATUSCD == 1",   # est - live trees only
    returntitle = TRUE           # out - return title information
    )
> Error in if (nrow(treex) == 0) { : argument is of length zero

Checking for and detaching the RPostgreSQL package before running the modGBtree code will solve the problem. A possible modification to the FIESTA code in datSumTree.R to specify SQLite driver the drv="RSQLite" at line 688 (and possibly line 708) seems to prevent the error in this example: sqldf::sqldf(tree.qry, dbname=dbname, drv="RSQLite") I'm not sure if this would cause other problems, but I wanted to pass it along in case it helps anyone who might have RPostgreSQL loaded in their environment while calling FIESTA functions. Thank you!

tfrescino commented 11 months ago

Thank you for pointing out this issue and for providing detailed information and possible solution. We have anticipated the use of RPostgreSQL, but have focused on SQLite databases, so we have not come across this error. We are working on incorporating a fix for the next version. Your help is much appreciated.