重新定义日志为通用时无法修复警告 [英] Can't fix warning when redefining log as a generic

查看:132
本文介绍了重新定义日志为通用时无法修复警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使 log 函数成为一个泛型,以便可以在 newclass 下改变它的行为。



我有一个包含DESCRIPTION文件的最小包:

 包:logmethod 
类型:包
标题:新的日志方法
版本:0.1
日期:2017-03-23
作者:Me
维护者:向谁抱怨< yourfault@somewhere.net>
描述:更多关于它的功能(也许不止一行)
许可证:MIT
LazyData:TRUE
RoxygenNote:5.0.1
编写R扩展和重新定义非S3基本函数作为一个R包中的S3函数?

 #'@name log_method 
#'@title A新日志方法
#'
#'@description一个新的\代码{log}方法
#'
#'@param xa数字,复数或测量变量
#'@param基于一个正的或复杂的测量变量:计算
#'对数的基数。默认为\ emph {e} = \ code {exp(1)}。
#'@param ...传递给其他方法的其他参数。
#'
#'@export

log < - 函数(x,base,...)
{
UseMethod(log )
}

#'@rdname log_method
#'@export

log.default< - function(x,base,... )
{
base :: log(x,base)
}

#'@rdname log_method
#'@export

log.newclass< - function(x,base,...)
{
print(See,it dispatched)
log.default(as.numeric(x ),as.numeric(base),...)
}

当我运行检查,我不断收到警告



  • 检查S3通用/方法一致性...警告
    警告:声明S3方法'log.default'未找到
    警告:声明S3方法'log.newclass'未找到
    请参见'通用函数和'方法'在'写入R
    扩展'手册中。


当我安装该包,我可以使用 log.d默认 log.newclass ,所以我不知道如何解释这个警告,找不到方法。 将一个R S3普通函数变成一个通用的描述如何制作S4功能,但如果可能的话,我想坚持使用S3。我很想知道为什么我不断收到这个警告。



编辑:



NAMESPACE也可能有用。

 #由roxygen2生成:不要手动编辑

S3method(log,默认)
S3method(log,newclass)
export(log)


解决方案

事实证明,我错过的一块难题是 log 和它的大多数表兄弟已经通用。从?log 文档详细信息


除logb以外的所有函数都是泛型函数:方法可以单独或通过数学集团通用定义它们

因此,新方法的正确编码将会有(注意缺少 UseMethod 调用)

  log .default<  - 函数(x,base,...)
{
base :: log(x,base)
}

log.newclass< ;函数(x,base,...)
{
print(See,it dispatched)
log.default(as.numeric(x),as.numeric(base ),...)
}

另一方面,由于 logb 没有被定义为通用的,这就是Writing R Extensions中的指令会相关的情况(因为派遣还没有设计)

  logb<  - 函数(x,base,...)
{
UseMethod(logb)
}

logb.default< - 函数(x,base,...)
{
base :: logb(x,base)
}

logb.newclass< - function(x ,base,...)
{
print(See,it dispatched)
logb.default(as.numeric(x),as.numeric(base),... )
}

所以如果你遇到类似问题的警告,可能值得检查函数的 base 版本是否已经不是泛型。


I would like to make the log function a generic so that I can alter it's behavior under some newclass.

I have a minimal package with a DESCRIPTION file:

Package: logmethod
Type: Package
Title: A new Log Method
Version: 0.1
Date: 2017-03-23
Author: Me
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: MIT
LazyData: TRUE
RoxygenNote: 5.0.1

And the R Code, based on what I've learned at Writing R Extensions and Is it bad style to redefine non-S3 base functions as S3 functions in an R package?

#' @name log_method
#' @title A New Log Method
#'
#' @description A new \code{log} method
#'
#' @param x a numeric, complex, or measured variable
#' @param base a positive or complex measured variable: the base with respect to which
#'   logarithms are computed. Defaults to \emph{e}=\code{exp(1)}.
#' @param ... Additional arguments to pass to other methods.
#'
#' @export

log <- function(x, base, ...)
{
  UseMethod("log")
}

#' @rdname log_method
#' @export

log.default <- function(x, base, ...)
{
  base::log(x, base)
}

#' @rdname log_method
#' @export

log.newclass <- function(x, base, ...)
{
  print("See, it dispatched")
  log.default(as.numeric(x), as.numeric(base), ...)
}

When I run check, I keep getting the warning

  • checking S3 generic/method consistency ... WARNING Warning: declared S3 method 'log.default' not found Warning: declared S3 method 'log.newclass' not found See section 'Generic functions and methods' in the 'Writing R Extensions' manual.

When I install the package, I'm able to use log.default and log.newclass, so I'm not sure how to interpret this warning that the methods are not found. Turning an R S3 ordinary function into a generic describes how to make an S4 function, but I'd like to stick to S3 if possible. I would love to know why I keep getting this warning.

EDIT:

The NAMESPACE might also be useful.

# Generated by roxygen2: do not edit by hand

S3method(log,default)
S3method(log,newclass)
export(log)

解决方案

It turns out the piece of the puzzle I was missing is that log and most of its cousins are already generic. From the ?log documentation details

All except logb are generic functions: methods can be defined for them individually or via the Math group generic.

Thus, the proper coding for the new methods would have been the following (notice the lack of a UseMethod call)

log.default <- function(x, base, ...)
{
  base::log(x, base)
}

log.newclass <- function(x, base, ...)
{
  print("See, it dispatched")
  log.default(as.numeric(x), as.numeric(base), ...)
}

On the other hand, since logb is not defined as a generic, that is a case where the instructions in Writing R Extensions would have been relevant (because dispatch is not yet designed)

logb <- function(x, base, ...)
{
  UseMethod("logb")
}

logb.default <- function(x, base, ...)
{
  base::logb(x, base)
}

logb.newclass <- function(x, base, ...)
{
  print("See, it dispatched")
  logb.default(as.numeric(x), as.numeric(base), ...)
}

So if you're coming across an warning like in the question, it may be worth checking into whether or not the base version of the function isn't already a generic.

这篇关于重新定义日志为通用时无法修复警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