-
Notifications
You must be signed in to change notification settings - Fork 18
/
enhancements.R
63 lines (60 loc) · 2.92 KB
/
enhancements.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#' Red significance and red trend
#'
#' Filters data returned by the fingertips_data function for values for areas
#' that are trending statistically significantly worse and the spot value is
#' significantly worse than the comparator (England or Parent) value in the
#' latest year of that indicator
#' @return A data frame of data extracted from
#' the Fingertips API
#' @param Comparator String, either "England" or "Parent" to determine which
#' field to compare the spot value significance to
#' @param ... Parameters provided to fingertips_data()
#' @examples
#' \dontrun{
#' # Returns data for the two selected domains at county and unitary authority geography
#' reddata <- fingertips_redred(ProfileID = 26, AreaTypeID = 102)}
#' @family data extract functions
#' @export
fingertips_redred <- function(Comparator = "England", ...) {
path <- "https://fingertips.phe.org.uk/api/"
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))
} 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))
} else {
stop("Comparator must be either England or Parent")
}
return(fingertips_redred)
}
#' High level statistics on Fingertips data
#'
#' A sentence that summarises the number of indicators, unique indicators and
#' profiles
#' @return A string that summarises the high level statistics of indicators and
#' profiles in Fingertips
#' @import dplyr
#' @examples
#' \dontrun{
#' # Returns a sentence describing number of indicators and profiles in Fingertips
#' fingertips_stats()}
#' @export
fingertips_stats <- function() {
cat("This may take a few moments... ")
fingertips_stats <- indicators()
summarised_stats <- fingertips_stats %>%
summarise_if(is.integer, n_distinct)
fingertips_stats <- sprintf("On %s Fingertips consisted of %s profiles, made up of %s indicators and %s distinct indicators.",
format(Sys.Date(), "%d/%m/%Y"),
summarised_stats$ProfileID,
nrow(fingertips_stats),
summarised_stats$IndicatorID)
return(cat(fingertips_stats))
}