这个网上的例子提到WDI包,给出了从世界银行的api接口获取数据的方法,
DF <- WDI(country = c("CN","RU","BR","ZA","IN"), indicator = "NY.GDP.MKTP.KD.ZG",
start = 1990, end = 2010, extra = FALSE)
但是调用的时候报错,如下信息:
"No such file or directoryfailed to load external entity”
跟进到源代码中看了一下,报错的地方就是dat <- xmlTreeParse(daturl, useInternal = TRUE)这句,daturl是个(有效的)网址。
但是如果像下面这样,先download.file下来,再从本地的xml中读取却没问题。最后没办法,自己改了个下面的function getWDI来拿数据,跟原来方法的区别就是下面两句。
# download from URL
download.file(URL, localFile, mode = "wb")
# read from the downloaded file
dat <- xmlTreeParse(localFile, useInternal = TRUE)
不知道为什么直接从url解析不行,跟本地机器的什么环境配置有关么?大家有没有遇到过这个问题,指教下。[s:13]
- DF <- getWDI(country = c("CN","RU","BR","ZA","IN"), indicator = "NY.GDP.MKTP.KD.ZG",
- start = 1990, end = 2010, extra = FALSE)
- ################################################################################
- # download data from world bank api and then read from the downloaded xml files
- ################################################################################
- getWDI <- function(country = "all", indicator = "NY.GNS.ICTR.GN.ZS", start = 2006,
- end = 2010, extra = FALSE) {
- ## Clean user error
- indicator <- sub("^[ \t]+|[ fire\t]+$", "", indicator)
- country <- sub("^[ \t]+|[ \t]+$", "", country)
- ## Download data in XML format for each country and each indicator
- dat.full <- NULL
- dat.indicator <- NULL
- for (a in indicator) {
- for (b in country) {
- localFile <- paste(b, a, "xml", sep = ".")
- daturl <- paste("http://api.worldbank.org/countries/", b,
- "/indicators/", a,
- "?date=",start,":",end,
- "&per_page=25000",
- "&format=XML",
- sep = "")
- # download from URL
- download.file(daturl , localFile, mode = "wb")
- # read from the downloaded file
- dat <- xmlTreeParse(localFile, useInternal = TRUE)
- iso2c <- unlist(lapply(getNodeSet(dat, "//wb:country"), xmlAttrs))
- dat <- xmlToDataFrame(dat)[,2:4]
- dat$iso2c <- iso2c
- ## Clean to ensure smooth merging
- colnames(dat) <- c("country","year", a, "iso2c")
- dat$year <- as.numeric(as.character(dat$year))
- dat$iso2c <- gsub(" ", "", as.character(dat$iso2c))
- dat$country <- gsub(" ", "", as.character(dat$country))
- dat.indicator <- rbind(dat.indicator, dat)
- daturl <- NULL
- localFile <- NULL
- }
- ## Merge
- if(is.null(dat.full)){
- dat.full <- dat.indicator
- }else{
- dat.full <- merge(dat.full, dat.indicator, all=TRUE)
- }
- dat.indicator <- NULL
- }
- ## Clean data
- dat.full <- dat.full[order(dat.full$iso2c, dat.full$year), c("country","iso2c", "year", indicator)]
- for (i in 3:ncol(dat.full)){dat.full[,i] <- as.numeric(as.character(dat.full[,i]))}
- ## Extras
- if(extra==TRUE){
- dat.full <- merge(dat.full, extra2010, all.x=TRUE)
- }
- return(dat.full)
- }


雷达卡


京公网安备 11010802022788号







