3

Program : R 3.2.1 for Mac OSX
IDE : RStudio
Output : rmkd > html
Package : "psych"
Function : alpha()


Problem :
While using alpha(data, na.rm=F, check.keys=F, delete=F), because portions of the input-data is negatively correlated and because I have check.keys = FALSE, I get the following message :

Some items XXX 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

Question :
My check.keys is set intentionally. Fully understanding the implications of the warning & mostly for aesthetic and educational reasons, how can I suppress it in my output?

Attempts so far :
1. I've tried suppressWarnings() & suppressMessages().
2. I've tried invisible() & sink(., type="message").
3. In the Rmd block, I've tried : ```{r warning=F, message=F}
4. Exploring print(alpha) I found what I think is the origin. Maybe someone understands how to suppress this part of the code? :

`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")
         }
}`

thanks!

mutableQ
  • 31
  • 2

1 Answers1

2

The culprit here is cat, which will not heed suppressMessages etc.

To catch it, you can use capture.output instead:

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

capture.output calls sink(…, type = "output") internally and discards/returns the result.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    thanks Konrad for your help. This does do the trick, but unfortunately doesn't suit my purposes. alpha() returns a lot of information and I'm trying to simply pull a portion of it to display in the knit file. Transforming everything into a character string using capture.output() makes my pulling less clear. Nevertheless, I learned a new trick, so thanks again. You're right that perhaps the developer of psych should modify their code. – mutableQ Aug 12 '15 at 19:03