2

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
T.G.
  • 743
  • 3
  • 6
  • 27

2 Answers2

4

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.

csgillespie
  • 59,189
  • 14
  • 150
  • 185
0

This answer worked, but the captured output was still printed. Here is a reprex of what worked for me:

library(dunn.test)

capture.output(df <-
  as.data.frame(dunn.test(
    x = iris$Sepal.Length,
    g = iris$Species,
    table = FALSE,
    kw = FALSE,
    label = FALSE,
    alpha = 0.05
  )), file = "NULL")

df
#>       chi2         Z            P   P.adjusted            comparisons
#> 1 96.93744 -6.106326 5.097522e-10 5.097522e-10    setosa - versicolor
#> 2 96.93744 -9.741785 1.000049e-22 1.000049e-22     setosa - virginica
#> 3 96.93744 -3.635459 1.387433e-04 1.387433e-04 versicolor - virginica

Created on 2020-04-17 by the reprex package (v0.3.0.9001)

Note that no message was printed and the output was also assigned to a dataframe.

Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51