Elad
Elad

Reputation: 33

save html files on a loop with tab_model on sjPlot in R

In a certain loop I am trying to use tab_model and save with it all the outcomes:

keys<-c("a","b","c")

for (i in keys) {
  formula <- as.formula(paste0("int", "~","int2", "+","int3"))
  x2 <- lme(formula, data = df, random = ~ 1 | int4, na.action = na.omit)
  tab_model(x2, file = paste0('name_', i,"_toshow.html"))
}

When I do that it runs and do not download the files (it works fine when I don't do that in a loop)

Upvotes: 2

Views: 793

Answers (1)

Zoe
Zoe

Reputation: 1000

I took some sample data from the documentation of the function. It's easier / faster to help if you provide some dummy data / a reproducible example!

# load package
library(sjPlot)
library(sjmisc)
library(sjlabelled)

# sample data
data("efc")
efc <- as_factor(efc, c161sex, c172code)

# sample regression
m1 <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, data = efc)

# try
for (i in 1:3) {
  print(tab_model(m1, file = paste0("test_",i,".html")))
}

It works when you put print() around the tab_model() function. This is because in loops, outputs are not automatically printed out. See here: Why do R objects not print in a function or a "for" loop?. Since the functions seems to be depending on printing the output before saving it, you have to "manually" print it.

Upvotes: 3

Related Questions