Skip to content

Commit

Permalink
Merge pull request #399 from lucdw/master
Browse files Browse the repository at this point in the history
Global switch (and function) to use lavaanC.
  • Loading branch information
yrosseel authored Dec 4, 2024
2 parents ab477a2 + 4531496 commit 3105868
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export("lavaan", "cfa", "sem", "growth",
"lavResiduals",
# utilities
"getCov", "char2num", "cor2cov",
"lavOptions",
"lavOptions", "lav_use_lavaanC",

"modindices", "modificationIndices", "modificationindices",
"standardizedSolution", "standardizedsolution",
Expand Down
22 changes: 22 additions & 0 deletions R/lav_options_default.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

lavaan_cache_env <- new.env(parent = emptyenv())

# function to get or set the switch to use c++ code in lavaanC
lav_use_lavaanC <- uselavaanC <- function(x) {
if (missing(x)) {
if (!exists("opt.lavaanC", lavaan_cache_env)) {
assign("opt.lavaanC", requireNamespace("lavaanC", quietly = TRUE), lavaan_cache_env)
}
return(get("opt.lavaanC", lavaan_cache_env))
} else {
if (!is.logical(x) || length(x) != 1L)
lav_msg_stop(gettext("'x' must be a scalar logical"))
if (x) {
if (!requireNamespace("lavaanC", quietly = TRUE)) {
lav_msg_warn(gettext("cannot use lavaanC, package not found."))
assign("opt.lavaanC", FALSE, lavaan_cache_env)
return(invisible(NULL))
}
}
assign("opt.lavaanC", x, lavaan_cache_env)
return(invisible(NULL))
}
}

# functions to handle warn/debug/verbose options
# (no longer in 'standard' options)
# if x not present returns the current value of opt.warn/debug/verbose
Expand Down
2 changes: 1 addition & 1 deletion R/lav_syntax_parser_cr.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ldw_parse_model_string_cr <- function(model.syntax = "",
paste(unlist(model.syntax), "", collapse = "\n")
)

if (requireNamespace("lavaanC", quietly = TRUE)) {
if (lav_use_lavaanC()) {
flat <- lavaanC::lav_parse_model_string_c(modelsrc)
} else {
flat <- lav_parse_model_string_r(modelsrc)
Expand Down
27 changes: 27 additions & 0 deletions man/lav_use_lavaanC.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
\name{lav_use_lavaanC}
\alias{lav_use_lavaanC}
\alias{uselavaanC}
\title{use C++ code in lavaanC}
\description{
Get or set switch to use the C++ code in lavaanC.}
\usage{
lav_use_lavaanC(x)
}
\arguments{
\item{x}{Optional scalar logical to set the switch ON or OFF.}
}
\value{
If \code{x} is missing the current value of the switch (logical).
If \code{x} is provided invisible(NULL).
}
\details{
The default value of the switch is ON (TRUE) if the \code{lavaanC} is available and
OFF (FALSE) if it is not. The switch cannot be set to TRUE if \code{lavaanC} is not available
(a warning message is issued).
}
\examples{
lav_use_lavaanC(FALSE)
lav_use_lavaanC()
lav_use_lavaanC(TRUE)
lav_use_lavaanC()
}

0 comments on commit 3105868

Please sign in to comment.