Igor Cobelo
Igor Cobelo

Reputation: 459

How to print the result of a function without using print ()?

I need to print the result of the Tukey test result within my function of my package. However, CRAN replied that I cannot use print () / cat (). If I use message (), the message does not come out in the format I want, as in print ().

res.aov <- aov(y ~ x, data= data)
  tukey<-TukeyHSD(res.aov)

print(tukey$`x[, 1]`)

With print (), the result comes out as I want it to appear on the console:

         diff         lwr        upr      p adj
2-1 0.0188276 0.003123183 0.03453202 0.01922062

But with message (), the result looks like this:

0.01882759965134230.003123182712210140.03453201659047440.0192206190347084

Any suggestions for the result to come out as with print ()? Thanks.

Upvotes: 2

Views: 301

Answers (1)

VFreguglia
VFreguglia

Reputation: 2311

You can use the capture.output() function to save the wanted result from the print function to a variable. Then you properly format the strings and pass it to message(). See the example below:

> x <- c(a = 1, b = 2)
> print(x)
# a b 
# 1 2 
> msg <- capture.output(print(x))
> message(paste(msg, collapse = "\n"))
# a b 
# 1 2 

Upvotes: 4

Related Questions