Old

CCG_boundaries_or_location <- function(CCG_code = NULL, practice_code = NULL){
  if (is.null(CCG_code) & is.null(practice_code)) {variablesegment <- stringr::str_c("org_type=ccg")}
  if (!is.null(CCG_code) & is.null(practice_code)) {variablesegment <- stringr::str_c("org_type=ccg&q=", CCG_code)}
  if (is.null(CCG_code) & !is.null(practice_code)) {variablesegment <- stringr::str_c("q=", practice_code)}
  if (!is.null(CCG_code) & !is.null(practice_code)) {variablesegment <- stringr::str_c("q=", CCG_code, ",", practice_code)}
  stringr::str_c("https://openprescribing.net/api/1.0/", "org_location/?", variablesegment) %>%
    readLines() %>%
    rjson::fromJSON()
}

New

CCG_boundaries_or_location <- function(CCG_code = NULL, practice_code = NULL, as_sf = TRUE/FALSE) {
  if (is.null(CCG_code) & is.null(practice_code)) {variablesegment <- stringr::str_c("org_type=ccg")}
  if (!is.null(CCG_code) & is.null(practice_code)) {variablesegment <- stringr::str_c("org_type=ccg&q=", CCG_code)}
  if (is.null(CCG_code) & !is.null(practice_code)) {variablesegment <- stringr::str_c("q=", practice_code)}
  if (!is.null(CCG_code) & !is.null(practice_code)) {variablesegment <- stringr::str_c("q=", CCG_code, ",", practice_code)}

  if (as_sf == TRUE) {
    stringr::str_c("https://openprescribing.net/api/1.0/", "org_location/?", variablesegment) %>% 
    sf::st_read() %>% 
      sf::st_as_sf()}

  else {
    stringr::str_c("https://openprescribing.net/api/1.0/", "org_location/?", variablesegment) %>%
    readLines() %>%
    rjson::fromJSON()}  
}
library(magrittr)
CCG_boundaries_or_location(as_sf = TRUE)
CCG_boundaries_or_location()
CCG_boundaries_or_location(CCG_code = "99H")
CCG_boundaries_or_location(CCG_code = "99H", practice_code = "P87003")
CCG_boundaries_or_location(practice_code = "P87003")
CCG_boundaries_or_location(as_sf = TRUE)
CCG_boundaries_or_location(CCG_code = "99H", as_sf = TRUE)
CCG_boundaries_or_location(CCG_code = "99H", practice_code = "P87003", as_sf = TRUE)
CCG_boundaries_or_location(practice_code = "P87003", as_sf = TRUE)


fergustaylor/openprescribingR documentation built on May 31, 2019, 1:15 p.m.