Prevent printing in a function, containing print command

Hi,

My issue is that I want to use the forecast::checkresiduals function inside my function, however, it contains a print command, and I am not able to prevent printing the full test description, even if I only want to see the p.value.

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
data('JohnsonJohnson')
mod <- forecast::auto.arima(JohnsonJohnson)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

f <- function(x) {
  p <- forecast::checkresiduals(x, plot = F)$p.value
  return(p)
}

f(mod)
#> 
#>  Ljung-Box test
#> 
#> data:  Residuals from ARIMA(3,1,1)(0,1,0)[4]
#> Q* = 5.6193, df = 4, p-value = 0.2294
#> 
#> Model df: 4.   Total lags used: 8
#> [1] 0.229439

Created on 2020-12-22 by the reprex package (v0.3.0)

Thank you for your answer in Advance,
Marcell

You can get that with sink() or capture.output():

ff <- function(x) {
  print("Ha")
  return(x)
}
y1 <- ff(3)
#> [1] "Ha"

capture.output( y2 <- ff(4), file = nullfile())

y1 == 3 && y2 == 4
#> [1] TRUE

Created on 2020-12-22 by the reprex package (v0.3.0)

1 Like

Read the help file.

test
Test to use for serial correlation. By default, if object is of class lm, then test="BG". Otherwise, test="LB". Setting test=FALSE will prevent the test results being printed.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.