函数从 R 控制台运行,但在内置到包中时失败
Function runs from R console, but fails when built into package
我有一个函数在直接从 R 控制台 运行 时可以完美运行。但是,当我构建并加载一个包含完全相同功能的包时,它会在我调用它时抛出错误。
#' Create divided difference matrix
#' @param x vector to process
#' @param d Dimension
#' @export
#' @return Output is divided difference matrix for use internally in regSmooth
divided.diff <- function (x, d){
m <- length(x)
if (d == 0) {
D <- Matrix::sparseMatrix(1:m, 1:m, x = 1, dims = c(m,m))}
else {
dx <- x[(d + 1):m] - x[1:(m - d)]
dl <- length(dx)
V <- Matrix::sparseMatrix(1:dl, 1:dl, x = 1/dx, dims = c(dl,dl))
D <- d * V %*% diff(divided.diff(x, d - 1))}
return (D)
}
示例:将上述函数定义剪切并粘贴到 R 控制台,然后 运行
divided.diff(1:5,2)
输出(如预期)是
3 x 5 sparse Matrix of class "dgCMatrix"
[1,] 1 -2 1 . .
[2,] . 1 -2 1 .
[3,] . . 1 -2 1
但是如果我使用 RStudio 构建包含此功能的包,然后安装并加载该包。然后运行使用相同的命令
divided.diff(1:5,2)
生成错误
Error in r[i1] : object of type 'S4' is not subsettable
回溯是:
5 diff.default(divided.diff(x, d - 1))
4 diff(divided.diff(x, d - 1)) at regSmooth1.0.R#14
3 divided.diff(x, d - 1)
2 diff(divided.diff(x, d - 1)) at regSmooth1.0.R#14
1 divided.diff(1:5, 2)
我不知道为什么这个函数会根据它是否在包中而表现不同。它似乎与构建创建 S4 class 对象的包有关,但除此之外我迷路了。对此行为的任何解释和如何让它工作的指示都非常感谢。
更新:DESCRIPTION 文件(删除联系方式)如下
Package: regSmooth
Title: Data Smoothing by Regularization
Version: 1.0
Date: 2016-02-14
Author: xxx
Authors@R: person("xx", "xx", email = "zz", role = c("aut", "cre"))
Maintainer: xx <xx@xx.edu>
Description: This package provides functions to perform smoothing by
Tikhonov regularization. Automated optimization of the regularization
parameter can optionally be conducted by cross-validation.
Depends: R (>= 3.1.1)
Imports: Matrix
License: GPL-2
RoxygenNote: 5.0.1
命名空间文件是:
# Generated by roxygen2: do not edit by hand
export(divided.diff)
export(regSmooth)
export(regSmoothAuto)
感谢 Ben Bolker 的评论引导我找到答案。 Matrix 包为其 类 创建了 diff 方法的 S4 版本。即使导入了 Matrix 包,我们也需要明确指示函数使用 diff 的 S4 版本,而不是通用版本。
添加
#' @importMethodsFrom Matrix diff
到函数序言,然后 运行 Roxygen2 在 NAMESPACE 中生成以下行。
importMethodsFrom(Matrix,diff)
这解决了问题。
我有一个函数在直接从 R 控制台 运行 时可以完美运行。但是,当我构建并加载一个包含完全相同功能的包时,它会在我调用它时抛出错误。
#' Create divided difference matrix
#' @param x vector to process
#' @param d Dimension
#' @export
#' @return Output is divided difference matrix for use internally in regSmooth
divided.diff <- function (x, d){
m <- length(x)
if (d == 0) {
D <- Matrix::sparseMatrix(1:m, 1:m, x = 1, dims = c(m,m))}
else {
dx <- x[(d + 1):m] - x[1:(m - d)]
dl <- length(dx)
V <- Matrix::sparseMatrix(1:dl, 1:dl, x = 1/dx, dims = c(dl,dl))
D <- d * V %*% diff(divided.diff(x, d - 1))}
return (D)
}
示例:将上述函数定义剪切并粘贴到 R 控制台,然后 运行
divided.diff(1:5,2)
输出(如预期)是
3 x 5 sparse Matrix of class "dgCMatrix"
[1,] 1 -2 1 . .
[2,] . 1 -2 1 .
[3,] . . 1 -2 1
但是如果我使用 RStudio 构建包含此功能的包,然后安装并加载该包。然后运行使用相同的命令
divided.diff(1:5,2)
生成错误
Error in r[i1] : object of type 'S4' is not subsettable
回溯是:
5 diff.default(divided.diff(x, d - 1))
4 diff(divided.diff(x, d - 1)) at regSmooth1.0.R#14
3 divided.diff(x, d - 1)
2 diff(divided.diff(x, d - 1)) at regSmooth1.0.R#14
1 divided.diff(1:5, 2)
我不知道为什么这个函数会根据它是否在包中而表现不同。它似乎与构建创建 S4 class 对象的包有关,但除此之外我迷路了。对此行为的任何解释和如何让它工作的指示都非常感谢。
更新:DESCRIPTION 文件(删除联系方式)如下
Package: regSmooth
Title: Data Smoothing by Regularization
Version: 1.0
Date: 2016-02-14
Author: xxx
Authors@R: person("xx", "xx", email = "zz", role = c("aut", "cre"))
Maintainer: xx <xx@xx.edu>
Description: This package provides functions to perform smoothing by
Tikhonov regularization. Automated optimization of the regularization
parameter can optionally be conducted by cross-validation.
Depends: R (>= 3.1.1)
Imports: Matrix
License: GPL-2
RoxygenNote: 5.0.1
命名空间文件是:
# Generated by roxygen2: do not edit by hand
export(divided.diff)
export(regSmooth)
export(regSmoothAuto)
感谢 Ben Bolker 的评论引导我找到答案。 Matrix 包为其 类 创建了 diff 方法的 S4 版本。即使导入了 Matrix 包,我们也需要明确指示函数使用 diff 的 S4 版本,而不是通用版本。
添加
#' @importMethodsFrom Matrix diff
到函数序言,然后 运行 Roxygen2 在 NAMESPACE 中生成以下行。
importMethodsFrom(Matrix,diff)
这解决了问题。