在webcrawler / webscraper设置中,我想动态扩展我的基本引用类URL,以便能够为各个主机/域编写特定方法.为了清楚起见,动态地说我的意思是“在遇到新域时自动生成类定义(例如,将从类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"
接受Martin所说的话(见上面的评论):R 2.14.0修复了我上面所描述的内容.
