【问题标题】:Import data to R from Atom-compliant data feed从兼容 Atom 的数据源将数据导入 R
【发布时间】:2013-06-07 19:12:29
【问题描述】:

有没有人有从 Atom 兼容的数据馈送将数据导入 R 的经验?我已经下载了一个“.atomsvc​​”文件并在记事本中打开了它的内容并得到以下信息:

<?xml version="1.0" encoding="utf-8" standalone="yes"?><service xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"><workspace><atom:title>OperationallyAvailableCapacity</atom:title><collection href="http://10.101.111.234/ReportServer?%2FInfoPost%2FOperationallyAvailableCapacity&amp;AssetNbr=51&amp;beg_date=05%2F03%2F2013%2000%3A00%3A00&amp;LocationNbr=%25&amp;LocationProp=%25&amp;LocationName=%25&amp;DirOfLow=%25&amp;rs%3AParameterLanguage=&amp;rs%3ACommand=Render&amp;rs%3AFormat=ATOM&amp;rc%3ADataFeed=xAx0x13"><atom:title>table1</atom:title></collection></workspace></service>

我猜想导入这个我可能必须使用 RCurl,但由于我对该包的经验有限,我希望有人能指出我正确的方向。

任何帮助将不胜感激。

【问题讨论】:

    标签: r import rcurl


    【解决方案1】:

    Feed 只为您提供 XML 格式的信息,可以使用 XML 包进行解析。

    library(XML)
    url <- 'http://housesofstones.com/blog/feed/atom/'
    
    # Download and parse the data
    xml_data <- xmlParse(url)
    
    # Convert the xml structure to a list so you can work with it in R
    xml_list <- xmlToList(xml_data)
    
    str(head(xml_list))
    
    List of 6
    $ title   :List of 2
    ..$ text  : chr "Houses of Stones"
    ..$ .attrs: Named chr "text"
    .. ..- attr(*, "names")= chr "type"
    $ subtitle:List of 2
    ..$ text  : chr "\"Science is facts; just as houses are made of stones, so is science made of facts; but a pile of stones is not a house and a c"| __truncated__
    ..$ .attrs: Named chr "text"
    .. ..- attr(*, "names")= chr "type"
    $ updated : chr "2013-05-16T12:16:49Z"
    $ link    : Named chr [1:3] "alternate" "text/html" "http://housesofstones.com/blog"
    ..- attr(*, "names")= chr [1:3] "rel" "type" "href"
    $ id      : chr "http://housesofstones.com/blog/feed/atom/"
    $ link    : Named chr [1:3] "self" "application/atom+xml" "http://housesofstones.com/blog/feed/atom/"
    ..- attr(*, "names")= chr [1:3] "rel" "type" "href"
    

    或者,使用您的示例数据:

    example_data <- '<?xml version="1.0" encoding="utf-8" standalone="yes"?><service xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"><workspace><atom:title>OperationallyAvailableCapacity</atom:title><collection href="http://10.101.111.234/ReportServer?%2FInfoPost%2FOperationallyAvailableCapacity&amp;AssetNbr=51&amp;beg_date=05%2F03%2F2013%2000%3A00%3A00&amp;LocationNbr=%25&amp;LocationProp=%25&amp;LocationName=%25&amp;DirOfLow=%25&amp;rs%3AParameterLanguage=&amp;rs%3ACommand=Render&amp;rs%3AFormat=ATOM&amp;rc%3ADataFeed=xAx0x13"><atom:title>table1</atom:title></collection></workspace></service>'
    
    xml_data <- xmlParse(example_data)
    
    # Convert the xml structure to a list so you can work with it in R
    xml_list <- xmlToList(xml_data)
    
    str(xml_list)
    
    List of 1
    $ workspace:List of 2
    ..$ title     : chr "OperationallyAvailableCapacity"
    ..$ collection:List of 2
    .. ..$ title : chr "table1"
    .. ..$ .attrs: Named chr "http://10.101.111.234/ReportServer?%2FInfoPost%2FOperationallyAvailableCapacity&AssetNbr=51&beg_date=05%2F03%2F2013%2000%3A00%3"| __truncated__
    .. .. ..- attr(*, "names")= chr "href"
    

    编辑

    仔细观察,您的特定示例数据似乎出于某种原因将大量信息保存在单个节点中,并以 URL 编码。如果您想要这些数据,则需要将其提取出来。

    首先,调用该单个节点,并对 URL 进行解码,以便于解析:

    xml_content <- URLdecode(xml_list$workspace$collection$.attrs)
    

    你的各种参数用“&”分隔,所以你可以用那个字符来分割字符串。

    xml_content <- unlist(strsplit(xml_content, "&"))
    

    每个新字符串都包含参数名称和值,用等号分隔。有几种方法可以将这些信息分开。也许最简单的方法是使用 plyr 包中的 str_split_fixed 函数:

    require(stringr)
    
    str_split_fixed(xml_content, "=", 2)
    
    [,1]                                                                          [,2]                 
    [1,] "http://10.101.111.234/ReportServer?/InfoPost/OperationallyAvailableCapacity" ""                   
    [2,] "AssetNbr"                                                                    "51"                 
    [3,] "beg_date"                                                                    "05/03/2013 00:00:00"
    [4,] "LocationNbr"                                                                 "%"                  
    [5,] "LocationProp"                                                                "%"                  
    [6,] "LocationName"                                                                "%"                  
    [7,] "DirOfLow"                                                                    "%"                  
    [8,] "rs:ParameterLanguage"                                                        ""                   
    [9,] "rs:Command"                                                                  "Render"             
    [10,] "rs:Format"                                                                   "ATOM"               
    [11,] "rc:DataFeed"                                                                 "xAx0x13"       
    

    【讨论】:

    • 感谢您的回复。但是提要中的某处应该有数据。我怎么把它弄出来?
    猜你喜欢
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2019-02-19
    • 2019-08-04
    • 2020-12-04
    相关资源
    最近更新 更多