【发布时间】:2018-03-09 13:16:21
【问题描述】:
编辑:添加完整代码
我为“情节”制作了一个 S4 方法,它似乎正在工作,除了它向控制台输出了一些杂散的NULL,我不知道它来自哪里。这是顶级代码:
print(plot(x = flux, y = 1, fastplot = TRUE, quietly = TRUE))
还有班级:
flux <- setClass(
# Set the class name
"flux",
slots = c(
raw.data = "list",
source.files = "character",
data = "matrix",
time = "POSIXct",
datatype = "character",
metadata = "data.frame"
)
)
以及方法:
setMethod("plot",
signature(x = "flux"),
function (x, y, ...) {
CheckFluxObject(x)
params <- LoadDefaults(flux = x)
# Interpret 'plot' arguments
par.restore <- par(no.readonly = TRUE)
on.exit(expr = par(par.restore), add = TRUE)
arguments <- list(...)
if (!("fastplot" %in% names(arguments))) {
fastplot <- FALSE
} else {
fastplot <- arguments$fastplot
arguments$fastplot <- NULL
}
if (!("quietly" %in% names(arguments))) {
quietly <- FALSE
} else {
quietly <- arguments$quietly
arguments$quietly <- NULL
}
par(ask=!(fastplot))
if (!("ylab" %in% arguments)) {
ylab <- params["units"]
} else {
ylab <- arguments$ylab
arguments$ylab <- NULL
}
# Pull relevant 'flux' class object data
data <- slot(x, "data")
if (missing("y")) {
y <- 1:ncol(data)
} else {
stopifnot(
is.integer(y),
all(y %in% 1:ncol(data))
)
}
# Bulk function execution
if (quietly == FALSE) {
message("Plotting data traces:")
}
plot.obj <- plot.new()
print("NULL is in the 'for' loop...")
for (i in y){
main <- colnames(data)[i]
plot.obj <- plot(slot(x, "time"), data[, i], main = main,
xlab = "Time", ylab = ylab, unlist(arguments))
print(plot.obj)
}
print("but is it also here??")
# Clean-up and exit
if (quietly == FALSE) {
message("Done plotting.")
}
if (length(y) == 1) {
invisible(plot.obj)
}
print("or here??")
invisible(NULL)
}
)
输出是:
[1] "NULL is in the 'for' loop..."
NULL
[1] "but is it also here??"
[1] "or here??"
NULL
如果我在invisible(NULL) 之后添加另一个print("what about here??"),
然后它会这样做:
[1] "NULL is in the 'for' loop..."
NULL
[1] "but is it also here??"
[1] "or here??"
[1] "what about here??"
[1] "what about here??"
函数返回或打印命令是否有一些我没有预料到的行为? CheckFluxObject 函数只是检查以确保所有插槽都已填满。
【问题讨论】:
-
这个问题很有趣,如果你能让它 100% 可重现会更好。请阅读minimal reproducible example。
-
来自
print(plot.obj)。我刚刚在控制台z = plot(1:10,1:10)中尝试,绘图立即创建,而变量z被分配NULL。所以你正在打印一个变量NULL -
好的,R. Schifini 的评论帮助我杀死了第一个 - 而不是
print(plot.obj)我使用了invisible(plot.obj)并且它仍然显示没有 null 的情节。现在是第二个!我认为我添加了所有必要的代码。
标签: r