我只是尝试将一个xml文件创建到一个dataframe。我的文件很大,可能是因为这个原因,当我试图这样做的时候,我会收到一个错误。错误是followingL:[<-.data.frame中的错误(*tmp*,i,names(nodes[i]),value =c(图形= "“),:重复的列下标,我使用的代码就是:
breast_cancer <- xmlParse("breastcancerkegg.xml")xmldaraframe <- xmlToDataFrame("breastcancerkegg.xml")如果需要xml文件的示例,请参阅以下genome.jp/pathway/hsa05224+H 00031
<?xml version="1.0"?>
<!DOCTYPE pathway SYSTEM "https://www.kegg.jp/kegg/xml/KGML_v0.7.2_.dtd">
<!-- Creation date: Apr 11, 2018 17:05:28 +0900 (GMT+9) -->
<pathway name="path:hsa05224" org="hsa" number="05224"
title="Breast cancer"
image="https://www.kegg.jp/kegg/pathway/hsa/hsa05224.png"
link="https://www.kegg.jp/kegg-bin/show_pathway?hsa05224">
<entry id="2" name="path:hsa05224" type="map"
link="https://www.kegg.jp/dbget-bin/www_bget?hsa05224">
<graphics name="TITLE:Breast cancer" fgcolor="#000000" bgcolor="#FFFFFF"
type="roundrectangle" x="123" y="58" width="165" height="25"/>
</entry>
<entry id="6" name="hsa:2099 hsa:2100" type="gene"
link="https://www.kegg.jp/dbget-bin/www_bget?hsa:2099+hsa:2100">
<graphics name="ESR1, ER, ESR, ESRA, ESTRR, Era, NR3A1..." fgcolor="#000000" bgcolor="#BFFFBF"
type="rectangle" x="710" y="187" width="46" height="17"/>
</entry>
<entry id="7" name="hsa:2099 hsa:2100" type="gene"
link="https://www.kegg.jp/dbget-bin/www_bget?hsa:2099+hsa:2100">
<graphics name="ESR1, ER, ESR, ESRA, ESTRR, Era, NR3A1..." fgcolor="#000000" bgcolor="#BFFFBF"
type="rectangle" x="1041" y="187" width="46" height="17"/>
</entry>
<entry id="9" name="cpd:C00951" type="compound"
link="https://www.kegg.jp/dbget-bin/www_bget?C00951">
<graphics name="C00951" fgcolor="#000000" bgcolor="#FFFFFF"
type="circle" x="237" y="190" width="8" height="8"/>
</entry>
<entry id="11" name="hsa:1956" type="gene"
link="https://www.kegg.jp/dbget-bin/www_bget?hsa:1956">
<graphics name="EGFR, ERBB, ERBB1, HER1, NISBD2, PIG61, mENA" fgcolor="#000000" bgcolor="#BFFFBF"
type="rectangle" x="325" y="593" width="46" height="17"/>
</entry>有什么想法吗?
发布于 2022-06-30 17:56:09
这是对OP评论的回答:如何访问XML文档中的特定节点。
比起xml2库,我更喜欢XML库。推荐使用xml2小体阅读/参考资料。
理解数据格式
第一步是在XML浏览器或纯文本编辑器中打开文件,查看文档的结构。我们可以看到有一个pathway节点,包含多个entry和relation节点。每个entry节点包含一个graphics节点,每个relation节点包含一个subtype节点。
加载XML文件
library(xml2)
xml <- read_xml('hsa05224.xml')提取所有entry节点
entries <- xml_find_all(xml, "/pathway//entry")在这里,路径中的/引用文档的根,//用于将节点与其子节点分开。
entries现在将是由130个项目(节点)组成的列表。每个节点可以有多个属性和多个子节点。属性有唯一的名称,子属性可以有相同的名称。
从单个节点获取信息
> # print entire node
> entries[[1]]
{xml_node}
<entry id="2" name="path:hsa05224" type="map" link="https://www.kegg.jp/dbget-bin/www_bget?hsa05224">
[1] <graphics name="TITLE:Breast cancer" fgcolor="#000000" bgcolor="#FFFFFF" type="roundrectangle" x="123" y="58" width="165" height="25"/>
> # get node path
> xml_path(entries[[1]])
[1] "/pathway/entry[1]"
> # get vector of all attributes
> xml_attrs(entries[[1]])
id name type
"2" "path:hsa05224" "map"
link
"https://www.kegg.jp/dbget-bin/www_bget?hsa05224"
> # get specific attributes
> xml_attr(entries[[1]], 'name')
[1] "path:hsa05224"
> xml_attr(entries[[1]], 'link')
[1] "https://www.kegg.jp/dbget-bin/www_bget?hsa05224"
> # get node name
> xml_name(entries[[1]])
[1] "entry"
> # get all sub-nodes inside node
> kids <- xml_children(entries[[1]])在最后一步中,我们提取了所有子节点,在每个子节点上我们可以再次执行相同的操作,例如:
> xml_path(kids[[1]])
[1] "/pathway/entry[1]/graphics"
> xml_attrs(kids[[1]])
name fgcolor bgcolor type x y
"TITLE:Breast cancer" "#000000" "#FFFFFF" "roundrectangle" "123" "58"
width height
"165" "25"
> xml_name(kids[[1]])
[1] "graphics"从这些文档中手动提取数据主要涉及大量的for()和/或lapply()-calls。
https://stackoverflow.com/questions/72818589
复制相似问题