forked from florianm/shiny-timeseries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
77 lines (70 loc) · 2.55 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#' Imports, config, global functions
library(shiny)
library(markdown)
library(whisker)
library(Hmisc)
library(ggplot2)
library(qcc)
library(scales)
library(RCurl)
library(rjson)
library(lubridate)
library(tidyr)
library(dplyr)
library(devtools)
library(ckanr)
#------------------------------------------------------------------------------#
# ckanR setup
# Constants
ckanr::ckanr_setup(url = "http://data-demo.dpaw.wa.gov.au/")
# ckanr::ckanr_setup(url = "http://internal-data.dpaw.wa.gov.au/")
#------------------------------------------------------------------------------#
# Data loading
#' Load CSV data from a URL and guess variable classes
#'
#' Reads numbers as numeric
#' Reads strings as factor
#' Converts column with names indicating date format ("Date", "date") to POSIXct
#' Will result in either numeric, POSIXct, or string/factor classes
#'
#' Date is parsed with lubridate::parse_date_time
#'
#' @param url A valid URL to a CSV file
#' @param ldo Lubridate date orders, default: "YmdHMSz", "YmdHMS","Ymd","dmY"
#' @param ltz Lubridate time zone, default: "Australia/Perth"
#' @param dcn Date column names, default: "date", "Date"
get_data <- function(url,
ldo = c("YmdHMSz", "YmdHMS","Ymd","dmY"),
ltz = "Australia/Perth",
dcn = c("date", "Date", "date.start", "date.end")
){
df <- read.table(url, sep = ',', header = T, stringsAsFactors = T)
# df<- cbind(
# lapply(select(df, matches("[Dd]ate")),
# function(x){x<- lubridate::parse_date_time(x, orders = ldo, tz = ltz)}),
# select(df, -matches("[Dd]ate")))
cn <- names(df)
df[cn %in% dcn] <- lapply(
df[cn %in% dcn],
function(x){x<- lubridate::parse_date_time(x, orders = ldo, tz = ltz)}
)
names(df) <- Hmisc::capitalize(names(df))
df
}
#' Filter a list of lists by a key matching a given value
#'
#' @param lol An R list of lists, e.g. a JSON dict
#' @param key The key to filter by
#' @param val The value to match against
list_filter <- function(lol, key, val){
Filter(function(lol){length(lol)>0 && lol[[key]] == val}, lol)
}
#' Make named list (name=url) from CKAN resource dict filtered by file type
#'
#' @param resource_dict A CKAN package$resources JSON dict, loaded as R list
#' @filetype_string The file type as string, e.g. "CSV", "PDF", "TXT"
#' @return A named list of CKAN resource names (as keys) and URLs (as values)
res2nl <- function(resource_dict, filetype_string){
rr <- list_filter(resource_dict, "format", filetype_string)
i <- setNames(lapply(rr, function(x){x$id}), lapply(rr, function(x){x$name}))
}