【问题标题】:Killing ghost NULL console outputs?杀死幽灵NULL控制台输出?
【发布时间】: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


【解决方案1】:

我会留在这里,直到出现更好的答案,如果有的话:

显然,绘图对象的print 方法返回NULL,如果您尝试在函数内生成绘图,最好的方法似乎是使用invisible(plot.object)invisible(plot(x, y, ...)),不是print

我仍然不确定第二个 NULL 来自哪里......

编辑:找到第二个!就像方法本身中的print(plot.obj) 一样,顶层代码中的print 正在抛出NULL。删除所有print 命令会杀死所有幽灵。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2015-02-26
    • 2014-04-03
    相关资源
    最近更新 更多