diff --git a/R/parseWebReport.R b/R/parseWebReport.R index a8d4a24d..ced767c4 100644 --- a/R/parseWebReport.R +++ b/R/parseWebReport.R @@ -18,30 +18,30 @@ # args = list(msso='2-MIN', fy='2018', asym='%', proj='0') -#' Parse contents of a web report, based on supplied arguments. +#' @title Parse contents of a web report, based on supplied arguments. #' -#' Parse contents of a web report, based on supplied arguments. +#' @description Parse contents of a web report, based on supplied arguments. #' -#' Report argument names can be inferred by inspection of the HTML source +#' @details Report argument names can be inferred by inspection of the HTML source #' associated with any given web report. #' #' @param url Base URL to a LIMS/NASIS web report. +#' #' @param args List of named arguments to send to report, see details. +#' #' @param index Integer index specifying the table to return, or, NULL for a #' list of tables -#' @return A \code{data.frame} object in the case of a single integer passed to -#' \code{index}, a \code{list} object in the case of an integer vector or NULL -#' passed to \code{index}. +#' +#' @return A `data.frame` object in the case of a single integer `index`, otherwise a `list` +#' #' @note Most web reports are for internal use only. +#' #' @author D.E. Beaudette and S.M. Roecker -#' @keywords IO -#' @examples #' -#' \donttest{ -#' # pending -#' } +#' @keywords IO #' #' @export parseWebReport +#' parseWebReport <- function(url, args, index = 1) { # suggested packages @@ -82,25 +82,47 @@ parseWebReport <- function(url, args, index = 1) { return(NULL) # read all of the HTML tables + # result is a list d <- rvest::html_table(x, header = TRUE) # sanity check empty list = no data if (length(d) < 1) return(NULL) - # if specified, get only the indexed table - if (!is.null(index)) { - d <- d[[index]] - } + ## TODO: consider message when length(d) > length(index) - # replace blanks with NA, problem with LIMS reports - idx <- unlist(lapply(d, is.character)) - if (any(idx)) { - d[idx] <- lapply(d[idx], function(x) ifelse(x == "", NA, x)) - } + # iterate over tables + d <- lapply(d, function(i) { + + # replace blanks with NA, problem with LIMS (NASIS Web) reports + idx <- unlist(lapply(i, is.character)) + if (any(idx)) { + i[idx] <- lapply(i[idx], function(x) ifelse(x == "", NA, x)) + } + + # convert to DF + # note: col names aren't legal data.frame names + i <- as.data.frame(i) + return(i) + }) - # note: col names aren't legal data.frame names - # done - return(as.data.frame(d)) + # return requested tables via `index` + # or all tables if NULL + if(is.null(index)) { + + # result is a list + return(d) + } else { + + # single table -> data.frame + if(length(index) == 1) { + return(d[[index]]) + } else { + # multiple tables -> list + return(d[index]) + } + + } + } diff --git a/man/fetchSDA_spatial.Rd b/man/fetchSDA_spatial.Rd index c0fda5be..8f4bb035 100644 --- a/man/fetchSDA_spatial.Rd +++ b/man/fetchSDA_spatial.Rd @@ -19,13 +19,13 @@ fetchSDA_spatial( \arguments{ \item{x}{A vector of map unit keys (\code{mukey}) or national map unit symbols (\code{nmusym}) for \code{mupolygon} geometry OR legend keys (\code{lkey}) or soil survey area symbols (\code{areasymbol}) for \code{sapolygon} geometry. If \code{geom.src="mlrapolygon"} then \code{x} refers to \code{MLRARSYM} (major land resource area symbols).} -\item{by.col}{Column name containing map unit identifier \code{"mukey"}, \code{"nmusym"}/\code{"nationalmusym"} for \code{geom.src="mupolygon"} OR \code{"areasymbol"}, \code{"areaname"}, \code{"mlraoffice"}, \code{"mouagncyresp"} for \code{geom.src="sapolygon"}; default is determined by \code{is.numeric(x)} \code{TRUE} for \code{mukey} or \code{lkey} and \code{nationalmusym} or \code{areasymbol} otherwise.} +\item{by.col}{Column name containing map unit identifier \code{"mukey"}, \code{"nmusym"}/\code{"nationalmusym"} for \code{geom.src} \code{mupolygon} OR \code{"areasymbol"}, \code{"areaname"}, \code{"mlraoffice"}, \code{"mouagncyresp"} for \code{geom.src} \code{sapolygon}; default is determined by \code{is.numeric(x)} \code{TRUE} for \code{mukey} or \code{lkey} and \code{nationalmusym} or \code{areasymbol} otherwise.} \item{method}{geometry result type: \code{"feature"} returns polygons, \code{"bbox"} returns the bounding box of each polygon (via \code{STEnvelope()}), \code{"point"} returns a single point (via \code{STPointOnSurface()}) within each polygon, \code{"extent"} returns an aggregate bounding box (the extent of all polygons, \code{geometry::EnvelopeAggregate()}) ), \code{"convexhull"} (\code{geometry::ConvexHullAggregate()}) returns the aggregate convex hull around all polygons, \code{"union"} (\code{geometry::UnionAggregate()}) and \code{"collection"} (\code{geometry::CollectionAggregate()}) return a \code{MULTIPOLYGON} or a \code{GEOMETRYCOLLECTION}, respectively, for each \code{mukey}, \code{nationalmusym}, or \code{areasymbol }. In the case of the latter four aggregation methods, the groups for aggregation depend on \code{by.col} (default by \code{"mukey"}).} \item{geom.src}{Either \code{mupolygon} (map unit polygons), \code{sapolygon} (soil survey area boundary polygons), or \code{mlrapolygon} (major land resource area boundary polygons)} -\item{db}{Default: \code{"SSURGO"}. When \code{geom.src} is \code{mupolygon}, use STATSGO polygon geometry instead of SSURGO by setting \code{db = "STATSGO"}.} +\item{db}{Default: \code{"SSURGO"}. When \code{geom.src} is \code{mupolygon}, use STATSGO polygon geometry instead of SSURGO by setting \code{db = "STATSGO"}} \item{add.fields}{Column names from \code{mapunit} or \code{legend} table to add to result. Must specify parent table name as the prefix before column name e.g. \code{mapunit.muname}.} diff --git a/man/parseWebReport.Rd b/man/parseWebReport.Rd index 9ab03a79..8164ffab 100644 --- a/man/parseWebReport.Rd +++ b/man/parseWebReport.Rd @@ -15,9 +15,7 @@ parseWebReport(url, args, index = 1) list of tables} } \value{ -A \code{data.frame} object in the case of a single integer passed to -\code{index}, a \code{list} object in the case of an integer vector or NULL -passed to \code{index}. +A \code{data.frame} object in the case of a single integer \code{index}, otherwise a \code{list} } \description{ Parse contents of a web report, based on supplied arguments. @@ -28,13 +26,6 @@ associated with any given web report. } \note{ Most web reports are for internal use only. -} -\examples{ - -\donttest{ -# pending -} - } \author{ D.E. Beaudette and S.M. Roecker