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

Issue10 #32

Merged
merged 108 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
afb5d64
add file get_latest_resource.R
ross-hull May 1, 2024
8ac21bd
create initial basic get_latest_resource.R
ross-hull May 2, 2024
7b443cb
create initial basic get_latest_resource.R
ross-hull May 2, 2024
fe42f0f
add applicable_datasets list and throw error if dataset is not in list
ross-hull May 3, 2024
3fab708
change name of file and function to include id, add a test file
ross-hull May 6, 2024
701bb33
remove test file for the moment
ross-hull May 6, 2024
e490d18
remove rows argument from get latest resource id function
ross-hull May 6, 2024
c9923ca
initial explore
ross-hull Apr 23, 2024
02d3fb4
remov file
ross-hull May 6, 2024
78dc3bf
create show_resources
ross-hull May 6, 2024
9ed7932
create simple show datasets function
ross-hull May 6, 2024
043abd8
Style code (GHA)
ross-hull May 6, 2024
6f41b83
remove get_resources.R file
ross-hull May 7, 2024
7ec3e04
delete get_latest_resource.R
ross-hull May 8, 2024
9dc2d5f
change return type of show_resources to be tibble and add created and…
ross-hull May 9, 2024
57c6345
Style code (GHA)
ross-hull May 9, 2024
b94c68c
add roxygen comment to R/show_datasets.R
ross-hull May 13, 2024
6b1a627
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull May 13, 2024
650ab7c
Update documentation
ross-hull May 13, 2024
d76c1c5
add roxygen comment to show_resources.R
ross-hull May 13, 2024
96e5eb7
Update documentation
ross-hull May 13, 2024
a0f153e
add documentation
ross-hull May 13, 2024
0bc0413
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull May 13, 2024
afe2fbc
Update documentation
ross-hull May 13, 2024
84c8193
fix wording of description for show_datasets
ross-hull May 14, 2024
45c510b
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull May 14, 2024
5d9a2ac
Update documentation
ross-hull May 14, 2024
07f0206
Update R/show_datasets.R
ross-hull May 14, 2024
5cf6361
Update R/show_resources.R
ross-hull May 14, 2024
63d7b7c
Update documentation
ross-hull May 14, 2024
e9d7e81
change name of show_datasets to list_datasets
ross-hull May 14, 2024
3ca9a48
Update documentation
ross-hull May 14, 2024
422b61d
change file name
ross-hull May 14, 2024
180631d
Update documentation
ross-hull May 14, 2024
8417553
make changes from show to list for list_resources
ross-hull May 14, 2024
5e7dc5f
Update documentation
ross-hull May 14, 2024
d3eb4d7
create empty get_datasets_additional_info file and function
ross-hull May 16, 2024
926ba59
write get_dataset_additional_info function
ross-hull May 16, 2024
55a8ab3
create option for additional info in list datasets
ross-hull May 16, 2024
e2ca447
add and update roxygen comments
ross-hull May 16, 2024
8b07eac
Update documentation
ross-hull May 16, 2024
6fcb440
remove export R/get_dataset_additional_info.R
ross-hull May 22, 2024
0664388
Update method of max date retrieval R/get_dataset_additional_info.R
ross-hull May 22, 2024
213584c
Update documentation
ross-hull May 22, 2024
971600c
use list instead of paste0 in R/get_dataset_additional_info.R
ross-hull May 22, 2024
7b0f819
Update R/list_datasets.R
ross-hull May 22, 2024
e56369d
Update documentation
ross-hull May 22, 2024
a1f7466
fix typo
ross-hull May 22, 2024
fe3d339
list datasets to return a tibble when include_additional_info is FALSE
ross-hull May 22, 2024
6d90eab
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull May 22, 2024
eef0c76
Update documentation
ross-hull May 22, 2024
9b2acac
typo
ross-hull Jun 12, 2024
0b6eb2f
merge issue10
ross-hull Jun 12, 2024
a226d45
add additional checking to get_latest_resourec_id by also checking if…
ross-hull Jun 12, 2024
2f3c2df
Merge branch 'master' into issue10
ross-hull Jun 14, 2024
d8cb21e
Style code (GHA)
ross-hull Jun 14, 2024
7ae5e04
remove include additional info parameter from list datasets
ross-hull Jun 14, 2024
5e803e7
Merge branch 'master' into issue10
Moohan Jun 19, 2024
bf62035
Add bullet to NEWS
Moohan Jun 19, 2024
2ae53d9
Add Ross (@ross-hull) as an package author
Moohan Jun 19, 2024
db691aa
refine get_dataset_additional_info
ross-hull Jun 20, 2024
c1978c0
convert dates from string to date times
ross-hull Jun 20, 2024
b7a1f13
typo
ross-hull Jun 20, 2024
dede5fb
comment code
ross-hull Jun 20, 2024
2c64bff
comment code
ross-hull Jun 20, 2024
4bb1898
comment
ross-hull Jun 20, 2024
4df6a1c
add example to get_dataset_additional_info
ross-hull Jun 20, 2024
1a92d3d
Merge branch 'get_latest_resourse_2' into issue10
ross-hull Jun 20, 2024
5a15a3b
delete mistake empty file
ross-hull Jun 20, 2024
bc50e49
add documentation
ross-hull Jun 20, 2024
8142c5e
typo in example
ross-hull Jun 20, 2024
4f608a4
try and fix merge conflict
ross-hull Jul 3, 2024
975cf62
Update documentation
ross-hull Jul 3, 2024
afd94aa
add simple test for list_datasets.R
ross-hull Jul 3, 2024
6bafa20
Style code (GHA)
ross-hull Jul 3, 2024
063a84b
add test to test-list_datasets.R
ross-hull Jul 3, 2024
9e5b32a
create test for list_resources.R
ross-hull Jul 3, 2024
bdd2391
update documentation mistake
ross-hull Jul 3, 2024
1653bfa
add 2 basic tests for get_latest_resource_id.R
ross-hull Jul 3, 2024
b275ec7
Style code (GHA)
ross-hull Jul 3, 2024
1f630b5
Fix some typos and format the documentation
Moohan Jul 4, 2024
b5edfac
Update tests/testthat/test-list_datasets.R
Moohan Jul 4, 2024
f5f006b
remove [ ] within roxygen comment
ross-hull Jul 5, 2024
4b5c066
update documentation
ross-hull Jul 5, 2024
1c512b9
update documentation
ross-hull Jul 5, 2024
4bbbbef
update documentation
ross-hull Jul 5, 2024
e2e9cbd
add tests
ross-hull Jul 5, 2024
e3b82fe
Update documentation
ross-hull Jul 5, 2024
9b01c75
update test
ross-hull Jul 11, 2024
f8c333a
Update R/get_dataset_additional_info.R
ross-hull Jul 11, 2024
7d54c30
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull Jul 11, 2024
b890d3b
Update documentation
ross-hull Jul 11, 2024
2c9938e
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull Jul 11, 2024
2c57c02
Update R/list_datasets.R
ross-hull Jul 11, 2024
a842e0e
fix type
ross-hull Jul 11, 2024
312dea4
add error message to expect_error test
ross-hull Jul 11, 2024
68fb34f
Merge branch 'master' into issue10
Moohan Aug 1, 2024
047266b
Only run tests if the platform is online
Moohan Aug 6, 2024
3ec6425
Expand tests for list_resources
Moohan Aug 6, 2024
295b558
Simplify test
Moohan Aug 6, 2024
a5e35f2
Expand list_dataset tests
Moohan Aug 6, 2024
7dd04b1
Style code (GHA)
Moohan Aug 6, 2024
84ec295
shorten variable names R/get_dataset_additional_info.R
ross-hull Aug 7, 2024
56112cf
Merge branch 'master' into issue10
Moohan Aug 7, 2024
fb05ec3
update tests test-get_dataset_additional_info.R
ross-hull Aug 7, 2024
f265e6b
Merge branch 'issue10' of github.com:Public-Health-Scotland/phsopenda…
ross-hull Aug 7, 2024
5ef7b3b
Style code (GHA)
ross-hull Aug 7, 2024
c1be162
Merge branch 'master' into issue10
csillasch Aug 28, 2024
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Version: 0.1.0.9000
Authors@R: c(
person("Csilla", "Scharle", , "[email protected]", role = c("aut", "cre")),
person("James", "McMahon", , "[email protected]", role = "aut"),
person("David", "Aikman", , "[email protected]", role = "aut")
person("David", "Aikman", , "[email protected]", role = "aut"),
person("Ross", "Hull", , "[email protected]", role = "aut")
)
Description: Functions to extract and interact with data from the Scottish
Health and Social Care Open Data platform.
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

