R/ensureAupusParameter.R

##' Ensure AUPUS parameters
##' 
##' This function performs several checks on the AUPUS parameters to ensure it
##' is valid.  Typically, these parameters are created by getAupusParameter and
##' should have no issues, but these parameters could be modified by the user.
##' 
##' @param aupusParam The AUPUS parameter list, typically as generated by
##' getAupusParameter.
##' 
##' @return No data is returned, but errors are raised if the aupusParam
##' argument is invalid.
##' 
##' @export
##' 

ensureAupusParameter = function(aupusParam){

    ## General tests
    stopifnot(names(aupusParam) == c("areaCode", "itemCode", "elementCode", "year",
                                "keyNames"))
    ## Only run some tests if swsContext data have been defined.
    swsChecks = exists("swsContext.baseRestUrl")
    
    ## areaCode
    stopifnot(is(aupusParam$areaCode, "character"))
    stopifnot(length(aupusParam$areaCode) == 1)
    if(swsChecks){
        validCodes = GetCodeList(domain = "faostat_one", dataset = "FS1_SUA",
                                 dimension = "geographicAreaFS")[, code]
        stopifnot(aupusParam$areaCode %in% validCodes)
    }
    
    ## itemCode
    stopifnot(is(aupusParam$itemCode, "character"))
    if(swsChecks){
        validCodes = GetCodeList(domain = "faostat_one", dataset = "FS1_SUA",
                                 dimension = "measuredItemFS")[, code]
        stopifnot(aupusParam$itemCode %in% validCodes)
    }
    
    ## elementCode
    stopifnot(is(aupusParam$elementCode, "character"))
    if(swsChecks){
        validCodes = GetCodeList(domain = "faostat_one", dataset = "FS1_SUA",
                                 dimension = "measuredElementFS")[, code]
        stopifnot(aupusParam$elementCode %in% validCodes)
    }
    
    ## year
    stopifnot(is(aupusParam$year, "character"))
    if(swsChecks){
        validCodes = GetCodeList(domain = "faostat_one", dataset = "FS1_SUA",
                                 dimension = "timePointYears")[, code]
        stopifnot(aupusParam$year %in% validCodes)
    }
    
    aupusParameterEnsured <<- TRUE
}
SWS-Methodology/faoswsAupus documentation built on May 9, 2019, 11:45 a.m.