在WebCrawler/Web Craper设置中,我想动态扩展我的基本参考类 URL 为了能够为各自的主机/域编写特定方法。只是要明确 动态 我的意思是像自动生成类定义,因为遇到了新域 (例如类 URL_something.com 这将从班级继承 URL)".

待遇,唯一的问题是我的班级 WebPage 期望字段的价值 url 上课 URL. 。它将接受课堂的对象 URL_something.com 因为这是从班级继承的 URL, ,但随后实际上将对象变成了类的实例 URL. 。所以我丢失了实际上是课堂的信息 URL_something.com.

您是否知道如何防止失去关键信息?

代码示例

setRefClass(Class="URL", fields=list(x="character"))
setRefClass(Class="WebPage", fields=list(url="URL"))

obj <- new("WebPage", url=new("URL", x="http://www.something.com/home/index.html"))
obj$url

# Method would recognize that there is no class 'URL_something.com' 
# yet and thus create it:
setRefClass(Class="URL_something.com", contains="URL")

# Another method would take care of mapping field values to 
# an instance of the new class:
> url.obj <- new("URL_something.com", x="http://www.something.com/home/index.html")
> inherits(url.obj, "URL")
[1] TRUE

> obj$url <- url.obj
> class(obj$url)
[1] "URL"
# So I lose the information that it was actually of class "URL_something.com"
有帮助吗?

解决方案

掌握马丁所说的话(请参见上面的评论):R 2.14.0修复了我上面描述的内容。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top