Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : How to suppress a persistent warning message from alpha() function in my Rmd output?

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!

like image 294
mutableQ Avatar asked Dec 02 '25 18:12

mutableQ


1 Answers

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.

like image 161
Konrad Rudolph Avatar answered Dec 05 '25 03:12

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!