Last active
August 7, 2016 15:35
-
-
Save patperu/a06a64bd865de090d5f12b568306281c to your computer and use it in GitHub Desktop.
OpenCoesione Rest API
This file contains 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
library('httr') | |
library('jsonlite') | |
library('dplyr') | |
library('ggplot2') | |
library('scales') | |
options(stringsAsFactors = FALSE) | |
cp <- function(x) Filter(Negate(is.null), x) | |
user_agent <- 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0' | |
#user_agent <- 'ropencoesione - https://github.com/patperu/ropencoesione' | |
diss_base <- function() "http://www.opencoesione.gov.it/api" | |
diss_GET <- function(path = "", args = list(), ...) { | |
x <- httr::GET(file.path(diss_base(), path), query = args, httr::add_headers("User-Agent" = user_agent), ...) | |
httr::stop_for_status(x) | |
jsonlite::fromJSON(httr::content(x, "text", encoding="UTF-8")) | |
} | |
diss <- function(apiroot = "progetti", territorio = NULL, tema = NULL, natura = NULL, ...) { | |
structure( | |
diss_GET(apiroot, args = cp(list(territorio = territorio, tema = tema, natura = natura)), ...), class = "ropencoesione") | |
} | |
res0 <- diss(territorio = "palermo-comune", tema = "occupazione") | |
# http://www.opencoesione.gov.it/api/aggregati | |
res1 <- diss(apiroot = "aggregati") | |
top_10_reg <- res1$aggregati$territori$regioni %>% | |
Map(data.frame, .) %>% | |
bind_rows() %>% | |
mutate(regione = gsub("-regione", "", basename(link))) %>% | |
arrange(desc(totali.progetti)) %>% | |
top_n(10, wt=totali.progetti) | |
top_10_reg | |
ggplot(top_10_reg, aes(reorder(regione, totali.progetti), totali.progetti)) + | |
geom_bar(stat = "identity") + | |
scale_y_continuous(labels = comma) + | |
labs(x = "", y = "Totali progetti") + | |
theme(axis.text.x = element_text(angle=45, hjust=1)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment