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

RXR-252: Remove download argument to read_who_table() #5

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: clinPK
Type: Package
Title: Clinical Pharmacokinetics Toolkit
Version: 0.10.2
Version: 0.10.3
Date: 2021-05-19
Authors@R: c(person("Ron", "Keizer", email = "ron@insight-rx.com", role
= "aut"), person("Jasmine", "Hughes", email =
Expand Down
2 changes: 1 addition & 1 deletion R/pct_for_age_generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pct_for_age_generic <- function(age = NULL, value = NULL, sex = NULL, variable="
type <- "wfa"
}

dat <- read_who_table(sex=sex, age=age, type=type, download=FALSE)
dat <- read_who_table(sex=sex, age=age, type=type)
tmp <- dat[which.min(abs(age - dat$age)),-(1:4)]
pct <- as.list(tmp)
if(!is.null(value)) {
Expand Down
45 changes: 19 additions & 26 deletions R/read_who_table.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#' Internal function to read WHO growth tables from package or download from WHO
#' Read WHO growth tables
#'
#' Provides a data frame of the WHO growth table for a given age, sex, and type
#' of measurement.
#'
#' This function uses files included in `system.file(package = "clinPK")`.
#' Previously this function also gave the option to download the tables from
#' WHO, but the original URL ("http://www.who.int/entity/childgrowth/standards")
#' no longer exists as of 2021-05-19.
#'
#' @param sex, either `male` or `female`
#' @param age age in years
#' @param type table type, choose from `wfa` (weight for age), `lhfa` (length for age)
#' @param who_url base URL for WHO growth tables
#' @param download download current tables from WHO?
#' @export
#' @md
read_who_table <- function(
sex = NULL,
age = NULL,
type = "wfa",
who_url = "http://www.who.int/entity/childgrowth/standards",
download = FALSE
type = "wfa"
) {
if(is.null(age)) {
stop("Age required!")
Expand All @@ -30,26 +35,14 @@ read_who_table <- function(
}

who_file <- paste0(type, '_', str_sex, '_', postfix,'.txt')
if(!download) {
# use tables supplied with package (also from WHO)
dat <- data.frame(read.table(file=paste0(system.file(package='clinPK'),'/', who_file),
sep = "\t", header = TRUE))
} else {
cat("Downloading data from WHO...")
con <- curl::curl(paste0(who_url, "/", who_file))
open(con)
tmp <- readLines(con)
close(con)
cat("done.")
dat <- c()
cnam <- strsplit(tmp[1], "\t")[[1]]
tmp <- tmp[-1]
for(i in seq(tmp)) {
dat <- rbind(dat, as.num(unlist(strsplit(tmp[i], "\t"))))
}
dat <- data.frame(dat)
colnames(dat) <- cnam
}
# use tables supplied with package (from WHO)
dat <- data.frame(
read.table(
file = paste0(system.file(package = 'clinPK'), '/', who_file),
sep = "\t",
header = TRUE
)
)
dat[,1] <- dat[,1]/unit # convert to years
colnames(dat)[1] <- "age"
if("StDev" %in% names(dat)) {
Expand Down
27 changes: 12 additions & 15 deletions man/read_who_table.Rd

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

39 changes: 39 additions & 0 deletions tests/testthat/test_read_who_table.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test_that("Table is returned", {
dat1 <- read_who_table(sex = "male", age = 10, type = "wfa")
dat2 <- read_who_table(sex = "female", age = 3, type = "lhfa")
expect_true(inherits(dat1, "data.frame"))
expect_true(nrow(dat1) > 0)
expect_true(inherits(dat2, "data.frame"))
expect_true(nrow(dat2) > 0)
expected_names <- c(
"age",
"L",
"M",
"S",
"P01",
"P1",
"P3",
"P5",
"P10",
"P15",
"P25",
"P50",
"P75",
"P85",
"P90",
"P95",
"P97",
"P99",
"P999"
)
expect_equal(names(dat1), expected_names)
expect_equal(names(dat2), expected_names)
})

test_that("read_who_table errors if no age provided", {
expect_error(read_who_table(sex = "male", type = "wfa"))
})

test_that("read_who_table errors if table type not recognized", {
expect_error(read_who_table(sex = "male", age = 10, type = "not a table"))
})