Skip to content

Commit

Permalink
fixes #118
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklucius committed Mar 16, 2017
1 parent 288e577 commit 4397579
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Description: Provides easier interaction with
format and manages throttling by 'Socrata'.
Users can upload data to Socrata portals directly
from R.
Version: 1.7.2-11
Date: 2017-03-15
Version: 1.7.2-12
Date: 2017-03-16
Author: Hugh Devlin, Ph. D., Tom Schenk, Jr., and John Malc
Maintainer: "Tom Schenk Jr." <developers@cityofchicago.org>
Depends:
Expand Down
32 changes: 21 additions & 11 deletions R/RSocrata.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,15 @@ getContentAsDataFrame <- function(response) {
#' @noRd
getSodaTypes <- function(response) { UseMethod('response') }
getSodaTypes <- function(response) {
result <- jsonlite::fromJSON(response$headers[['x-soda2-types']])
names(result) <- jsonlite::fromJSON(response$headers[['x-soda2-fields']])
return(result)
dataTypes <- response$headers[['x-soda2-types']]
if (is.null(dataTypes)) {
return(NULL)
}
else {
result <- jsonlite::fromJSON(response$headers[['x-soda2-types']])
names(result) <- jsonlite::fromJSON(response$headers[['x-soda2-fields']])
return(result)
}
}

#' Get a full Socrata data set as an R data frame
Expand Down Expand Up @@ -313,14 +319,18 @@ read.socrata <- function(url, app_token = NULL, email = NULL, password = NULL,
page <- getContentAsDataFrame(response)
result <- rbind.fill(result, page) # accumulate
}
# convert Socrata calendar dates to posix format
for(columnName in colnames(result)[!is.na(dataTypes[fieldName(colnames(result))])
& (dataTypes[fieldName(colnames(result))] == 'calendar_date'
| dataTypes[fieldName(colnames(result))] == 'floating_timestamp')]) {
result[[columnName]] <- posixify(result[[columnName]])
}
for(columnName in colnames(result)[!is.na(dataTypes[fieldName(colnames(result))]) & dataTypes[fieldName(colnames(result))] == 'money']) {
result[[columnName]] <- no_deniro(result[[columnName]])
if (is.null(dataTypes)) {
warning("Dates and currency fields will be converted to character")
} else {
# convert Socrata calendar dates to posix format
for(columnName in colnames(result)[!is.na(dataTypes[fieldName(colnames(result))])
& (dataTypes[fieldName(colnames(result))] == 'calendar_date'
| dataTypes[fieldName(colnames(result))] == 'floating_timestamp')]) {
result[[columnName]] <- posixify(result[[columnName]])
}
for(columnName in colnames(result)[!is.na(dataTypes[fieldName(colnames(result))]) & dataTypes[fieldName(colnames(result))] == 'money']) {
result[[columnName]] <- no_deniro(result[[columnName]])
}
}
# convert logical fields to character
for(columnName in colnames(result)) {
Expand Down
13 changes: 10 additions & 3 deletions tests/testthat/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,16 @@ test_that("read Socrata CSV from New Backend (NBE) endpoint", {
})

test_that("Warn instead of fail if X-SODA2-* headers are missing", {
dfJson <- read.socrata("https://data.healthcare.gov/resource/enx3-h2qp.json")
dfCsv <- read.socrata("https://data.healthcare.gov/resource/enx3-h2qp.csv")
## the above will fail, more tests to come
expect_warning(dfCsv <- read.socrata("https://data.healthcare.gov/resource/enx3-h2qp.csv?$limit=1000"),
info="https://github.com/Chicago/RSocrata/issues/118")
expect_warning(dfJson <- read.socrata("https://data.healthcare.gov/resource/enx3-h2qp.json?$limit=1000"),
info="https://github.com/Chicago/RSocrata/issues/118")
expect_silent(df <- read.socrata("https://odn.data.socrata.com/resource/pvug-y23y.csv"))
expect_silent(df <- read.socrata("https://odn.data.socrata.com/resource/pvug-y23y.json"))
expect_equal("data.frame", class(dfCsv), label="class", info="https://github.com/Chicago/RSocrata/issues/118")
expect_equal("data.frame", class(dfJson), label="class", info="https://github.com/Chicago/RSocrata/issues/118")
expect_equal(150, ncol(dfCsv), label="columns", info="https://github.com/Chicago/RSocrata/issues/118")
expect_equal(140, ncol(dfJson), label="columns", info="https://github.com/Chicago/RSocrata/issues/118")
})

test_that("read Socrata CSV as character", {
Expand Down

0 comments on commit 4397579

Please sign in to comment.