This short note shows a way to easily create and automate descriptive commentary for reports using Fingertips data. We will use national data from the Public Health Outcomes Framework.

We need 3 R packages:

The first step is to load the libraries (you’ll need to install the packages if they are not already available).

library(fingertipsR)
library(tidyverse, quietly = TRUE)
library(glue, quietly = TRUE)

Then we will download the indicatorIDs for PHOF - the profileID is 19:

phofids <- indicators(ProfileID = 19)$IndicatorID
phofnames <- indicators(ProfileID = 19)$IndicatorName
phofmeta <- indicator_metadata(ProfileID = 19)

Get data for England

data <- fingertips_data(ProfileID = 19)

data_england <- data %>%
  filter(AreaName == "England") %>%
  left_join(phofmeta)


# title prep
data_england <- data_england %>%
  separate(IndicatorName, remove = FALSE, into = c("index", "indicator"), sep = "-") %>%
  mutate(indicator = tolower(indicator))

Text for an indicator

## pull data frame for relevant indicator
data_0.1i <- data_england %>%
  filter(stringr::str_detect(IndicatorName, "0.1i"), Sex == "Male", Age == "65" ) 


## indicator name
name_0.1i <- data_0.1i %>% pull(unique(indicator)) 

## first value
min_0.1i <- data_0.1i %>%
  filter(TimeperiodSortable == min(TimeperiodSortable)) %>%
  pull(Value)

## first year
year_min_0.1i <- data_0.1i %>%
  filter(TimeperiodSortable == min(TimeperiodSortable) ) %>%
  pull(Timeperiod)


## last value
year_max_0.1i <- data_0.1i %>%
  filter(TimeperiodSortable == max(TimeperiodSortable) ) %>%
  pull(Timeperiod)

## most recent year  
max_0.1i <- data_0.1i %>%
  filter(TimeperiodSortable == max(TimeperiodSortable) ) %>%
  pull(Value)


## gender
gender <- data_0.1i %>% select(Sex) %>% distinct() %>% pull()

## change over time
change <- max_0.1i - min_0.1i


## behaviour depending on value
diff <- ifelse(max_0.1i - min_0.1i >0 , "increased", "decreased")



## units
units <- data_0.1i %>% select(Unit) %>% distinct() %>% pull()


## glue it all together
commentary <- glue("{gender}", " {name_0.1i[1]} ", {diff}, " by {round(change, 2)}"," {tolower(units)} from ",
                   {round(min_0.1i, 2)}, " in 
                   {year_min_0.1i} to {round(max_0.1i, 2)} in {year_max_0.1i}. "
                   )

Male life expectancy at 65 increased by 2.4 years from 16.28 in 2001 - 03 to 18.68 in 2013 - 15.