Constructor to instantiate Package class.

Package.load(descriptor = list(), basePath = NA, strict = FALSE)

Arguments

descriptor

Data package descriptor as local path, url or object.

basePath

Base path for all relative paths

strict

Strict flag to alter validation behavior. Setting it to TRUE leads to throwing errors on any operation with invalid descriptor.

See also

Examples

# Load local descriptor descriptor <- system.file('extdata/dp1/datapackage.json', package = "datapackage.r") dataPackage <- Package.load(descriptor) dataPackage$descriptor
#> $name #> [1] "abc" #> #> $resources #> $resources[[1]] #> $resources[[1]]$name #> [1] "random" #> #> $resources[[1]]$format #> [1] "csv" #> #> $resources[[1]]$path #> $resources[[1]]$path[[1]] #> [1] "data.csv" #> #> #> $resources[[1]]$schema #> $resources[[1]]$schema$fields #> $resources[[1]]$schema$fields[[1]] #> $resources[[1]]$schema$fields[[1]]$name #> [1] "name" #> #> $resources[[1]]$schema$fields[[1]]$type #> [1] "string" #> #> #> $resources[[1]]$schema$fields[[2]] #> $resources[[1]]$schema$fields[[2]]$name #> [1] "size" #> #> $resources[[1]]$schema$fields[[2]]$type #> [1] "number" #> #> #> #> #> $resources[[1]]$profile #> [1] "data-resource" #> #> $resources[[1]]$encoding #> [1] "utf-8" #> #> #> #> $views #> $views[[1]] #> $views[[1]]$type #> [1] "vegalite" #> #> $views[[1]]$spec #> $views[[1]]$spec$data #> $views[[1]]$spec$data$resource #> [1] "random" #> #> #> $views[[1]]$spec$mark #> [1] "bar" #> #> $views[[1]]$spec$encoding #> $views[[1]]$spec$encoding$x #> $views[[1]]$spec$encoding$x$field #> [1] "name" #> #> $views[[1]]$spec$encoding$x$type #> [1] "ordinal" #> #> #> $views[[1]]$spec$encoding$y #> $views[[1]]$spec$encoding$y$field #> [1] "size" #> #> $views[[1]]$spec$encoding$y$type #> [1] "quantitative" #> #> #> #> #> #> #> $profile #> [1] "data-package" #>
# Retrieve Package Descriptor descriptor2 <- '{"resources": [{"name": "name", "data": ["data"]}]}' dataPackage2 <- Package.load(descriptor2) dataPackage2$descriptor
#> $resources #> $resources[[1]] #> $resources[[1]]$name #> [1] "name" #> #> $resources[[1]]$data #> $resources[[1]]$data[[1]] #> [1] "data" #> #> #> $resources[[1]]$profile #> [1] "data-resource" #> #> $resources[[1]]$encoding #> [1] "utf-8" #> #> #> #> $profile #> [1] "data-package" #>
# Expand Resource Descriptor descriptor3 <- helpers.from.json.to.list('{"resources": [{ "name": "name", "data": ["data"] }] }') dataPackage3 <- Package.load(descriptor3) dataPackage3$descriptor
#> $resources #> $resources[[1]] #> $resources[[1]]$name #> [1] "name" #> #> $resources[[1]]$data #> $resources[[1]]$data[[1]] #> [1] "data" #> #> #> $resources[[1]]$profile #> [1] "data-resource" #> #> $resources[[1]]$encoding #> [1] "utf-8" #> #> #> #> $profile #> [1] "data-package" #>
# Expand Tabular Resource Schema descriptor4 <- helpers.from.json.to.list('{ "resources": [{ "name": "name", "data": ["data"], "profile": "tabular-data-resource", "schema": { "fields": [{ "name": "name" }] } }] }') dataPackage4 <- Package.load(descriptor4) dataPackage4$descriptor
#> $resources #> $resources[[1]] #> $resources[[1]]$name #> [1] "name" #> #> $resources[[1]]$data #> $resources[[1]]$data[[1]] #> [1] "data" #> #> #> $resources[[1]]$profile #> [1] "tabular-data-resource" #> #> $resources[[1]]$schema #> $resources[[1]]$schema$fields #> $resources[[1]]$schema$fields[[1]] #> $resources[[1]]$schema$fields[[1]]$name #> [1] "name" #> #> $resources[[1]]$schema$fields[[1]]$type #> [1] "string" #> #> $resources[[1]]$schema$fields[[1]]$format #> [1] "default" #> #> #> #> $resources[[1]]$schema$missingValues #> $resources[[1]]$schema$missingValues[[1]] #> [1] "" #> #> #> #> $resources[[1]]$encoding #> [1] "utf-8" #> #> #> #> $profile #> [1] "data-package" #>
# Expand Tabular Resource Dialect descriptor5 <- helpers.from.json.to.list('{ "resources": [{ "name": "name", "data": ["data"], "profile": "tabular-data-resource", "dialect": { "delimiter": "custom" } }] }') dataPackage5 <- Package.load(descriptor5) dataPackage5$descriptor
#> $resources #> $resources[[1]] #> $resources[[1]]$name #> [1] "name" #> #> $resources[[1]]$data #> $resources[[1]]$data[[1]] #> [1] "data" #> #> #> $resources[[1]]$profile #> [1] "tabular-data-resource" #> #> $resources[[1]]$dialect #> $resources[[1]]$dialect$delimiter #> [1] "custom" #> #> $resources[[1]]$dialect$doubleQuote #> [1] TRUE #> #> $resources[[1]]$dialect$lineTerminator #> [1] "\r\n" #> #> $resources[[1]]$dialect$quoteChar #> [1] "\"" #> #> $resources[[1]]$dialect$skipInitialSpace #> [1] TRUE #> #> $resources[[1]]$dialect$header #> [1] TRUE #> #> $resources[[1]]$dialect$caseSensitiveHeader #> [1] FALSE #> #> #> $resources[[1]]$encoding #> [1] "utf-8" #> #> #> #> $profile #> [1] "data-package" #>
# Add, Get and Remove Package Resources descriptor6 <- helpers.from.json.to.list( system.file('extdata/dp1/datapackage.json', package = "datapackage.r")) dataPackage6 <- Package.load(descriptor6) resource6 <- dataPackage6$addResource( helpers.from.json.to.list('{"name": "name", "data": ["test"]}')) dataPackage6$resources[[2]]$source
#> [[1]] #> [1] "test" #>
# Get resource dataPackage6$getResource('name')
#> <Resource> #> Public: #> checkRelations: function () #> clone: function (deep = FALSE) #> commit: function (strict = NULL) #> descriptor: active binding #> errors: active binding #> headers: active binding #> infer: function () #> initialize: function (descriptor, basePath, strict = FALSE, dataPackage = list()) #> inline: active binding #> iter: function (relations = FALSE, options = list()) #> local: active binding #> multipart: active binding #> name: active binding #> profile: active binding #> rawIter: function (stream = FALSE) #> rawRead: function () #> read: function (relations = FALSE, ...) #> remote: active binding #> save: function (target) #> schema: active binding #> source: active binding #> table: active binding #> tabular: active binding #> valid: active binding #> Private: #> basePath_: C:/Users/kleanthis-okfngr/Documents/datapackage-r/docs/r ... #> build_: function () #> currentDescriptor_: list #> dataPackage_: Package, R6 #> errors_: list #> getRelations_: function () #> getTable_: function () #> nextDescriptor_: list #> profile_: Profile, R6 #> relations_: NULL #> sourceInspection_: list #> strict_: FALSE #> table_: NULL
# Remove resource dataPackage6$removeResource('name')
#> <Resource> #> Public: #> checkRelations: function () #> clone: function (deep = FALSE) #> commit: function (strict = NULL) #> descriptor: active binding #> errors: active binding #> headers: active binding #> infer: function () #> initialize: function (descriptor, basePath, strict = FALSE, dataPackage = list()) #> inline: active binding #> iter: function (relations = FALSE, options = list()) #> local: active binding #> multipart: active binding #> name: active binding #> profile: active binding #> rawIter: function (stream = FALSE) #> rawRead: function () #> read: function (relations = FALSE, ...) #> remote: active binding #> save: function (target) #> schema: active binding #> source: active binding #> table: active binding #> tabular: active binding #> valid: active binding #> Private: #> basePath_: C:/Users/kleanthis-okfngr/Documents/datapackage-r/docs/r ... #> build_: function () #> currentDescriptor_: list #> dataPackage_: Package, R6 #> errors_: list #> getRelations_: function () #> getTable_: function () #> nextDescriptor_: list #> profile_: Profile, R6 #> relations_: NULL #> sourceInspection_: list #> strict_: FALSE #> table_: NULL
dataPackage6$getResource('name')
#> NULL
# Modify and Commit Data Package descriptor7 <- helpers.from.json.to.list( '{"resources": [{"name": "name", "data": ["data"]}]}') dataPackage7 <- Package.load(descriptor7) dataPackage7$descriptor$resources[[1]]$name <- 'modified' ## Name did not modified. dataPackage7$resources[[1]]$name
#> [1] "name"
## Should commit the changes dataPackage7$commit() # TRUE - successful commit
#> [1] TRUE
dataPackage7$resources[[1]]$name
#> [1] "modified"