export("%>%")
export(get_dataset)
export(get_dataset_additional_info)
ross-hull marked this conversation as resolved.
Show resolved Hide resolved
export(get_latest_resource)
export(get_resource)
export(get_resource_sql)
export(list_datasets)
export(list_resources)
importFrom(magrittr,"%>%")
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ dates to returned data (#24).
- `get_dataset()` will now suggest multiple dataset names, when the dataset
you've asked for doesn't exist (i.e. there's a typo) and there are multiple
likely candidates (#28).
- Two new functions `list_datasets()` and `list_resources()` allow browsing
available datasets and resources (#10).

# phsopendata 0.1.0 (2021-07-22)

Expand Down
43 changes: 43 additions & 0 deletions R/get_dataset_additional_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' get a datasets additional info
#'
#' `get_dataset_additional_info()` returns a tibble of dataset names along with
#' the amount of resources it has and the date it was last updated.Last updated
#' is taken to mean the most recent date a resource within the dataset was
#' created or modified.
#'
#' @inheritParams get_dataset
#'
#' @return a [tibble][tibble::tibble-package] with the data
#' @export
#' @examples
#' get_dataset_additional_info("gp-practice-populations")
get_dataset_additional_info <- function(dataset_name) {
# define query

query <- list("id" = dataset_name)
# fetch the data
content <- phs_GET("package_show", query)

# get the amount of resources
amount_of_resources <- content$result$resources %>%
length()


# get the last recourse created and modified dates
last_resource_created_date <- purrr::map_chr(content$result$resources, ~ .$created)

Check warning on line 27 in R/get_dataset_additional_info.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_dataset_additional_info.R,line=27,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 85 characters.
last_resource_modified_date <- purrr::map_chr(content$result$resources, ~ .$last_modified)

Check warning on line 28 in R/get_dataset_additional_info.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_dataset_additional_info.R,line=28,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 92 characters.

# get the latest between the created and modified dates and change to datetime format

Check warning on line 30 in R/get_dataset_additional_info.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_dataset_additional_info.R,line=30,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 87 characters.
most_recent_resource_date <- max(last_resource_modified_date, last_resource_created_date) %>%

Check warning on line 31 in R/get_dataset_additional_info.R

View workflow job for this annotation

GitHub Actions / lint

file=R/get_dataset_additional_info.R,line=31,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 95 characters.
as.POSIXct(format = "%FT%X", tz = "UTC")


# create tibble to return
return_value <- tibble::tibble(
"name" = dataset_name,
"n_resources" = amount_of_resources,
"last_updated" = most_recent_resource_date
)

return(return_value)
}
18 changes: 18 additions & 0 deletions R/list_datasets.R
Moohan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Lists all available datasets
#'
#' `list_datasets()` shows all of the datasets hosted on the phs open data platform.
#'
#' @return A tibble.
#' @export
#'
#' @examples
#' head(list_datasets())
list_datasets <- function() {
# fetch the data
content <- phs_GET("package_list", "")

data_sets <- tibble::tibble("name" = unlist(content$result))


return(data_sets)
}
43 changes: 43 additions & 0 deletions R/list_resources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Lists all available resources for a dataset
#'
#' `list_resources()` returns all of the resources associated
#' with a dataset
#'
#' @inheritParams get_dataset
#'
#' @return a [tibble][tibble::tibble-package] with the data
#' @export
#'
#' @examples
#' list_resources("weekly-accident-and-emergency-activity-and-waiting-times")
list_resources <- function(dataset_name) {
# throw error if name type/format is invalid
check_dataset_name(dataset_name)

# define query and try API call
query <- list("id" = dataset_name)
content <- try(
phs_GET("package_show", query),
silent = TRUE
)

# if content contains a 'Not Found Error'
# throw error with suggested dataset name
if (grepl("Not Found Error", content[1])) {
suggest_dataset_name(dataset_name)
}

# define list of resource IDs names date created and date modified within dataset
all_ids <- purrr::map_chr(content$result$resources, ~ .x$id)
all_names <- purrr::map_chr(content$result$resources, ~ .x$name)
all_date_created <- purrr::map_chr(content$result$resources, ~ .x$created) %>%
as.POSIXct(format = "%FT%X", tz = "UTC")
all_date_modified <- purrr::map_chr(content$result$resources, ~ .x$last_modified) %>%
as.POSIXct(format = "%FT%X", tz = "UTC")
return_value <- tibble::tibble(
"res_id" = all_ids, "name" = all_names,
"created" = all_date_created, "last_modified" = all_date_modified
)

return(return_value)
}
24 changes: 24 additions & 0 deletions man/get_dataset_additional_info.Rd

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

17 changes: 17 additions & 0 deletions man/list_datasets.Rd

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

22 changes: 22 additions & 0 deletions man/list_resources.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-get_dataset_additional_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
skip_if_offline(host = "www.opendata.nhs.scot")

test_that("returns data in the expected format", {
dataset <- get_dataset_additional_info("weekly-accident-and-emergency-activity-and-waiting-times")

expect_s3_class(dataset, "tbl_df")
expect_equal(nrow(dataset), 1)
expect_named(dataset, c("name", "n_resources", "last_updated"))
})
13 changes: 13 additions & 0 deletions tests/testthat/test-list_datasets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
skip_if_offline(host = "www.opendata.nhs.scot")

test_that("returns more than 0 datasets", {
expect_gte(nrow(list_datasets()), 1)
})

test_that("returns data in the expected format", {
data <- list_datasets()

expect_s3_class(data, "tbl_df")
expect_named(data, "name")
expect_equal(dplyr::n_distinct(data[["name"]]), nrow(data))
})
17 changes: 17 additions & 0 deletions tests/testthat/test-list_resources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
skip_if_offline(host = "www.opendata.nhs.scot")

test_that("returns data in the expected format", {
data <- list_resources("diagnostic-waiting-times")

expect_s3_class(data, "tbl_df")
expect_named(data, c("res_id", "name", "created", "last_modified"))
expect_equal(dplyr::n_distinct(data[["res_id"]]), nrow(data))
expect_equal(dplyr::n_distinct(data[["name"]]), nrow(data))
})

test_that("returns errors properly", {
expect_error(list_resources(), "argument \"dataset_name\" is missing, with no default$")
expect_error(list_resources("bad_name"), "dataset_name must be in dash-case")
expect_error(list_resources("incorrect-name"), "Can't find the dataset name")
expect_error(list_resources("diagnostic-waiting-time"), "diagnostic-waiting-times")
})
Loading