Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Accessing CKAN in R
This is a short tutorial in using R to run a search for and directly use live open data through the opendata.swiss portal's API interface. Details of the API are documented at handbook.opendata.swiss.
As a first step, we install the ckanr library package, and activate it.
We can initialise the CKAN library with any Web-accessible open data portal simply by supplying the web link to the starting page.
ckanr_setup(url = "https://opendata.swiss")
If there are no connection errors, we are now ready to run a search to get some data packages:
x <- package_search(q = 'name:arbeitslosenquote', rows = 1)
Note that on the Swiss server the titles are multilingual, so we extract just the German (de) title.
To download live open data, now we can select the URL of the first resource in the first package:
tsv_url <- x$results[]$resources[]$download_url
We wish to download the remote (Tab Separated Values) data file, and parse it in one step:
raw_data <- read.csv(tsv_url, header=T, sep="\t")
At this point we are ready to draw a simple plot of the first and second column with visualization code such as:
plot(raw_data[,2], raw_data[,1], type="b")
Get the full example script on GitHub.