Problem with `print(summary(fit))` on `lavaan` fit object but only as part of package

The function foo below works as expected when ran interatively by printing the full lavaan fit summary, but not when ran as a package function. Interactive (works):

foo <- function(fit){
    print(summary(fit))
}
HS.model <- 'visual =~ x1 + x2 + x3'
library(lavaan)
fit <- lavaan::cfa(model = HS.model, data = HolzingerSwineford1939)
foo(fit)
#> lavaan 0.6-12 ended normally after 23 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of model parameters                         6
#> 
#>   Number of observations                           301
#> 
------------------[TRUNCATED FOR BREVITY]----------------
Created on 2022-08-26 by the [reprex package](https://reprex.tidyverse.org)

But when the same function is ran from within package:

HS.model <- 'visual =~ x1 + x2 + x3'
library(lavaan)
library(lavaanExtra)
fit <- lavaan::cfa(model = HS.model, data = HolzingerSwineford1939)
foo(fit)
#> Length  Class   Mode 
#>      1 lavaan     S4
Created on 2022-08-26 by the [reprex package](https://reprex.tidyverse.org)

As can be seen, it prints length, class, and mode, rather than the fit summary, as when ran interactively. What might be the solution for surely such a common problem?

Ok so the function is available here: lavaanExtra/foo.R at main · rempsyc/lavaanExtra · GitHub

And defined as such:

foo <- function(fit){
  print(summary(fit))
}

And the example just above (in the docs, ?foo). So if we try a fresh install with remotes::install_github("rempsyc/lavaanExtra") on a new R session:

library(lavaan)
library(lavaanExtra)
HS.model <- 'visual =~ x1 + x2 + x3'
fit <- lavaan::cfa(model = HS.model, data = HolzingerSwineford1939)
foo(fit)
#> Length  Class   Mode 
#>      1 lavaan     S4

<sup>Created on 2022-08-26 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>

OK I tried this:

#' @importFrom lavaan print.lavaan.fitMeasures

But it wasn't working:

object ‘print.lavaan.fitMeasures’ is not exported by 'namespace:lavaan' 

But turns out, one workaround is a simple import of the entire package(!!!)...

#' @import lavaan

Probably that this was necessary in order to access the internal print functions of lavaan, because even triple colons (lavaan:::print.lavaan.fitMeasures) didn't work.

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.