我正在尝试熟悉软件包$user = User::find($userId)->load(['jobs'=>function($q){
$q->where('job_title', 'some title');
}]);
。
特别是,我想了解打印摘要方法在roxygen2
中的工作方式。
假设我需要创建一种函数:
roxygen2
然后,我想为我的功能创建一个#' my function
#' @param mat an object of class \code{matrix} with 4 rows and 4 columns
#' @return an object of class \code{matrix} with 4 rows and 4 columns
#' @export
my_function <- function(mat){
res <- list()
res[["output.1"]] <- mat
class(res) <- "mynewpackage"
attributes(res[["output.1"]])$attr <- "output.a"
res[["output.2"]] <- mat
class(res) <- "mynewpackage"
attributes(res[["output.2"]])$attr <- "output.a"
res[["output.3"]] <- mat
class(res) <- "mynewpackage"
attributes(res[["output.3"]])$attr <- "output.b"
return(res)
}
。我这样做:
summary
最后,我希望摘要的输出看起来更像#' summary
#' @title summary
#' @method summary mynewpackage
#' @noRd
#' @export
"summary.mynewpackage" <- function(object, print = "output.1", ...) {
if (print == "output.1") {
res <- list(coefficients = object[["output.1"]],
print = print)
print(res)
} else if (print == "output.2") {
res <- list(coefficients = object[["output.2"]],
print = print)
print(res)
} else if (print == "output.3") {
res <- list(coefficients = object[["output.3"]],
print = print)
print(res)
}
class(res) <- "summary.mynewpackage"
res
}
。
为了这个目的,这就是我迷路的地方,我尝试了以下失败的尝试:
print.summary.lm
我期望的是跑步
#' print
#' @title print
#' @aliases print.mynewpackage
#' @param x numeric number
#' @param digits number of digits
#' @param ... other arguments
#' @noRd
#' @export
"print.mynewpackage" <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
x <- x[[sample(1:3, 1)]]
if( attributes(x[["output.1"]])$attr == "output.a" ) {
print.default(format(x[,1], digits = digits), print.gap = 2L,
quote = FALSE)
cat("\n")
invisible(x)
} else {
print.default(format(x[,1], digits = digits), print.gap = 2L,
quote = FALSE)
cat("\n")
invisible(x)
}
}
#' @rdname print
#' @param x numeric number
#' @param ... other arguments
#' @method print summary
#' @noRd
#' @export
"print.summary" <- function(x, ...){
UseMethod("print.summary")
}
#' @rdname print
#' @title print
#' @name print
#' @aliases print.summary
#' @param object numeric number
#' @param ... additional parameters
#' @param print object to be printed
#' @param digits number of digits to be printed
#' @param signif.stars TRUE or FALSE
#' @method print.summary mynewpackage
#' @importFrom stats printCoefmat
#' @export
"print.summary.mynewpackage" <- function(object, print = "output.1", digits = max(3L, getOption("digits") - 3L), signif.stars = getOption("show.signif.stars"), ...) {
if (print == "output.1") {
printCoefmat(object[["output.1"]], digits = digits, signif.stars = signif.stars)
} else if (print == "output.2") {
printCoefmat(object[["output.2"]], digits = digits, signif.stars = signif.stars)
} else if (print == "output.3") {
printCoefmat(object[["output.3"]], digits = digits, signif.stars = signif.stars)
}
}
并为cmat <- cbind(rnorm(3, 10), sqrt(rchisq(3, 12)))
cmat <- cbind(cmat, cmat[, 1]/cmat[, 2])
cmat <- cbind(cmat, 2*pnorm(-cmat[, 3]))
colnames(cmat) <- c("Estimate", "Std.Err", "Z value", "Pr(>z)")
my_function(cmat)
summary(my_function(cmat))
获取summary(my_function(cmat))
产生的输出
但是,访问print.summary.mynewpackage(my_function(cmat))
的唯一方法是运行:
print.summary.mynewpackage()
关于我该怎么做的任何提示?