knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of
opendataes is to interact and download data from the https://datos.gob.es API.
This API is an effort from the Spanish government to unify all data sources from different provinces and regions into a single API. The API includes data from entities such as universities, small and big city halls, autonomous communities and Spain as a whole. With over 19,000 datasets in topics all the way from the number of parking spaces in a given city to the levels of air pollution at the regional level (at the moment of writing, October 2018), the API keeps growing with a rich set of public information that researchers and data analysts can use for their own research.
Because aggregating data from all these different sources poses a challenge for reading different formats and harmonizing different datasets,
opendataes is very conservative in what it can read. Once you install the package, you can print the contents of
available_publishers to explore which formats and publishers are available.
This package is meant to be developed by the R community and it is completely open to new pull requests, ideas and collaborations. The idea of the package is to include as many formats and publishers as possible by bringing the support and knowledge of other developers If you're interested in collaborating, please check the vignettes as the package is described in detail there and file a pull request.
opendataes is currently being tested and is not available on CRAN. You can install the development version from Github with:
The package has one main function that allows to read data from the API:
openes_load. However, that function can be used in two different ways.
You can search for a dataset here and click on it's names to go to its homepage. You'll have something like this:
openes_load needs only the end path of a given dataset to read the data. So, we copy this:
and pass it on to
openes_load, and that is it.
library(opendataes) id <- 'l01080193-elecciones-al-parlamento-europeo-resultados-absolutos-por-seccion-censal-de-la-ciudad-de-barcelona' elections <- openes_load(id)
opendataes allows to seach datasets by keywords. For example, to read the same data as before we could search for a keyword and specifying the publisher of that dataset. We know that the data belongs to the 'Ayuntamiento of Barcelona' but
openes_keywords requires that publisher's code.
First we extract the code using the
publishers_available data frame from
pb_code <- publishers_available$publisher_code[publishers_available$publishers == 'Ayuntamiento de Barcelona']
(you can check out the available publishers with
publishers_available, which should increase as the package evolves)
And then we search for a given key keyword
kw <- openes_keywords('elecciones', pb_code) kw
Once we have that,
openes_load only requires that we pass this exact data frame but only with one row. We can do some data munging and subset our data of interest.
final_dt <- kw[grepl("Elecciones al Parlamento Europeo. Resultados absolutos por sección censal de la ciudad de Barcelona", kw$description, fixed = TRUE), ] final_dt
And we pass it to
elections <- openes_load(final_dt)
Once we have the results, we get a print out of the relevant metadata of the dataset.
But more importantly, we can subset the metadata of the dataset as well as the data that was read.
For a deeper explanation of what the columns of the metadata mean and some important caveats of what the package can and cannot do, please read the package's vignettes.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.