This tutorial will show you how to install the R package for working with Data Packages and Table Schema, load a CSV file, infer its schema, and write a Tabular Data Package.
For this tutorial, we will need the Data Package R package (datapackage.r).
devtools package is required to install the datapackage.r package from github.
# Install devtools package if not already install.packages("devtools")
And then install the development version of datapackage.r from github.
install.packages("datapackage.r") # or install the development package devtools::install_github("frictionlessdata/datapackage.r")
You can start using the package by loading datapackage.r
.
library(datapackage.r)
You can add useful metadata by adding keys to metadata dict attribute. Below, we are adding the required name
key as well as a human-readable title
key. For the keys supported, please consult the full Data Package spec. Note, we will be creating the required resources
key further down below.
dataPackage <- Package.load() dataPackage$descriptor['name'] <- 'period-table' dataPackage$descriptor['title'] <- 'Periodic Table' # commit the changes to Package class dataPackage$commit()
We will use periodic-table data from remote path: https://raw.githubusercontent.com/frictionlessdata/datapackage-r/master/vignettes/exampledata/data.csv
url <- 'https://raw.githubusercontent.com/frictionlessdata/datapackage-r/master/vignettes/exampledata/data.csv' pt_data <- read.csv2(url, sep = ',') knitr::kable(head(pt_data, 10), align = 'c')
We can guess at our CSV's schema by using infer
from the Table Schema package. We pass directly the remote link to the infer function, the result of which is an inferred schema. For example, if the processor detects only integers in a given column, it will assign integer
as a column type.
filepath <- 'https://raw.githubusercontent.com/frictionlessdata/datapackage-r/master/vignettes/exampledata/data.csv' schema <- tableschema.r::infer(filepath)
Once we have a schema, we are now ready to add a resource
key to the Data Package which points to the resource path and its newly created schema. Below we define resources with three ways, using json text format with usual assignment operator in R list objects and directly using addResource
function of Package
class:
# define resources using json text resources <- helpers.from.json.to.list( '[{ "name": "data", "path": "filepath", "schema": "schema" }]' ) resources[[1]]$schema <- schema resources[[1]]$path <- filepath
# or define resources using list object resources <- list(list( name = "data", path = filepath, schema = schema ))
And now, add resources to the Data Package:
dataPackage$descriptor[['resources']] <- resources dataPackage$commit()
Or you can directly add resources using addResources
function of Package
class:
resources <- list(list( name = "data", path = filepath, schema = schema )) dataPackage$addResource(resources)
Now we are ready to write our datapackage.json
file to the current working directory.
dataPackage$save('exampledata')
The datapackage.json
(download) is inlined below. Note that atomic number has been correctly inferred as an integer
and atomic mass as a number
(float) while every other column is a string
.
jsonlite::prettify(helpers.from.list.to.json(dataPackage$descriptor))
Now that you have created your Data Package, you might want to publish your data online so that you can share it with others.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.