Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update24 #33

Merged
merged 12 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ jobs:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}
- {os: ubuntu-latest, r: 'oldrel-4'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidyqwi
Title: A Convenient API for Accessing United States Census Bureau's Quarterly Workforce Indicator
Version: 0.1.2
Version: 0.1.3
Authors@R: c(
person(given = "Michael",
family = "DeWitt",
Expand All @@ -26,7 +26,7 @@ Description: The purpose of this package is to access the
Quarterly Workforce Indicator is available at
<https://www.census.gov/data/developers/data-sets/qwi.html>.
Depends:
R (>= 3.2),
R (>= 3.5),
future (>= 1.6.2)
Imports:
dplyr,
Expand All @@ -44,10 +44,12 @@ License: MIT + file LICENSE
BugReports: https://github.com/medewitt/tidyqwi/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.3.1
Suggests:
testthat,
covr,
knitr,
rmarkdown
rmarkdown,
spelling
VignetteBuilder: knitr
Language: en-US
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# tidyqwu 0.1.3
* Adds capability for pulling workforce investment areas via @mrembart via PR #31.
* Internal changes due to changes in `tidyr` and `dplyr`
* Misc internal upkeep regarding Census API

# tidyqwi 0.1.2

* Internal changes in preparation for `dplyr` v1.0.0
Expand Down
24 changes: 20 additions & 4 deletions R/check_census_api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,47 @@
#' @param call a returned call from the US Census API
#' @import xml2
#' @import httr
#'@examples \donttest{
#'@examples
#'
#' if(FALSE){
#'library(tidyqwi)
#'library(httr)
#' # A single call to the API without an API Key
#'url <- "api.census.gov/data/timeseries/qwi/sa?get=Emp&for=county:198&key=NOKEY"
#'single_call <- httr::GET(url)
#' stop_for_status(single_call)
#'
#'# Because a non valid API key was specified an message will be returned
#'
#'check_census_api_call(single_call)
#'
#' check_census_api_call(single_call)
#'}
#'
#' @return a string vector with the message from the US Census API
#' @export


check_census_api_call <- function(call){



if(class(call) != "response"){
stop("A valid response was not returned")
}
if( grepl("Invalid", call %>%
httr::content(encoding = "UTF-8") %>%
xml2::as_xml_document()%>%
xml2::xml_find_all("head") %>%
xml2::xml_text())) {

stop("A valid key must be included with each data API request.")
}

returned_call <- httr::content(call, as = "text", encoding = "UTF-8")

if( show_condition(xml2::as_xml_document(returned_call)["node"]) !="error"){



if( class(show_condition(xml2::as_xml_document(returned_call)["node"])) !="error"){
returned_call %>%
xml2::as_xml_document()%>%
xml2::xml_find_all("body") %>%
Expand Down
12 changes: 6 additions & 6 deletions R/get_qwi.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ get_qwi <- function(years,


if(!endpoint %in% c("sa", "se", "rh")){
stop(sprintf("You have not specified a valid endpoint one of `sa``, `se``, or `rh`", endpoint))
stop(sprintf("You have not specified a valid endpoint one of `sa``, `se``, or `rh`"))
}

endpoint_to_retrieve <- switch( endpoint,
Expand Down Expand Up @@ -263,11 +263,11 @@ get_qwi <- function(years,
call <- httr::GET(urls$url[[1]])


if(!substr(call$status_code,1,1) == "2"|
show_condition(check_census_api_call(call))!="error"){
if(!substr(call$status_code, 1, 1) == "2" ||
class(show_condition(check_census_api_call(call)))!="error") {
# IF 200 was not returned then there was an error.

if(grepl(pattern = "valid key must", check_census_api_call(call))){
if(grepl(pattern = "valid key must", check_census_api_call(call))) {
stop(check_census_api_call(call))
}
}
Expand All @@ -276,7 +276,7 @@ get_qwi <- function(years,

#results <- purrr::map(urls$url, httr::GET)
results <- vector("list", length = nrow(urls))
for(i in 1:nrow(urls)){
for(i in 1:nrow(urls)) {
results[[i]] <- httr::GET(urls$url[[i]])
#print(paste0(i, "out of", nrow(urls)))
}
Expand All @@ -292,7 +292,7 @@ get_qwi <- function(years,

a<- purrr::transpose(output)[["result"]]

non_error_returns <- tidyr::spread_(
non_error_returns <- tidyr::spread(
dplyr::bind_rows(
purrr::compact(a)),
"parameter", "value", fill = NA)
Expand Down
2 changes: 1 addition & 1 deletion R/parse_qwi_message.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parse_qwi_message <- function(x) {
if (class(x) != "response") {
stop("You have not passed a valid response")
}
y <- dplyr::as_data_frame(
y <- dplyr::as_tibble(
jsonlite::fromJSON(
httr::content(x, as = "text", encoding = "UTF-8")
)
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
utils::data("owner_codes", package=pkgname, envir=parent.env(environment()))
utils::data("qwi_var_names", package=pkgname, envir=parent.env(environment()))
utils::data("state_info", package=pkgname, envir=parent.env(environment()))
utils::data("geo_codes", package=pkgname, envir=parent.env(environment()))
}

# Helper for trycatching in the code
Expand All @@ -21,3 +22,6 @@ show_condition <- function(code) {



globalVariables({
c("geo_codes", "geo_level", "state_fips")
})
15 changes: 8 additions & 7 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
## Release Summary
This is a minor release. (v0.1.1 to v0.1.2)
This is a minor release. (v0.1.2 to v0.1.3)

* Fixes in preparation for dplyr 1.0.0
* Added to documentation to specify returned objects per guidelines
* Fixes graceful failure to a web API
* Internal updates

## Test environments
* local OS X install, R 4.0
* ubuntu 14.04 (on travis-ci), R 4.0 (devel + release)
* local win-builder (release) R 4.0
* local OS X install, R 4.3
* GitHub Actions
* Windows (release, 3.6)
* MacOS (release)
* Ubuntu (release, devel)

## R CMD check results

0 errors | 0 warnings | 0 notes

25 changes: 25 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AppVeyor
DOI
FIP
FIPs
Indcator
MSA
Orcid
QWI
Quaterly
cbsa
fips
geolevel
granuality
lifecycle
ownercode
qwi
retrive
rh
sa
se
sucessful
tibble
tidyqwi’
tidyqwu
troublshooting
6 changes: 4 additions & 2 deletions man/check_census_api_call.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/geo_codes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/spelling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(requireNamespace('spelling', quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
4 changes: 2 additions & 2 deletions tests/testthat/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("Catch for correct variables", {
})

test_that("Catch for endpoint checking", {
expect_error(get_qwi(years = c(2011), states = c("01"), apikey = "MYKEY", endpoint = "RR"),
expect_error(get_qwi(years = c(2011), states = c("01"), apikey = "MYKEY", endpoint = "RR", variables = "EarnBeg"),
"You have not specified a valid endpoint one of `sa``, `se``, or `rh`")
})

Expand All @@ -44,7 +44,7 @@ test_that("Catch for seasonal adjustment", {
test_that("Valid API Key", {
skip_on_cran()
expect_error(get_qwi(years = c(2011), states = c("01"), apikey = "A"),
"A valid key must be included with each data API request. You included a key with this request, however, it is not valid. Please check your key and try again.If you do not have a key you my sign up for one here. ")
"A valid key must be included with each data API request.")
})

# check that labels fails
Expand Down
Loading