rstdoutdunn.test

control the output of dunn test


I'm using dunn.test() from the package dunn.test, and I don't want it to print anything to the std output (console) because I'm doing it many times, and I want to see if I have any warnings before that.

So I canceled the printing of kruskal wallis test, and the printing of the table like this-

dunn.test(x = data, g = grouping, kw = FALSE, table = FALSE)

but it still prints a newline after each test, is there any way to prevent it from printing the new line? or a way to catch the newline from being printed?

reproducible example of dunn.test-

library(dunn.test)
df <- data.frame(d = c(rnorm(100), rnorm(100, 5)),
                 group1 = rep(c('a','b','c','d'),50),
                 group2 = rep(c('a','b','c','d'),each =50))
test1 <- dunn.test(x = df$d, df$group1)
test2 <- dunn.test(x = df$d, df$group2)
test3 <- dunn.test(x = df$d, df$group1, kw = FALSE)
test4 <- dunn.test(x = df$d, df$group1, kw = FALSE, table = FALSE) # still prints a newline

Solution

  • You can use the capture.output() function to hide the output

    capture.output(dunn.test(x = df$d, df$group1))
    

    Any error or warning raised will still be displayed.