Skip to content

Commit

Permalink
changes based on updates to API - new field name for parent compariso…
Browse files Browse the repository at this point in the history
…n, new field name for compared to goal, and other minor updates
  • Loading branch information
Sebastian Fox committed Aug 14, 2018
1 parent 7c9ecc8 commit 52a361b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
23 changes: 12 additions & 11 deletions R/enhancements.R
Expand Up @@ -18,21 +18,22 @@

fingertips_redred <- function(Comparator = "England", ...) {
path <- "https://fingertips.phe.org.uk/api/"
fingertips_redred <- fingertips_data(path = path, ...)
filter_field <- names(fingertips_redred)[grepl("^Compared", names(fingertips_redred))]
if (Comparator == "England") {
fingertips_redred <- fingertips_data(path = path, ...) %>%
group_by(IndicatorID, Sex, Age, CategoryType, Category) %>%
filter(TimeperiodSortable == max(TimeperiodSortable) &
grepl("[Ww]orse",RecentTrend) &
grepl("[Ww]orse", ComparedtoEnglandvalueorpercentiles))
filter_field <- filter_field[grepl("^ComparedtoEngland", filter_field)]
} else if (Comparator == "Parent") {
fingertips_redred <- fingertips_data(path = path, ...) %>%
group_by(IndicatorID, Sex, Age, CategoryType, Category) %>%
filter(TimeperiodSortable == max(TimeperiodSortable) &
grepl("[Ww]orse",RecentTrend) &
grepl("[Ww]orse", Comparedtosubnationalparentvalueorpercentiles))
filter_field <- filter_field[!grepl("Comparedtogoal|ComparedtoEngland", filter_field)]
} else if (Comparator == "Goal") {
filter_field <- filter_field[grepl("^Comparedtogoal", filter_field)]
} else {
stop("Comparator must be either England or Parent")
stop("Comparator must be either England, Parent or Goal")
}
fingertips_redred <- fingertips_redred %>%
group_by(IndicatorID, Sex, Age, CategoryType, Category) %>%
filter(TimeperiodSortable == max(TimeperiodSortable) &
grepl("[Ww]orse",RecentTrend) &
grepl("[Ww]orse", !!as.name(filter_field)))
return(fingertips_redred)
}

Expand Down
8 changes: 5 additions & 3 deletions R/retrieve_data.R
Expand Up @@ -93,16 +93,18 @@ new_data_formatting <- function(dataurl) {
#quote = "",
stringsAsFactors = FALSE,
check.names = FALSE)
names(new_data)[names(new_data)=="Target data"] <- "Compared with goal"
names(new_data)[names(new_data)=="Target data"] <- "Compared to goal"
parent_field_name <- names(new_data)[grepl("^Compared", names(new_data))]
parent_field_name <- parent_field_name[!grepl("Compared to goal|Compared to England", parent_field_name)]
character_fields <- c("Indicator Name", "Parent Code",
"Parent Name", "Area Code",
"Area Name", "Area Type",
"Sex", "Age", "Category Type",
"Category", "Time period",
"Value note", "Recent Trend",
"Compared to England value or percentiles",
"Compared to subnational parent value or percentiles",
"New data", "Compared with goal")
parent_field_name,
"New data", "Compared to goal")
numeric_fields <- c("Value", "Lower CI 95.0 limit",
"Upper CI 95.0 limit", "Lower CI 99.8 limit",
"Upper CI 99.8 limit", "Count",
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-enhancements.R
Expand Up @@ -4,14 +4,15 @@ library(fingertipsR)
context("Enhancement functions")

test_that("fingertips_redred should return an error", {
expect_error(fingertips_redred(Comparator = "Sub-national", IndicatorID == 10101),
"Comparator must be either England or Parent")
expect_error(fingertips_redred(Comparator = "Sub-national", IndicatorID = 10101),
"Comparator must be either England, Parent or Goal")
})

numcols <- 26
test_that(paste("fingertips_redred should return a", numcols, "field data.frame"), {
expect_equal(ncol(fingertips_redred(90616, AreaTypeID = 152, Comparator = "England")), numcols)
expect_equal(ncol(fingertips_redred(90616, AreaTypeID = 152, Comparator = "Parent")), numcols)
expect_equal(ncol(fingertips_redred(90776, Comparator = "Goal")), numcols)
})


Expand Down

0 comments on commit 52a361b

Please sign in to comment.