Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
import jsonlite, fixes to oa_combine to add dataset column for each u…
Browse files Browse the repository at this point in the history
…nique

dataset combined
fixed some tests for new changes
  • Loading branch information
sckott committed Aug 10, 2016
1 parent 5f9145d commit 007f226
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ BugReports: https://github.com/sckott/openadds/issues
LazyData: true
Imports:
httr (>= 1.2.1),
jsonlite (>= 1.0),
readr (>= 1.0.0),
dplyr (>= 0.5.0),
tibble (>= 1.1),
Expand Down
5 changes: 5 additions & 0 deletions R/combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ oa_combine <- function(...) {
paths <- vapply(cb, attr, "", which = "path")
readmes <- vapply(cb, attr, "", which = "readme")

dnames <- unlist(lapply(cb, function(z) {
rep(attr(z, "name"), length(z))
}))

# unnest list of data.frames
cbdat <- unlist(cb, recursive = FALSE)

Expand All @@ -61,6 +65,7 @@ oa_combine <- function(...) {
}
out[[i]] <- stats::setNames(tmp, nms)
}
out[[i]]$dataset <- dnames[i]
}
}
structure(dplyr::bind_rows(out),
Expand Down
19 changes: 3 additions & 16 deletions R/oa_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ oa_get.character <- function(x, overwrite = FALSE, ...) {
resp <- oa_GET(x, ...)
structure(resp, class = "oa",
id = x,
path = make_path(basename(x)),
path = make_path(x),
readme = read_me(x),
name = get_name(x))
# structure(resp, class = c("tbl_df", "data.frame", "oa"),
# id = x,
# path = make_path(basename(x)),
# readme = read_me(x))
}

oa_GET <- function(url, ...){
Expand All @@ -81,16 +77,15 @@ oa_GET <- function(url, ...){
ff <- res$request$output$path
}
switch(strextract(basename(ff), "\\zip|csv|geojson"),
csv = read_csv_(ff),
csv = list(read_csv_(ff)),
zip = read_zip_(ff),
geojson = read_geojson_(ff)
geojson = list(read_geojson_(ff))
)
}

get_name <- function(x) gsub("\\..+", "", basename(x))

make_path <- function(x) {
# file.path(oa_cache_path(), x)
xx <- grep("[A-Za-z]", strsplit(x, "/")[[1]], value = TRUE)
xx <- xx[!grepl("http|openaddresses|runs", xx)]
file.path(oa_cache_path(), paste0(xx, collapse = "_"))
Expand All @@ -108,7 +103,6 @@ read_zip_ <- function(fname) {
file_type(exdir),
csv = {
files <- list.files(exdir, pattern = ".csv", full.names = TRUE, recursive = TRUE)
#if (length(files) > 1) stop('More than 1 csv file found', call. = FALSE)
lapply(files, read_csv_)
},
shp = {
Expand All @@ -117,7 +111,6 @@ read_zip_ <- function(fname) {
},
geojson = {
files <- list.files(exdir, pattern = ".geojson", full.names = TRUE, recursive = TRUE)
#if (length(files) > 1) stop('More than 1 csv file found', call. = FALSE)
lapply(files, read_geojson_)
}
)
Expand All @@ -128,12 +121,6 @@ read_geojson_ <- function(x) {
}

read_shp_ <- function(x) {
# shpfile <- list.files(dir, pattern = ".shp", full.names = TRUE, recursive = TRUE)
# if (length(shpfile) != 1) {
# shpfile2 <- grep("\\.shp$", shpfile, value = TRUE)
# shpfile <- shpfile2[1]
# message("Many shp files, using \n", shpfile2[1], "\n others available:\n", shpfile2[-1])
# }
tmp <- maptools::readShapeSpatial(x)
tibble::as_data_frame(tmp@data)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-oa_combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("oa_combine works", {

expect_gt(NROW(aa), NROW(out1))
expect_gt(NROW(aa), NROW(out2))
expect_gt(NROW(out1), NROW(out2))
expect_gt(NROW(out1[[1]]), NROW(out2[[1]]))
})

test_that("oa_combine fails well", {
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-oa_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ test_that("oa_get works", {
# works for zip files
aa <- oa_get(urls[3])

expect_is(aa, "data.frame")
expect_is(aa, "oa")
expect_is(aa[[1]], "data.frame")
expect_equal(attr(aa, "id"), urls[3])

# works for csv files
bb <- suppressWarnings(oa_get(urls[5]))

expect_is(bb, "data.frame")
expect_is(bb, "oa")
expect_is(bb[[1]], "data.frame")
expect_equal(attr(bb, "id"), urls[5])

# works for geojson files
cc <- oa_get(urls[2])

expect_is(cc, "data.frame")
expect_is(cc, "oa")
expect_is(cc[[1]], "data.frame")
expect_equal(attr(cc, "id"), urls[2])
# geojson data exists
expect_true("geometry.type" %in% names(cc))
expect_true("geometry.coordinates" %in% names(cc))
expect_is(cc$geometry.coordinates[[1]], "array")
expect_type(cc$geometry.coordinates[[1]][,,1], "double")
expect_true("geometry.type" %in% names(cc[[1]]))
expect_true("geometry.coordinates" %in% names(cc[[1]]))
expect_is(cc[[1]]$geometry.coordinates[[1]], "array")
expect_type(cc[[1]]$geometry.coordinates[[1]][,,1], "double")
})

test_that("oa_get fails well", {
Expand Down

0 comments on commit 007f226

Please sign in to comment.