Skip to content

Commit 65d98da

Browse files
committed
quandl_datatable now makes a metadata call to get proper types to then pass to readr.
1 parent 9451a39 commit 65d98da

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: tidyquandl
22
Title: A tidy interface to financial data via Quandl.com
3-
Version: 0.1.2.0
3+
Version: 0.1.2.9000
44
Authors@R: person("Clayton", "Yochum", email = "[email protected]", role = c("aut", "cre"))
55
Description: A tidy alternative to the `Quandl` package. Automatically batches and retries requests and ensures tibble outputs.
66
License: MIT + file LICENSE
@@ -28,5 +28,5 @@ ByteCompile: true
2828
Encoding: UTF-8
2929
LazyData: true
3030
Roxygen: list(markdown = TRUE)
31-
RoxygenNote: 6.0.1
31+
RoxygenNote: 6.1.0
3232
VignetteBuilder: knitr

R/tables.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Retrieve data from a Quandl Tables API Endpoint
22
#'
3-
#' This is a replacementfor [Quandl::Quandl.datatable] which allows for multiple
3+
#' This is a replacement for [Quandl::Quandl.datatable] which allows for multiple
44
#' attempts, batches long parameters into multiple requests, always fetches all
55
#' results, and always returns a tibble.
66
#'
@@ -56,6 +56,9 @@ quandl_datatable <- function(code, ..., .batch = 50L) {
5656
stop("`.batch` must be a single integerish value")
5757
}
5858

59+
# get column types from metadata
60+
type_df <- quandl_datatable_meta(code)$columns
61+
5962
# process batches
6063
csv <- batch_parameters(list(...), .batch) %>%
6164
purrr::map(fetch_all_results, path = paste0("datatables/", code)) %>% # make requests
@@ -65,7 +68,7 @@ quandl_datatable <- function(code, ..., .batch = 50L) {
6568
stringr::str_c(collapse = "\n") # collapse to single string
6669

6770
# read all at once
68-
readr::read_csv(csv)
71+
readr::read_csv(csv, col_types = convert_col_spec(type_df))
6972
}
7073

7174

R/utils.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,25 @@ fetch_all_results <- function(params, path) {
126126

127127
responses
128128
}
129+
130+
131+
#' Translate Quandl-provided types to readr-compatible column spec
132+
#' type_tbl should be from e.g. quandl_datatable_meta("WIKI/PRICES")$columns
133+
#'
134+
#' @noRd
135+
#' @keywords internal
136+
convert_col_spec <- function(type_tbl) {
137+
138+
types <- stringr::str_to_lower(type_tbl$type)
139+
140+
funcs <- dplyr::case_when(
141+
stringr::str_detect(types, "^float|^bigdecimal|^double") ~ list(readr::col_double()),
142+
stringr::str_detect(types, "^integer") ~ list(readr::col_integer()),
143+
stringr::str_detect(types, "^datetime") ~ list(readr::col_datetime()),
144+
stringr::str_detect(types, "^date") ~ list(readr::col_date()),
145+
TRUE ~ list(readr::col_character())
146+
) %>%
147+
rlang::set_names(type_tbl$name)
148+
149+
purrr::lift_dl(readr::cols)(funcs)
150+
}

man/quandl_datatable.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-tables.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("quandl_datatable()", {
3636
it("can be batched without affecting results", {
3737
tickers <- c("AAPL", "GOOGL", "MSFT")
3838

39-
expect_identical(
39+
expect_equal(
4040
quandl_datatable("WIKI/PRICES", ticker = tickers, date = "2018-01-02"),
4141
quandl_datatable("WIKI/PRICES", ticker = tickers, date = "2018-01-02", .batch = 2L)
4242
)
@@ -46,7 +46,7 @@ describe("quandl_datatable()", {
4646
tickers <- c("AAPL", "GOOGL", "MSFT")
4747

4848
# > 100 results
49-
expect_identical(
49+
expect_equal(
5050
quandl_datatable("WIKI/PRICES", ticker = tickers, date.gte = "2018-01-01", date.lt = "2018-02-01"),
5151
quandl_datatable("WIKI/PRICES", ticker = tickers, date.gte = "2018-01-01", date.lt = "2018-02-01", qopts.per_page = 20, .batch = 2L)
5252
)

tests/testthat/test-utils.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,20 @@ describe("batch_params()", {
116116
expect_error(tidyquandl:::batch_parameters(params, 10L), "not yet supported")
117117
})
118118
})
119+
120+
describe("convert_col_spec()", {
121+
122+
it("converts types as expected", {
123+
type_df <- data.frame(
124+
name = letters[1:8],
125+
type = c("float", "BigDecimal(34, 12)", "double", "integer", "Datetime", "Date", "String", "foobar")
126+
)
127+
128+
result <- tidyquandl:::convert_col_spec(type_df)
129+
130+
expect_identical(
131+
result,
132+
readr::cols(a = "d", b = "d", c = "d", d = "i", e = "T", f = "D", g = "c", h = "c")
133+
)
134+
})
135+
})

0 commit comments

Comments
 (0)