繁体   English   中英

使用 RJSDMX R 从 API 获取 XML 数据

[英]get XML data from API using RJSDMX R

我正在尝试将 XML 数据导入 R。 具体来说,通过世界综合贸易解决方案 (WITS) 获得所有国家之间的双边贸易水平。 在 WITS 网站上,它说连接到他们的 API 的官方 package 是RJSDMX ,但我很难下载数据。

Package use example: https://rstudio-pubs-static.s3.amazonaws.com/593878_b305636e73314eba87615097421034b3.html

WITS API 文档: http://wits.worldbank.org/data/public/WITSAPI_UserGuide.pdf

到目前为止,这是我的代码:

library(RJSDMX)
org <- "WITS"

# Get trade flows using the RJSDMX package
# https://rstudio-pubs-static.s3.amazonaws.com/593878_b305636e73314eba87615097421034b3.html
flow <- getFlows("WITS")
flow$`WBG_WITS,DF_WITS_TradeStats_Trade,1.0`

#parse "WITS" dataflow
fas_dataflow <- strsplit(names(flow[3]),",")
idx <- 1
#if Case 2 found, then get the second element
ifelse(length(fas_dataflow[[1]]) > 1, idx <-2, idx <-1)
fas_dataflow <- fas_dataflow[[1]][idx]

#get list of dimensions of "Average duration of unemployment" DSD
fas_dimensions <- getDimensions(org, fas_dataflow)

#use flatten to remove first level of indices
fas_codelist <- map(.x = names(flatten(fas_dimensions)),
                    flow = fas_dataflow,
                    provider = org,
                    .f = getCodes) %>% set_names(names(flatten(fas_dimensions)))
getCodes(org, fas_dataflow, "FREQ")
getCodes(org, fas_dataflow, "REPORTER")
getCodes(org, fas_dataflow, "PARTNER")
getCodes(org, fas_dataflow, "PRODUCTCODE")
getCodes(org, fas_dataflow, "INDICATOR")
# this doesn't work    
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A.MPRT-PRTNR-SHR")))
# neither does this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A....MPRT-PRTNR-SHR")))
# and neither does this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A....MPRT-PRTNR-SHR.reported")))
# or this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A.MPRT-PRTNR-SHR.reported")))

如果这是一个明显的问题,请原谅我,但我使用 XML API 的经验有限。

先感谢您!!

编辑:我能够直接通过 API 文档页面上的 URL 获得 WITS 双边贸易流。 另请参阅将 XML 解析为数据帧

生成的代码如下所示:

# Set link to website
link1 <-"http://wits.worldbank.org/API/V1/SDMX/V21/datasource/tradestats-trade/reporter/all/year/2018/partner/all/indicator/MPRT-PRTNR-SHR"

# Get data from webpage
data_prices <- getURL(link1)

# Parse XML data
xmlfile <- xmlParse(data_prices)

# Get place nodes
places <- getNodeSet(xmlfile, "//Series")


# Get values for each place
values <- lapply(places, function(x){
  # Get current place id
  pid <- xmlAttrs(x)
  
  # Get values for each gas type for current place
  newrows <- lapply(xmlChildren(x), function(y){
    # Get type and update time values
    attrs <- xmlAttrs(y)
    
    # Get price value
    price <- xmlValue(y)
    names(price) <- "price"
    
    # Return values
    return(c(pid, attrs, price))
  })
  # Combine rows to single list
  newrows <- do.call(rbind, newrows)
  
  # Return rows
  return(newrows)
})

# Combine all values into a single dataframe
df <- as.data.frame(do.call(rbind, values), stringsAsFactors = FALSE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM