【问题标题】:R : How to suppress a persistent warning message from alpha() function in my Rmd output?R:如何在我的 Rmd 输出中抑制来自 alpha() 函数的持续警告消息?
【发布时间】:2015-11-05 09:41:48
【问题描述】:

程序:Mac OSX 的 R 3.2.1
IDE:RStudio
输出:rmkd > html
包装:“心理”
功能:alpha()


问题:
在使用alpha(data, na.rm=F, check.keys=F, delete=F) 时,由于部分输入数据是负相关的,并且因为我有 check.keys = FALSE,所以我收到以下消息:

部分项目XXX与总量表呈负相关, 可能应该颠倒过来。为此,请再次运行该函数 'check.keys=TRUE' 选项

问题:
我的 check.keys 是故意设置的。完全理解警告的含义,主要是出于审美和教育原因,我如何在输出中抑制它?

目前的尝试:
1. 我试过suppressWarnings() & suppressMessages()
2. 我试过invisible() & sink(., type="message")
3. 在 Rmd 块中,我试过了:```{r warning=F, message=F}
4.探索print(alpha)我发现了我认为的起源。也许有人知道如何抑制这部分代码? :

`p1 <- principal(x)
if (any(p1$loadings < 0)) {
     if (check.keys) {
         warning("Some items were negatively correlated with total scale and were automatically reversed.\n This is indicated by a negative sign for the variable name.")
         keys <- 1 - 2 * (p1$loadings < 0)
         }
     else {
         warning("Some items were negatively correlated with the total scale and probably should be reversed.  To do this, run the function again with the 'check.keys=TRUE' option")
         cat("Some items (", rownames(p1$loadings)[(p1$loadings < 0)], ") were negatively correlated with the total scale and probably should be reversed.  To do this, run the function again with the 'check.keys=TRUE' option")
         }
}`

谢谢!

【问题讨论】:

  • 这应该通知代码的开发者,他们的函数会做坏事(不应该有cat)。
  • 康拉德是正确的。让我知道这个问题很有帮助。它现在已在 psych 1.5.8 中修复(希望本周末来到 CRAN)。
  • 我做了一个小猴子补丁,直到希望得到修复,请参阅gist.github.com/gurix/10c675632798da04d9d2

标签: r warnings alpha suppress psych


【解决方案1】:

这里的罪魁祸首是cat,它不会理会suppressMessages等。

要捕获它,您可以改用capture.output

invisible(capture.output(alpha(data, na.rm=F, check.keys=F, delete=F)))

capture.output 在内部调用sink(…, type = "output") 并丢弃/返回结果。

【讨论】:

  • 感谢康拉德的帮助。这确实可以解决问题,但不幸的是不适合我的目的。 alpha() 返回大量信息,我试图简单地提取其中的一部分以显示在 knit 文件中。使用 capture.output() 将所有内容转换为字符串使我的拉动不太清楚。不过,我学到了一个新的技巧,所以再次感谢。你说得对,也许 psych 的开发者应该修改他们的代码。
猜你喜欢
  • 1970-01-01
  • 2012-06-20
  • 2012-02-09
  • 2014-05-31
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多