Improved backwards compatibility and made more robust
This commit is contained in:
parent
25a0388983
commit
81d55f348c
|
@ -3,7 +3,6 @@
|
|||
#' @param columns Columns to return (NULL returns all)
|
||||
#' @param countries Which countries to return (list of ISO codes)
|
||||
#' @param list Return a vector with all possible columns?
|
||||
#' @param countrydata The countrydata table
|
||||
#'
|
||||
#' This file provides data on countries, updated regularly. If list = TRUE,
|
||||
#' other parameters will be overridden and it will return a list of columns.
|
||||
|
@ -20,15 +19,20 @@
|
|||
#'
|
||||
#' @export
|
||||
|
||||
country.data <- function(columns = NULL, countries = NULL, list = FALSE,
|
||||
countrydata = read.csv(system.file("extdata", "countrydata.csv", package = "matchcountry"),
|
||||
na.strings = "", stringsAsFactors = FALSE)) {
|
||||
country.data <- function(columns = NULL, countries = NULL, list = FALSE) {
|
||||
countrydata <- tryCatch(get("countrydata", .mc), error = function(e) {
|
||||
read.mc()
|
||||
get("countrydata", .mc)
|
||||
})
|
||||
|
||||
if(list) {
|
||||
return(names(countrydata))
|
||||
} else {
|
||||
if(is.null(countries)) countries <- countrydata$iso
|
||||
if(is.null(columns)) columns <- names(countrydata)
|
||||
|
||||
return(countrydata[countrydata$iso %in% countries, columns])
|
||||
row.names(countrydata) <- countrydata$iso
|
||||
|
||||
return(countrydata[countries, columns])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ match.country <- function(country, output = "iso", language = "english") {
|
|||
replace <- c("&" = "AND", "SAINT" = "ST", "ISDS" = "ISLANDS", "REPUBLIC OF" = "")
|
||||
|
||||
tryCatch({
|
||||
input <- gsub("\ug2|\uc1|\ue0|\uc0|\ue2|\uc2|\ue4|\uc4|\ue3|\uc3|\ue5|\uc5", "a", input)
|
||||
input <- gsub("\ue1|\uc1|\ue0|\uc0|\ue2|\uc2|\ue4|\uc4|\ue3|\uc3|\ue5|\uc5", "a", input)
|
||||
input <- gsub("\ue7|\uc7", "c", input)
|
||||
input <- gsub("\ue9|\uc9|\ue8|\uc8|\uea|\uca|\ueb|\ucb", "e", input)
|
||||
input <- gsub("\ued|\ucd|\uec|\ucc|\uee|\uce|\uef|\ucf", "i", input)
|
||||
|
@ -59,9 +59,16 @@ match.country <- function(country, output = "iso", language = "english") {
|
|||
|
||||
m1 <- match
|
||||
|
||||
if(!is.null(language)) {
|
||||
if(!is.null(language)) if(language == "iso") {
|
||||
m1 <- data.frame(language = "iso", iso = countrydata$iso, name = unlist(countrydata[grep("^iso", names(countrydata))]))
|
||||
m1 <- m1[!is.na(m1$name),]
|
||||
m1 <- rbind(m1, language = "iso", iso = "GBR", name = "UK")
|
||||
} else {
|
||||
m1 <- m1[toupper(match$language) == toupper(language),]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
m1$match <- removepunctuation(m1$name)
|
||||
m1 <- m1[!duplicated(m1$match),]
|
||||
|
@ -71,8 +78,13 @@ match.country <- function(country, output = "iso", language = "english") {
|
|||
isos <- m1[removepunctuation(country),]
|
||||
|
||||
if(output != "iso") {
|
||||
m2 <- countrydata[!is.na(countrydata[[output]]), c("iso", output)]
|
||||
output_a <- output
|
||||
if(output %in% c("imf.official", "imf")) output_a <- "imf.advem"
|
||||
if(output %in% "hdi") output_a <- "hdi"
|
||||
|
||||
m2 <- countrydata[!is.na(countrydata[[output_a]]), c("iso", output_a)]
|
||||
row.names(m2) <- m2$iso
|
||||
names(m2)[2] <- output
|
||||
isos[[output]] <- ifelse(is.na(isos$iso), NA, m2[isos$iso, output])
|
||||
}
|
||||
})
|
||||
|
|
|
@ -18,6 +18,10 @@ read.mc <- function(match = read.csv(system.file("extdata", "match.csv", package
|
|||
tryCatch(.mc, error = function(e) {
|
||||
.mc <<- new.env(parent = emptyenv())
|
||||
})
|
||||
|
||||
names(countrydata)[1] <- "iso"
|
||||
names(match)[1] <- "language"
|
||||
|
||||
assign("match", match, .mc)
|
||||
assign("countrydata", countrydata, .mc)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
iso,iso3digit,iso2digit,isonum,country,spanish,french,parent,parentrel,startyear,endyear,region,continent,wb.income,imf.advem,undp.hdi,regionalbloc,broad.triad,m49.major,m49.minor,m49.dd,g20,g7brics,oecd,eea,cptpp,afcfta,currency,curiso,iso3alt
|
||||
iso,iso3digit,iso2digit,isonum,country,spanish,french,parent,parentrel,startyear,endyear,region,continent,wb.income,imf.advem,undp.hdi,regionalbloc,broad.triad,m49.major,m49.minor,m49.dd,g20,g7brics,oecd,eea,cptpp,afcfta,currency,curiso,iso3alt
|
||||
ABW,ABW,AW,533,Aruba,Aruba,Aruba,Netherlands,Constituent Country,,,"S. & C. America, Caribbean",North America,High-income economies,Emerging and Developing Economies,,,North America,Americas,Caribbean,Developing and Transition,Non G20,,Non-OECD,Not in Europe,Not in Pacific Region,Not in Africa,Aruban Florin,AWG,ABW
|
||||
AFG,AFG,AF,4,Afghanistan,Afganistán,Afghanistan,,,,,South & Central Asia,Asia,Low-income economies,Emerging and Developing Economies,Low human development,SAARC,Asia Pacific,Asia,Southern Asia,Developing and Transition,Non G20,Other Emerging and Developing Economies,Non-OECD,Not in Europe,Not in Pacific Region,Not in Africa,Afghani,AFN,AFG
|
||||
AGO,AGO,AO,24,Angola,Angola,Angola,,,,,Sub-Saharan Africa,Africa,Lower-middle-income economies,Emerging and Developing Economies,Medium human development,SADC,,Africa,Middle Africa,Developing and Transition,Non G20,Other Emerging and Developing Economies,Non-OECD,Not in Europe,Not in Pacific Region,Ratified,Kwanza,AOA,AGO
|
||||
|
|
|
|
@ -4,22 +4,14 @@
|
|||
\alias{country.data}
|
||||
\title{Get Country Data}
|
||||
\usage{
|
||||
country.data(
|
||||
columns = NULL,
|
||||
countries = NULL,
|
||||
list = FALSE,
|
||||
countrydata = read.csv(system.file("extdata", "countrydata.csv", package =
|
||||
"matchcountry"), na.strings = "", stringsAsFactors = FALSE)
|
||||
)
|
||||
country.data(columns = NULL, countries = NULL, list = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{columns}{Columns to return (NULL returns all)}
|
||||
|
||||
\item{countries}{Which countries to return (list of ISO codes)}
|
||||
|
||||
\item{list}{Return a vector with all possible columns?}
|
||||
|
||||
\item{countrydata}{The countrydata table
|
||||
\item{list}{Return a vector with all possible columns?
|
||||
|
||||
This file provides data on countries, updated regularly. If list = TRUE,
|
||||
other parameters will be overridden and it will return a list of columns.
|
||||
|
|
Loading…
Reference in New Issue