我已经写了一个small package for logging,我正在通过r-forge分发它,最近我收到了some very interesting feedback关于如何使其更易于使用的信息,但是该功能基于在2.12中添加到R中的东西(setRefClass)。

我想继续为R-2.11分发软件包,因此我正在寻找一种自动包含或排除S4语法糖的方法,并在将库加载到R> = 2.12系统上时将其包括在内。

我看到的另一个选择是,编写一个需要2.12的小型S4程序包,导入更简单的logging程序包,并导出语法上加糖的接口(interface)...我不太喜欢它,因为我需要选择其他S4软件包的名称。

谢谢Gabor,它提供了一种避免这种需要的方法,但问题仍然悬而未决。

最佳答案

这可以通过原始软件包来完成。这支持旧版本的R(已经存在了很多年),因此您不会遇到旧版本R与新版本R的问题。

library(proto)
library(logging)

Logger. <- proto(
        new = function(this, name)
            this$proto(name = name),
        log = function(this, ...)
            levellog(..., logger = this$name),
        setLevel = function(this, newLevel)
            logging::setLevel(newLevel, container = this$name),
        addHandler = function(this, ...)
            logging::addHandler(this, ..., logger = this$name),
        warn = function(this, ...)
            this$log(loglevels["WARN"], ...),
        error = function(this, ...)
            this$log(loglevels["ERROR"], ...)
)
basicConfig()
l <- Logger.$new(name = "hierarchic.logger.name")
l$warn("this may be bad")
l$error("this definitely is bad")

这给出了输出:
> basicConfig()
> l <- Logger.$new(name = "hierarchic.logger.name")
> l$warn("this may be bad")
2011-02-28 10:17:54 WARNING:hierarchic.logger.name:this may be bad
> l$error("this definitely is bad")
2011-02-28 10:17:54 ERROR:hierarchic.logger.name:this definitely is bad

在上文中,我们仅在日志记录之上分层了原型(prototype),但是由于日志记录对象和原型(prototype)对象都是R环境,因此可以将每个日志记录对象转换为原型(prototype)对象,即两者都可以。那将摆脱多余的层。

有关更多信息,请参见http://r-proto.googlecode.com

关于r - 分发具有可选依赖项的R包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5140447/

10-12 13:57