rOpenGov / helsinki
1
# This file is a part of the helsinki package (http://github.com/rOpenGov/helsinki)
2
# in association with the rOpenGov project (ropengov.github.io)
3

4
# Copyright (C) 2010-2014 Juuso Parkkinen / Louhos <louhos.github.com>. 
5
# All rights reserved.
6

7
# This program is open source software; you can redistribute it and/or modify 
8
# it under the terms of the FreeBSD License (keep this notice): 
9
# http://en.wikipedia.org/wiki/BSD_licenses
10

11
# This program is distributed in the hope that it will be useful, 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14

15

16
#' Helsinki Region Infoshare statistics API
17
#'
18
#' Retrieves data from the Helsinki Region Infoshare (HRI) statistics API:
19
#' http://dev.hel.fi/stats/.
20
#' Currently provides access to the 'aluesarja't data: http://www.aluesarjat.fi/.
21
#' 
22
#' Current implementation is very simple.
23
#' You can either get the list of resources with query="",
24
#' or query for a specific resources and retrieve it in a
25
#' three-dimensional array form.
26
#'
27
#' @param query A string, specifying the dataset to query
28
#' @param verbose logical. Should R report extra information on progress? 
29
#' @return results A three-dimensional array of results.
30
#' 
31
#' @importFrom RCurl getCurlHandle
32
#' @importFrom RCurl getForm
33
#' @importFrom rjson fromJSON
34
#' @export
35
#' 
36
#' @references See citation("helsinki") 
37
#' @author Juuso Parkkinen \email{louhos@@googlegroups.com}
38
#' @examples stats.array <- get_hri_stats("aluesarjat_a03s_hki_vakiluku_aidinkieli")
39

40
get_hri_stats <- function (query="", verbose=TRUE) {
41
  
42
  ## TODO
43
  # implement grepping for resources? as in eurostat
44 0
  if (verbose)
45 0
    message("Accessing Helsinki Region Infoshare statistics API...")
46
  
47
  # Use the regional statistics API
48 0
  api.url <- "http://dev.hel.fi/stats/resources/"
49
  # For resources list
50 0
  if (query=="")
51 0
    query.url <- paste0(api.url, query)
52
  # For a specific resource, use jsontstat
53
  else
54 0
    query.url <- paste0(api.url, query, "/jsonstat")
55
  
56
  # Check whether url available
57 0
  if (!RCurl::url.exists(query.url)) {
58 0
    message(paste("Sorry! Url", query.url, "not available!\nReturned NULL."))
59 0
    return(NULL)
60
  }
61
  # Access data with RCurl
62 0
  curl <- RCurl::getCurlHandle(cookiefile = "")
63 0
  suppressWarnings(
64 0
    res.json <- RCurl::getForm(uri=query.url, curl=curl)
65
  )
66
  # Process json into a list
67 0
  res.list <- rjson::fromJSON(res.json)
68
  
69
  # Process and show list of resources
70 0
  if (query=="") {
71 0
    resources <- names(res.list[["_embedded"]])
72 0
    names(resources) <- sapply(res.list[["_embedded"]], function(x) x$metadata$label)
73 0
    if (verbose)
74 0
      message("Retrieved list of available resources.")
75 0
    return(resources)
76
    
77
  } else {
78
    
79
    ## Process jsonstat results into an array
80
    # For info about jsontstat, see http://json-stat.org/format/
81
    # Possible R package ot use: https://github.com/ajschumacher/rjstat
82
    
83
    # Process dimensions metadata
84 0
    dims <- res.list$dataset$dimension$size
85 0
    names(dims) <- res.list$dataset$dimension$id
86 0
    dimnames <- lapply(res.list$dataset$dimension[3:(length(dims)+2)], function(x) {res=unlist(x$category$label); names(res)=NULL; res})
87
    
88
    # Construct an array
89
    
90
    # For special characters:
91
    # Merkintojen selitykset:
92
    #   .. (kaksi pistetta), tietoa ei ole saatu, se on liian epavarma ilmoitettavaksi tai se on salattu;
93
    # . (piste), loogisesti mahdoton esitettavaksi;
94
    # 0 (nolla), suure pienempi kuin puolet kaytetysta yksikosta.
95
    # assign NA to ".", and ".."
96
    # => simple as.numeric() is fine, produces NA for "." and ".."
97
    
98
    # Have to reverse the dimensions, because in arrays
99
    # "The values in data are taken to be those in the array with the leftmost subscript moving fastest."
100 0
    res.list$dataset$value[res.list$dataset$value %in% c(".", "..")] <- NA
101 0
    res.array <- array(data=as.numeric(res.list$dataset$value), dim=rev(dims), dimnames=rev(dimnames))
102 0
    if (verbose)
103 0
      message("Retrieved resource '",query,"'")
104 0
    return(res.array)
105
  }
106
  #   # Test that it works
107
  #   query <- "aluesarjat_a03s_hki_vakiluku_aidinkieli"
108
  #   hki.vakiluku <- get_hri_stats(query)
109
  #   library(reshape2)
110
  #   df <- reshape2::melt(hki.vakiluku)
111
  
112
}

Read our documentation on viewing source code .

Loading