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 8d48973
Showing
15 changed files
with
200 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,2 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
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,3 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData |
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 @@ | ||
Package: estatapi | ||
Type: Package | ||
Title: R package for e-Stat API | ||
Version: 0.1 | ||
Date: 2015-11-01 | ||
Authors@R: person("Hiroaki", "Yutani", email = "yutani.ini@gmail.com", role = c("aut", "cre")) | ||
Description: e-Stat is a service privided by Japanese Government. | ||
License: MIT + file LICENSE | ||
LazyData: TRUE | ||
Imports: | ||
httr, | ||
dplyr, | ||
purrr | ||
RoxygenNote: 5.0.0 |
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,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(estat_api) | ||
export(estat_getMetaInfo) | ||
export(estat_getStatsData) | ||
export(estat_getStatsList) | ||
import(dplyr) | ||
import(httr) |
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,6 @@ | ||
#' R package for e-Stat API | ||
#' | ||
#' @docType package | ||
#' @import httr dplyr | ||
#' @name estatapi | ||
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,8 @@ | ||
#' getMetaInfo | ||
#' | ||
#' @export | ||
estat_getMetaInfo <- function(appId, statsDataId, ...) { | ||
j <- estat_api("rest/2.0/app/json/getMetaInfo", appId = appId, statsDataId = statsDataId, ...) | ||
|
||
get_class_info(j$GET_META_INFO$METADATA_INF$CLASS_INF$CLASS_OBJ) | ||
} |
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 @@ | ||
#' getStatsData | ||
#' | ||
#' @export | ||
estat_getStatsData <- function(appId, statsDataId, ...) | ||
{ | ||
j <- estat_api("rest/2.0/app/json/getStatsData", appId = appId, statsDataId = statsDataId, ...) | ||
|
||
class_info <- get_class_info(j$GET_STATS_DATA$STATISTICAL_DATA$CLASS_INF$CLASS_OBJ) | ||
|
||
value_df <- j$GET_STATS_DATA$STATISTICAL_DATA$DATA_INF$VALUE %>% | ||
bind_rows | ||
|
||
value_df <- value_df %>% | ||
mutate(value = as.numeric(`$`)) | ||
|
||
for (info_name in names(class_info)) { | ||
value_df <- merge_class_info(value_df, class_info, info_name) | ||
} | ||
|
||
value_df | ||
} |
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,10 @@ | ||
#' getStatsList | ||
#' | ||
#' @export | ||
estat_getStatsList <- function(appId, searchWord, ...) { | ||
j <- estat_api("rest/2.0/app/json/getStatsList", appId = appId, searchWord = searchWord, ...) | ||
|
||
j$GET_STATS_LIST$DATALIST_INF$TABLE_INF %>% | ||
purrr::map(as_flattened_character) %>% | ||
bind_rows | ||
} |
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,45 @@ | ||
#' Util | ||
|
||
ESTAT_API_URL <- "http://api.e-stat.go.jp/" | ||
|
||
#' @export | ||
estat_api <- function(path, appId, ...) | ||
{ | ||
res <- GET( | ||
ESTAT_API_URL, path = path, | ||
query = list( | ||
appId = appId, | ||
... | ||
)) | ||
|
||
httr::warn_for_status(res) | ||
|
||
content(res) | ||
} | ||
|
||
as_flattened_character <- function(x) | ||
{ | ||
purrr::map(purrr::flatten(x), as.character) | ||
} | ||
|
||
force_bind_rows <- function(x) | ||
{ | ||
if(is.list(x[[1]])) dplyr::bind_rows(x) else dplyr::as_data_frame(x) | ||
} | ||
|
||
get_class_info <- function(class_obj) | ||
{ | ||
class_info <- lapply(class_obj, function(x) force_bind_rows(x$CLASS)) | ||
names(class_info) <- sapply(class_obj, function(x) x$"@id") | ||
class_info | ||
} | ||
|
||
merge_class_info <- function(value_df, class_info, name) | ||
{ | ||
info <- class_info[[name]] %>% | ||
select(`@code`, `@name`) | ||
|
||
key <- sprintf("@%s", name) | ||
colnames(info) <- c(key, sprintf("%s_info", name)) | ||
left_join(value_df, info, by = key) | ||
} |
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 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: knitr | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source | ||
PackageRoxygenize: rd,collate,namespace |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.