将对象从{XML}包中转换回"普通"R字符向量的最佳方法是什么?
例如:
require(XML)
doc <- htmlParse("http://cran.r-project.org/web/packages/XML/index.html")
class(doc)
# [1] "HTMLInternalDocument" "HTMLInternalDocument"
# "XMLInternalDocument" "XMLAbstractDocument"
Run Code Online (Sandbox Code Playgroud)
与此建议类似,我可以这样做:
doc.char <- capture.output(doc)
Run Code Online (Sandbox Code Playgroud)
但这似乎是一条迂回的路线.但是,我没有找到任何其他适当的方法.这已经困扰了我几次.
如果您只想要一个字符向量,那么请使用readLines()而不是htmlParse().但是你可能有更具体的需求然后答案是使用XPath来查询doc; 请参阅该帮助页面上的?getNodeSet(和语法doc["//path"])和示例.
对于你提出的具体问题
library(XML)
doc <- htmlParse("http://cran.r-project.org/web/packages/XML/index.html")
showMethods(class=class(doc), where=search())
Run Code Online (Sandbox Code Playgroud)
到了
as(doc, "character")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3066 次 |
| 最近记录: |