4

在 webcrawler/webscraper 设置中,我想动态扩展我的基本参考类URL,以便能够为各个主机/域编写特定的方法。为了清楚起见,动态我的意思是“在遇到新域时自动生成类定义(例如URL_something.com,将从 class 继承的类URL)”。

一种享受,唯一的问题是我的班级WebPage期望 field 的值url是 class URL。它将接受 class 的对象,URL_something.com因为 this 从 class 继承URL,但实际上将对象转换为 class 的实例URL。所以我失去了它实际上是 class 的信息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"
4

1 回答 1

1

了解 Martin 所说的话(参见上面的评论):R 2.14.0 修复了我上面描述的内容。

于 2011-11-15T23:24:42.687 回答