Last active
August 9, 2018 10:07
-
-
Save loleg/aef3fd6aa91e2a65c80627bb0f29f49d to your computer and use it in GitHub Desktop.
A snippet of R showing how to access a CKAN portal via its API, in this case opendata.swiss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("ckanr") | |
library('ckanr') | |
# Initialise the CKAN library with a remote portal | |
ckanr_setup(url = "https://opendata.swiss") | |
# 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 | |
x$results[[1]]$title$de | |
# Get the URL of the first resource in the first package | |
tsv_url <- x$results[[1]]$resources[[1]]$download_url | |
# Download the remote (Tab Separated Values) data file | |
# ..and parse it in one step | |
raw_data <- read.csv(tsv_url, header=T, sep="\t") | |
# Draw a simple plot of the first and second column | |
plot(raw_data[,2], raw_data[,1], type="b") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment