R/getPostcodeAreas.R

Defines functions getPostcodeArea getPostCodeAreaList getPostCodeAreaUrl

wiki_base_url <- "https://en.wikipedia.org/wiki/"

getPostCodeAreaUrl <- function(key) {
# lookup key from postcode area once lookup table ready
    pcodeareaurl <- paste0(wiki_base_url,key, "postcode_area")
    return(pcodeareaurl)
    }

getPostCodeAreaList <- function(area) {
listofpostcodeareasurl <- paste0(wiki_base_url,"List_of_postcode_districts_in_the_United_Kingdom")
html_listofpostcodeareas <- xml2::read_html(listofpostcodeareasurl)
table_listofpostcodeareas <- rvest::html_nodes(html_listofpostcodeareas,
                                               xpath='//table[@class="toc"]') %>%
    rvest::html_table()
stringofareas <- unlist(table_listofpostcodeareas[[1]])
vectorofareas <- strsplit(x=stringofareas,split=" +",perl=T)
return(vectorofareas)
    }

getPostcodeArea <- function(area="Liverpool") {
wiki_postcode_area_url <- getPostCodeAreaUrl("L_")
html_postcode_area <- xml2::read_html(wiki_postcode_area_url)
table_postcode_area <- rvest::html_nodes(html_postcode_area,
                                         xpath='//table[@class="wikitable sortable"]') %>%
    rvest::html_table()
table_postcode_area <- table_postcode_area[[1]]
print(table_postcode_area)
return(table_postcode_area[,1]) # list of postcodes in area
}
    
mihamn/VEH0122 documentation built on July 11, 2020, 3:45 a.m.