Skip to content

Commit

Permalink
Add spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
haziqj committed May 31, 2024
1 parent 66a5e6c commit fc37703
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: INLAvaan
Type: Package
Title: Bayesian Structural Equation Modelling with INLA
Version: 0.1.0.9012
Version: 0.1.0.9013
Authors@R:
c(person("Haziq", "Jamil",
email = "[email protected]",
Expand Down
2 changes: 2 additions & 0 deletions R/21-rgeneric_cached.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ inla_sem_cached <- function(
sd_z <- sqrt(exp(theta[idx_psi])) # sd_z = sd_z ^ 2 (latent sd)
lvrho <- theta_to_rho(theta[idx_lvrho])

if (!is.null(sp)) sp$spin()

list(
lambda = lambda,
beta = beta,
Expand Down
42 changes: 20 additions & 22 deletions R/30-inlavaan.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,25 @@ inlavaan <- function(

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if (target == "INLA") {

sp <- NULL
if (!verbose) {
current_cli.progress_show_after <- options("cli.progress_show_after")
options("cli.progress_show_after" = 0)
sp <- cli::make_spinner(
which = cli::get_spinner("bouncingBall"),
template = paste("{spin}", sample(cli_messages, 1)),
stream = "message",
static = "silent"
)
}

jagtrans <- try(
lav2inla(
lavobject = LAV,
lavdata = lavdata,
dp = dp
dp = dp,
sp = sp
# inits = initsin
)
)
Expand Down Expand Up @@ -824,27 +838,7 @@ inlavaan <- function(
res <- try(do.call(rjcall, rjarg))
} else {
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# spinner <- cli::make_spinner(
# # which = "simpleDotsScrolling",
# template = paste(sample(cli_messages, 1), "{spin}")
# )
# spinner$spin()
#
# f <- future({
# invisible({
# sink(tempfile()) # Redirect output to a temporary file
# require("INLA")
# result <- do.call("inla", rjarg)
# sink(NULL)
# result
# })
# }, seed = TRUE)
# while (!resolved(f)) {
# Sys.sleep(0.1) # Briefly pause to avoid hogging CPU
# spinner$spin() # The spinner automatically updates
# }
# res <- value(f)
# spinner$finish()

res <- do.call("inla", rjarg)

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Expand Down Expand Up @@ -1268,6 +1262,10 @@ inlavaan <- function(
# }
# }

if (!verbose) {
options("cli.progress_show_after" = current_cli.progress_show_after)
}

out
}

Expand Down
4 changes: 3 additions & 1 deletion R/40-lav_export_INLA.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ lav2inla <- function(
lavobject,
lavdata = NULL,
dp = NULL,
sp = NULL,
n.chains = 1,
mcmcextra = "",
inits = "prior",
Expand Down Expand Up @@ -1301,7 +1302,8 @@ lav2inla <- function(
q = pta$nfac[[1]],
init = inlastart,
partable = partable,
theta_to_rho = theta_to_rho # utility function, see 10-utilities.R
theta_to_rho = theta_to_rho, # utility function, see 10-utilities.R
sp = sp
# safe_solve = safe_solve
# optimize = TRUE
)
Expand Down
14 changes: 2 additions & 12 deletions inst/demo.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ true_model <- "
eta1 =~ 1*y1 + 1.2*y2 + 1.5*y3
eta2 =~ 1*y4 + 1.2*y5 + 1.5*y6
eta2 ~~ 0.3*eta1
y1 ~~ 0.05*y4
y2 ~~ 0.05*y5
y3 ~~ 0.05*y6
y1 ~~ 0.1*y1
y2 ~~ 0.1*y2
y3 ~~ 0.1*y3
y4 ~~ 0.1*y4
y5 ~~ 0.1*y5
y6 ~~ 0.1*y6
"
dat <- lavaan::simulateData(true_model, sample.nobs = 1000)

Expand All @@ -29,7 +18,8 @@ mod <- "
# y2 ~~ y5
# y3 ~~ y6
"
fit <- isem(model = mod, data = dat, meanstructure = FALSE, verbose = TRUE); summary(fit)
fit <- isem(model = mod, data = dat, meanstructure = FALSE, verbose = TRUE)
summary(fit)

# Political democracy SEM example ----------------------------------------------
myModel <- '
Expand Down
49 changes: 49 additions & 0 deletions inst/spinner.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Spinner in INLAvaan ----------------------------------------------------------
# devtools::install_github("haziqj/INLAvaan")
library(INLAvaan)
library(lavaan)

myModel <- '
# latent variables
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
ind60 =~ x1 + x2 + x3
# latent regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual covariances
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
# About 10 seconds
fit <- isem(model = myModel, data = PoliticalDemocracy)

# Example using cli progressbar ------------------------------------------------
fun <- function() {
# Setup
options(cli.spinner = "boxBounce")
options(cli.progress_show_after = 0)
cli_progress_bar(
name = "Laplace-ing through p dimensions",
format = "{cli::pb_spin} {cli::pb_name} [{cli::pb_elapsed}]",
clear = FALSE
)

# Simulating an inla call
while (TRUE) {
if (runif(1) < 0.01) break
Sys.sleep(0.05)
cli_progress_update()
}
cli_progress_update(force = TRUE)

# Defaults
options(cli.spinner = "dots")
options(cli.progress_show_after = 2)
}
fun()
2 changes: 1 addition & 1 deletion man/inlavaan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc37703

Please sign in to comment.