Skip to content

Commit 5a971dd

Browse files
committed
Merge pull request #47 from quandl/AP-1780/handle-empty-response
set column names for empty dataframe properly
2 parents 178b3cc + 4df9eb8 commit 5a971dd

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

R/Quandldatatable.R

+18
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,23 @@ Quandl.datatable <- function(code, paginate=FALSE, ...) {
4747
"https://github.com/quandl/quandl-r/blob/master/README.md#datatables"), call. = FALSE)
4848
}
4949

50+
df <- quandl.datatable.set_df_columns(df, columns)
51+
52+
return(df)
53+
}
54+
55+
quandl.datatable.set_df_columns <- function(df, columns) {
56+
ncols <- length(columns[,1])
57+
# if df is empty create an empty df with ncolumns set
58+
# or else we won't be able to set the column names
59+
if (nrow(df) <= 0 && ncols > 0) {
60+
df <- data.frame(matrix(ncol = ncols, nrow = 0))
61+
}
62+
63+
# set column names
5064
names(df) <- columns[,1]
65+
66+
# set column types
5167
df <- quandl.datatable.convert_df_columns(df, columns[,2])
5268

5369
return(df)
@@ -63,6 +79,8 @@ quandl.datatable.convert_df_columns <- function(df, column_types) {
6379
df[,i] <- as.numeric(df[,i])
6480
} else if (grepl("^date", column_types[i])) {
6581
df[,i] <- as.Date(df[,i])
82+
} else {
83+
df[,i] <- as.character(df[,i])
6684
}
6785
}
6886
return(df)

tests/testthat/test-datatable-helper.r

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ mock_datatable_data <- function(cursor_id = 'null') {
3636
mock_data <- gsub("\"#cursor_id\"", cursor_id, mock_data)
3737
return(mock_data)
3838
}
39+
40+
mock_empty_datatable_data <- function() {
41+
mock_data <- "{\"datatable\":
42+
{\"data\": [],
43+
\"columns\":[{\"name\":\"ticker\",\"type\":\"String\"},
44+
{\"name\":\"oper_income\",\"type\":\"BigDecimal(12,4)\"},
45+
{\"name\":\"comm_share_holder\",\"type\":\"Integer\"},
46+
{\"name\":\"per_end_date\",\"type\":\"Date\"}]},
47+
\"meta\":{\"next_cursor_id\": null
48+
}
49+
}"
50+
return(mock_data)
51+
}

tests/testthat/test-datatable.r

+19-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ with_mock(
6868
data <- Quandl.datatable('ZACKS/FC')
6969
expect_equal(names(data), c("ticker", "oper_income", "comm_share_holder", "per_end_date"))
7070
}),
71-
test_that("response data columns are convereted to proper data types", {
71+
test_that("response data columns are converted to proper data types", {
7272
data <- Quandl.datatable('ZACKS/FC')
7373
expect_is(data[,1], "character")
7474
expect_is(data[,2], "numeric")
@@ -91,4 +91,22 @@ with_mock(
9191
})
9292
)
9393

94+
context("Quandl.datatable() empty data response")
95+
with_mock(
96+
`httr::VERB` = function(http, url, config, body, query) {
97+
mock_response(content = mock_empty_datatable_data())
98+
},
99+
`httr::content` = function(response, as="text") {
100+
response$content
101+
},
102+
test_that("empty response data columns are converted to proper data types", {
103+
data <- Quandl.datatable('ZACKS/FC')
104+
expect_equal(nrow(data), 0)
105+
expect_is(data[,1], "character")
106+
expect_is(data[,2], "numeric")
107+
expect_is(data[,3], "numeric")
108+
expect_is(data[,4], "Date")
109+
})
110+
)
111+
94112
reset_config()

0 commit comments

Comments
 (0)