R/dpfieldnames.R

Defines functions dpfieldnames.dataresource dpfieldnames.tableschema dpfieldnames

Documented in dpfieldnames

#' List the fields in a Data Resource
#' 
#' @param x object for which to get the field names. This can either be a Data
#' Resource or Table Schema.
#'
#' @return
#' Returns a character vector with the fields in the Data Resource.
#'
#' @export
dpfieldnames <- function(x) {
  UseMethod("dpfieldnames")
}

#' @export
dpfieldnames.tableschema <- function(x) {
  # TODO: convert to dpproperty
  fields <- x$fields
  if (is.null(fields)) stop("Fields are missing from Table Schema of Data Resource.")
  sapply(fields, function(f) {
    if (!exists("name", f)) stop("Field without name.")
    f$name
  })
}

#' @export
dpfieldnames.dataresource <- function(x) {
  schema <- dpschema(x)
  if (is.null(schema)) stop("Data Resource does not have a schema property.")
  dpfieldnames(schema)
}
djvanderlaan/datapackage documentation built on March 18, 2024, 4:57 p.m.