Skip to content

Commit

Permalink
Merge pull request #33 from Health-SocialCare-Scotland/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
jackhannah95 authored Apr 17, 2020
2 parents fe12c7a + f2d50bb commit 3614e02
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 97 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: phsmethods
Title: Standard Methods for use in Public Health Scotland
Version: 0.1.1
Version: 0.2.0
Authors@R: c(
person("Jack", "Hannah", email = "[email protected]", role = c("aut", "cre")),
person("David", "Caldwell", email = "[email protected]", role = c("aut", "rev")),
person("David", "Caldwell", email = "[email protected]", role = c("aut", "cre")),
person("Lucinda", "Lawrie", email = "[email protected]", role = "rev"),
person("Jack", "Hannah", role = "aut"),
person("Tina", "Fu", email = "[email protected]", role = "aut"),
person("Ciara", "Gribben", email = "[email protected]", role = "aut"),
person("Chris", "Deans", email = "[email protected]", role = "aut"),
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# phsmethods 0.2.0 (2020-04-17)

- New functions added: `age_group()`([#23](https://github.com/Health-SocialCare-Scotland/phsmethods/issues/23), [@chrisdeans](https://github.com/chrisdeans)); `chi_check()`([#30](https://github.com/Health-SocialCare-Scotland/phsmethods/issues/30), [@graemegowans](https://github.com/graemegowans)); `chi_pad()`([#30](https://github.com/Health-SocialCare-Scotland/phsmethods/issues/30), [@graemegowans](https://github.com/graemegowans)); and `match_area()`([#13](https://github.com/Health-SocialCare-Scotland/phsmethods/issues/13), [@jvillacampa](https://github.com/jvillacampa)).

- The first argument of `postcode()` is now `x`, as opposed to `string`. This is unlikely to break much, if any, existing code. `postcode()` is also now slightly faster.

- `phsmethods` no longer imports `stringi`.

- `phsmethods` now depends on a version of R >= 2.10.

- [Jack Hannah](https://github.com/jackhannah95) is no longer a maintainer.

# phsmethods 0.1.1 (2020-02-10)

- `file_size()`, `fin_year()`, `qtr()`, `qtr_end()`, `qtr_next()` and `qtr_prev()` now use `inherits(x, "y")` instead of `class(x) == "y"` to check class. The reasoning is explained in this [blogpost by Martin Maechler](https://developer.r-project.org/Blog/public/2019/11/09/when-you-think-class.-think-again/index.html).
Expand Down
14 changes: 8 additions & 6 deletions R/age_group.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#' Create age groups
#'
#' @description
#' age_group() takes a numeric vector and assigns each age to the appropriate
#' age group.
#' \code{age_group()} takes a numeric vector and assigns each age to the
#' appropriate age group.
#'
#' @param x a vector of numeric values
#' @param from the start of the smallest age group. The default is \code{0}.
#' @param to the end point of the age groups. The default is \code{90}.
#' @param by the size of the age groups. The default is \code{5}.
#' @param as_factor The default behaviour is to return a character vector. Use
#' \code{TRUE} to return a factor vector instead.
#' \code{TRUE} to return a factor vector instead.
#'
#' @return A character vector, where each element is the age group for the
#' corresponding element in \code{x}. If \code{as_factor = TRUE} a factor
#' vector is returned instead.
#' corresponding element in \code{x}. If \code{as_factor = TRUE}, a factor
#' vector is returned instead.
#'
#' @details
#' The \code{from}, \code{to} and \code{by} values are used to create distinct
Expand Down Expand Up @@ -63,8 +63,10 @@ age_group <- function(x,
right = FALSE,
ordered_result = TRUE)

if(as_factor == FALSE)
if (as_factor == FALSE) {
agegroup <- as.character(agegroup)
}

agegroup

}
2 changes: 1 addition & 1 deletion R/chi.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ checksum <- function(x) {
sub_num(x, 9)

j <- floor(i / 11) # Discard remainder
k <- 11 * (j + 1) - i # Check sum calculation
k <- 11 * (j + 1) - i # Checksum calculation
k <- ifelse(k == 11, 0, k) # If 11, make 0

# Check if output matches the checksum
Expand Down
3 changes: 1 addition & 2 deletions R/file_size.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ file_size <- function(filepath = getwd(), pattern = NULL) {
}

y <- x %>%
purrr::map(~file.info(paste0(filepath, "/", .))$size) %>%
unlist() %>%
purrr::map_dbl(~file.info(paste0(filepath, "/", .))$size) %>%

# The gdata package defines a kilobyte (KB) as 1,000 bytes, and a
# kibibyte (KiB) as 1,024 bytes
Expand Down
2 changes: 1 addition & 1 deletion R/fin_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
fin_year <- function(date) {

if (!inherits(date, c("Date", "POSIXct"))) {
stop("The input must have Date or POSIXct class.")
stop("The input must have Date or POSIXct class")
}

# Simply converting all elements of the input vector resulted in poor
Expand Down
6 changes: 3 additions & 3 deletions R/match_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ match_area <- function(x) {
"\U2022 RA2704: Unknown Residency"))
}

# Reading area code to name lookup
# Load area code to name lookup
area_lookup <- phsmethods::area_lookup

# Transforming variable into data frame to allow merging with lookup
# Transform variable into data frame to allow merging with lookup
code_var <- tibble::enframe(code_var,
name = NULL,
value = "geo_code")

# Merging lookup with code variable and retrieving only the name
# Merge lookup with code variable and retrieving only the name
dplyr::left_join(code_var,
area_lookup,
by = "geo_code") %>%
Expand Down
2 changes: 1 addition & 1 deletion R/phsmethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ NULL
# Stops notes from appearing in R CMD check because of undefined global
# variable '.' and allows area_lookup dataset to be used inside match_area
# function
utils::globalVariables(c(".", "area_lookup"))
if(getRversion() >= "2.15.1") utils::globalVariables(c(".", "area_lookup"))
16 changes: 8 additions & 8 deletions R/qtr.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#' value.
#'
#' \itemize{
#' \item \code{qtr} returns the current quarter.
#' \item \code{qtr} returns the current quarter
#'
#' \item \code{qtr_end} returns the last month in the quarter.
#' \item \code{qtr_end} returns the last month in the quarter
#'
#' \item \code{qtr_next} returns the next quarter.
#' \item \code{qtr_next} returns the next quarter
#'
#' \item \code{qtr_prev} returns the previous quarter.
#' \item \code{qtr_prev} returns the previous quarter
#' }
#'
#' @details Quarters are defined as:
Expand Down Expand Up @@ -44,7 +44,7 @@ qtr <- function(date, format = c("long", "short")) {
format <- match.arg(format)

if (!inherits(date, "Date")) {
stop("The input must have Date class.")
stop("The input must have Date class")
}

quarter_num <- lubridate::quarter(date)
Expand Down Expand Up @@ -80,7 +80,7 @@ qtr_end <- function(date, format = c("long", "short")) {
format <- match.arg(format)

if (!inherits(date, "Date")) {
stop("The input must have Date class.")
stop("The input must have Date class")
}

quarter_num <- lubridate::quarter(date)
Expand Down Expand Up @@ -116,7 +116,7 @@ qtr_next <- function(date, format = c("long", "short")) {
format <- match.arg(format)

if (!inherits(date, "Date")) {
stop("The input must have Date class.")
stop("The input must have Date class")
}

quarter_num <- lubridate::quarter(date)
Expand Down Expand Up @@ -152,7 +152,7 @@ qtr_prev <- function(date, format = c("long", "short")) {
format <- match.arg(format)

if (!inherits(date, "Date")) {
stop("The input must have Date class.")
stop("The input must have Date class")
}

quarter_num <- lubridate::quarter(date)
Expand Down
87 changes: 65 additions & 22 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ knitr::opts_chunk$set(

`phsmethods` contains functions for commonly undertaken analytical tasks in [Public Health Scotland (PHS)](https://www.publichealthscotland.scot/):

- `age_group()` categorises ages into groups
- `chi_check()` assesses the validity of a CHI number
- `chi_pad()` adds a leading zero to nine-digit CHI numbers
- `file_size()` returns the names and sizes of files in a directory
- `fin_year()` assigns a date to a financial year in the format `YYYY/YY`
- `match_area()` converts geography codes into area names
- `postcode()` formats improperly recorded postcodes
- `qtr()`, `qtr_end()`, `qtr_next()` and `qtr_prev()` assign a date to a quarter

Expand Down Expand Up @@ -60,76 +64,115 @@ To access the help file for any of `phsmethods`' functions, type `?function_name
?postcode
```

### age_group

```{r age_group}
a <- c(54, 7, 77, 1, 26, 101)
# By default age_group goes in 5 year increments from 0 to 90+
age_group(a)
# But these settings can be changed
age_group(a, from = 0, to = 80, by = 10)
```

### chi_check

```{r chi_check, message = FALSE, warning = FALSE}
# chi_check returns specific feedback on why a CHI number might be invalid
library(dplyr)
b <- tibble(chi = c("0101011237", "3213201234", "123456789", "12345678900", "010120123?"))
b %>% mutate(validity = chi_check(chi))
```

### chi_pad

```{r chi_pad}
# Only nine-digit characters are prefixed with a zero
chi_pad(c("101011237", "101201234", "123223"))
```


### file_size

```{r file_size}
# Names and sizes of all files in the tests/testthat/files folder
file_size(testthat::test_path("files"))
# Names and sizes of Excel files only in the tests/testthat/files folder
file_size(testthat::test_path("files"), pattern = ".xlsx?$")
file_size(testthat::test_path("files"), pattern = "\\.xlsx?$")
```

### fin_year

```{r fin_year}
a <- lubridate::dmy(c(21012017, 04042017, 17112017))
fin_year(a)
c <- lubridate::dmy(c(21012017, 04042017, 17112017))
fin_year(c)
```

### match_area

```{r match_area}
match_area("S13002781")
d <- tibble(code = c("S02000656", "S02001042", "S08000020", "S12000013", "S13002605"))
d %>% mutate(name = match_area(code))
```

### postcode

```{r postcode, message = FALSE, warning = FALSE}
```{r postcode, warning = FALSE}
# The default is pc7 format
postcode("G26QE")
# But pc8 format can also be applied
postcode(c("KA89NB", "PA152TY"), format = "pc8")
# postcode accounts for irregular spacing and lower case letters
library(dplyr)
b <- tibble(pc = c("G 4 2 9 B A", "g207al", "Dg98bS", "DD37J y"))
b %>% mutate(pc = postcode(pc))
e <- tibble(pc = c("G 4 2 9 B A", "g207al", "Dg98bS", "DD37J y"))
e %>% mutate(pc = postcode(pc))
```

### qtr, qtr_end, qtr_next and qtr_prev
```{r qtr}
c <- lubridate::dmy(c(26032012, 04052012, 23092012))
f <- lubridate::dmy(c(26032012, 04052012, 23092012))
# qtr returns the current quarter and year
# The default is long format
qtr(c)
qtr(f)
# But short format can also be applied
qtr(c, format = "short")
qtr(f, format = "short")
# qtr_end returns the last month in the quarter
qtr_end(c)
qtr_end(c, format = "short")
qtr_end(f)
qtr_end(f, format = "short")
# qtr_next returns the next quarter
qtr_next(c)
qtr_next(c, format = "short")
qtr_next(f)
qtr_next(f, format = "short")
# qtr_prev returns the previous quarter
qtr_prev(c)
qtr_prev(c, format = "short")
qtr_prev(f)
qtr_prev(f, format = "short")
```

## Contributing to phsmethods

At present, the maintainers of this package are [Jack Hannah](https://github.com/jackhannah95), [David Caldwell](https://github.com/davidc92) and [Lucinda Lawrie](https://github.com/lucindalawrie).
At present, the maintainers of this package are [David Caldwell](https://github.com/davidc92) and [Lucinda Lawrie](https://github.com/lucindalawrie).

This package is intended to be in continuous development and contributions may be made by anyone within PHS. If you would like to make a contribution, please first create an [issue](https://github.com/Health-SocialCare-Scotland/phsmethods/issues) on GitHub and assign **both** of the package maintainers to it. This is to ensure that no duplication of effort occurs in the case of multiple people having the same idea. The package maintainers will discuss the issue and get back to you as soon as possible.

This package is intended to be in continuous development and contributions may be made by anyone within PHS. If you would like to contribute a function, or propose an improvement to an existing function, please first create an [issue](https://github.com/Health-SocialCare-Scotland/phsmethods/issues) on GitHub and assign **all** of the package maintainers to it. This is to ensure that no duplication of effort occurs in the case of multiple people having the same idea. The package maintainers will discuss the issue and get back to you as soon as possible.
While the most obvious and eyecatching (as well as intimidating) way of contributing is by writing a function, this isn't the only way to make a useful contribution. Fixing typos in documentation, for example, isn't the most glamorous way to contribute, but is of great help to the package maintainers. Please see this [blogpost by Jim Hester](https://www.tidyverse.org/blog/2017/08/contributing/) for more information on getting started with contributing to open source software.

When contributing, please create a [branch](https://github.com/Health-SocialCare-Scotland/phsmethods/branches) in this repository and carry out all work on it. Please ensure you have linked RStudio to your GitHub account using `usethis::edit_git_config()` prior to making your contribution. When you are ready for a review, please create a [pull request](https://github.com/Health-SocialCare-Scotland/phsmethods/pulls) and assign **all** of the package maintainers as reviewers. One or more of them will conduct a review, provide feedback and, if necessary, request changes prior to merging your branch.
When contributing, please create a [branch](https://github.com/Health-SocialCare-Scotland/phsmethods/branches) in this repository and carry out all work on it. Please ensure you have linked RStudio to your GitHub account using `usethis::edit_git_config()` prior to making your contribution. When you are ready for a review, please create a [pull request](https://github.com/Health-SocialCare-Scotland/phsmethods/pulls) and assign **both** of the package maintainers as reviewers. One or both of them will conduct a review, provide feedback and, if necessary, request changes prior to merging your branch.

Please be mindful of information governance when contributing to this package. No data files (aside from publicly available and downloadable datasets or unless explicitly approved), server connection details, passwords or person identifiable or otherwise confidential information should be included anywhere within this package or any other repository (whether public or private) used within PHS. This includes within code and code commentary. For more information on security when using git and GitHub, and on using git and GitHub for version control more generally, please see the [Transforming Publishing Programme](https://www.isdscotland.org/Products-and-Services/Transforming-Publishing-Programme/)'s [Git guide](https://nhs-nss-transforming-publications.github.io/git-guide/) and [GitHub guidance](https://github.com/NHS-NSS-transforming-publications/GitHub-guidance).

Please feel free to add yourself to the 'Authors' section of the `Description` file when contributing. As a rule of thumb, please assign your role as author (`"aut"`) when writing an exported function, and as contributor (`"ctb"`) when editing an existing function and/or writing a non-exported function.
Please feel free to add yourself to the 'Authors' section of the `Description` file when contributing. As a rule of thumb, please assign your role as author (`"aut"`) when writing an exported function, and as contributor (`"ctb"`) for anything else.

`phsmethods` will, as much as possible, adhere to the [tidyverse style guide](https://style.tidyverse.org/) and the [rOpenSci package development guide](https://devguide.ropensci.org/). The most pertinent points to take from these are:

Expand All @@ -141,6 +184,6 @@ Please feel free to add yourself to the 'Authors' section of the `Description` f
- All functions should be tested using [`testthat`](https://github.com/r-lib/testthat)
- The package should always pass `devtools::check()`

It's not necessary to have experience with GitHub or of building an R package to contribute to `phsmethods`; as long as you can write an R function, the package maintainers can assist with error handling, writing documentation, testing and other aspects of package development. It is advised, however, to consult Hadley Wickham's [R Packages](https://r-pkgs.org/) book prior to making a contribution. It may also be useful to consult the [documentation](https://github.com/Health-SocialCare-Scotland/phsmethods/tree/master/R) and [tests](https://github.com/Health-SocialCare-Scotland/phsmethods/tree/master/tests/testthat) of existing functions within this package as a point of reference.
It's not necessary to have experience with GitHub or of building an R package to contribute to `phsmethods`. If you wish to contribute code then, as long as you can write an R function, the package maintainers can assist with error handling, writing documentation, testing and other aspects of package development. It is advised, however, to consult [Hadley Wickham's R Packages book](https://r-pkgs.org/) prior to making a contribution. It may also be useful to consult the [documentation](https://github.com/Health-SocialCare-Scotland/phsmethods/tree/master/R) and [tests](https://github.com/Health-SocialCare-Scotland/phsmethods/tree/master/tests/testthat) of existing functions within this package as a point of reference.

Please note that this README may fail to 'Knit' at times as a result of network security settings. This will likely be due to the badges for continuous integration and test coverage at the top of the document. If you are editing the `README.Rmd` document and are unable to successfully get it to 'Knit', please contact the package maintainers for assistance.
Please note that this README may fail to 'Knit' at times as a result of network security settings. This will likely be due to the badges for the package's release version, continuous integration status and test coverage at the top of the document. If you are editing the `README.Rmd` document and are unable to successfully get it to 'Knit', please contact the package maintainers for assistance.
Loading

0 comments on commit 3614e02

Please sign in to comment.