-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c1382ef
Showing
62 changed files
with
13,000 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^_pkgdown\.yml$ | ||
^docs$ | ||
^pkgdown$ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#dev | ||
dev/ | ||
|
||
# History files | ||
.Rhistory | ||
.Rapp.history | ||
|
||
# Session Data files | ||
.RData | ||
.RDataTmp | ||
|
||
# User-specific files | ||
.Ruserdata | ||
|
||
# Example code in package build process | ||
*-Ex.R | ||
|
||
# Output files from R CMD build | ||
/*.tar.gz | ||
|
||
# Output files from R CMD check | ||
/*.Rcheck/ | ||
|
||
# RStudio files | ||
.Rproj.user/ | ||
|
||
# produced vignettes | ||
vignettes/*.html | ||
vignettes/*.pdf | ||
|
||
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 | ||
.httr-oauth | ||
|
||
# knitr and R markdown default cache directories | ||
*_cache/ | ||
/cache/ | ||
|
||
# Temporary files created by R markdown | ||
*.utf8.md | ||
*.knit.md | ||
|
||
# R Environment Variables | ||
.Renviron | ||
|
||
# translation temp files | ||
po/*~ | ||
|
||
# RStudio Connect folder | ||
rsconnect/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Package: quantec | ||
Title: R wrapper for quantec API | ||
Version: 0.1.0 | ||
Authors@R: | ||
c(person(given = "Hanjo", | ||
family = "Odendaal", | ||
role = c("aut", "cre"), | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0003-1172-0444")) | ||
) | ||
Description: Data wrapper around the quantec API. | ||
Depends: R (>= 4.0.0) | ||
License: MIT + file LICENSE | ||
Encoding: UTF-8 | ||
LazyData: true | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.2.3 | ||
VignetteBuilder: knitr | ||
Imports: | ||
dplyr, | ||
glue, | ||
httr, | ||
jsonlite, | ||
logger, | ||
lubridate |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 HanjoStudy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export("%>%") | ||
export(quantec_get_data) | ||
import(dplyr) | ||
import(jsonlite) | ||
import(logger) | ||
import(lubridate) | ||
importFrom(glue,glue) | ||
importFrom(httr,RETRY) | ||
importFrom(httr,add_headers) | ||
importFrom(httr,content) | ||
importFrom(httr,http_error) | ||
importFrom(httr,http_type) | ||
importFrom(httr,status_code) | ||
importFrom(magrittr,"%>%") | ||
importFrom(readr,read_csv) | ||
importFrom(scales,comma) | ||
importFrom(snakecase,to_sentence_case) | ||
importFrom(snakecase,to_snake_case) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#' quantec.R | ||
#' @import logger | ||
#' @import dplyr | ||
#' @import lubridate | ||
#' @import jsonlite | ||
#' @importFrom httr RETRY add_headers http_type http_error content status_code | ||
#' @importFrom snakecase to_sentence_case to_snake_case | ||
#' @importFrom glue glue | ||
#' @importFrom readr read_csv | ||
#' @importFrom scales comma | ||
#' | ||
|
||
PKG_VERSION <- utils::packageDescription('quantec')$Version |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' Get Quantec Data | ||
#' | ||
#' @description Get timeseries by specifying comma seperated code(s). | ||
#' @param time_series_code time series code to return, `NMS-EC_BUS,NMS-GA_BUS` | ||
#' @param freq frequency to return `M`, `Q` or `A` | ||
#' @param start_year year to start | ||
#' @param end_year year to end | ||
#' @param respformat to return `csv` or `json` | ||
#' @param log_file log file to output to if in parallel | ||
#' @param is_tidy tidyformat for easy read | ||
#' | ||
#' @return tibble | ||
#' @export | ||
#' | ||
|
||
quantec_get_data <- function(time_series_code, | ||
freq = c("M", "Q", "A")[1], | ||
start_year = "1900-01-01", | ||
end_year = Sys.Date(), | ||
respformat = c("csv", "json")[1], | ||
log_file, | ||
is_tidy = TRUE){ | ||
|
||
|
||
if(!missing(log_file)) | ||
log_appender(appender_file(log_file, max_lines = 1e6)) | ||
|
||
apikey <- Sys.getenv("QUANTEC_API") | ||
|
||
url <- glue("https://www.easydata.co.za/api/v3/download/") | ||
|
||
query_list <- all_param <- list( | ||
timeSeriesCodes = time_series_code, | ||
respFormat = respformat, | ||
freqs = freq, | ||
auth_token = apikey, | ||
startYear = start_year, | ||
endYear = end_year, | ||
isTidy = TRUE | ||
) | ||
|
||
all_param['auth_token'] <- NULL | ||
all_param <- toJSON(all_param) | ||
|
||
log_debug(skip_formatter(glue("[{time_series_code}] -- Querying with parameters: [{all_param}]"))) | ||
|
||
response <- GET(url, query_list = query_list) | ||
|
||
check_response_error(response) | ||
|
||
out <- content(response) %>% read_csv(show_col_types = FALSE) | ||
log_debug(glue("[{time_series_code}] -- Found {comma(nrow(out))} rows")) | ||
|
||
return(out) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#' Pipe operator | ||
#' | ||
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. | ||
#' | ||
#' @name %>% | ||
#' @rdname pipe | ||
#' @keywords internal | ||
#' @export | ||
#' @importFrom magrittr %>% | ||
#' @usage lhs \%>\% rhs | ||
#' @param lhs A value or the magrittr placeholder. | ||
#' @param rhs A function call using the magrittr semantics. | ||
#' @return The result of calling `rhs(lhs)`. | ||
NULL |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' Check API response for errors | ||
#' | ||
#' @param response A response object. | ||
#' | ||
#' @return NULL. Raises an error if response has an error code. | ||
check_response_error <- function(response) { | ||
if (http_error(response)) { | ||
status <- status_code(response) | ||
error <- glue("API request failed [{status}]") | ||
|
||
message <- content(response)$message | ||
if (!is.null(message)) { | ||
error <- glue("{error}: {message}") | ||
} | ||
|
||
stop(error, call. = FALSE) | ||
} | ||
} | ||
#' Wrapper for httr::GET() | ||
#' | ||
#' @param url URL to retrieve. | ||
#' @param config Additional configuration settings. | ||
#' @param ... Further named parameters. | ||
#' @param retry Number of times to retry request on failure. | ||
GET <- function(url = NULL, config = list(), retry = 5, query_list, ...) { | ||
headers = list() | ||
response <- httr::RETRY( | ||
"GET", | ||
url, | ||
config, | ||
..., | ||
query = query_list, | ||
do.call(add_headers, headers), | ||
handle = NULL, | ||
times = retry, | ||
terminate_on = c(400, 404, 429, 500) | ||
) | ||
|
||
# Check for "400 UNAUTHORISED". | ||
# Check for "429 LIMIT EXCEEDED". | ||
if (response$status_code %in% c(400, 429)) { | ||
out <- response %>% | ||
content(as = "text", encoding = "UTF-8") %>% | ||
fromJSON() %>% | ||
toJSON(pretty = TRUE) | ||
|
||
stop(out, call. = FALSE) | ||
} | ||
|
||
response | ||
} |
Oops, something went wrong.