sjplot sjt.corr 在 R 中的循环中似乎表现不佳

sjplot sjt.corr doesn't appear to play nice in a loop in R

R 的业余用户,只是在简化一些代码方面遇到困难。我有一个测试结果数据帧列表,我希望 运行 这些数据帧中的 sjt.corr 函数用于质量保证。

编辑

一个可重现的小例子:

library(sjplot)
list <- list()
list$a <- as.data.frame(cbind(c(1,2,3,4,5,6,7,8),c(1,2,3,4,5,7,6,8)))
list$b <- as.data.frame(cbind(c(1,2,3,4,5,7,6,8),c(7,6,8,5,4,3,2,1)))
list$c <- as.data.frame(cbind(c(7,6,8,5,4,3,2,1),c(1,2,3,4,5,6,7,8)))

我不确定我的循环在哪里失败了。如果您看到错误,请告诉我:

for (i in seq_along(list)) {
  sjt.corr(
    list[[i]],
    na.deletion = "pairwise",
    corr.method = "spearman",
    file = paste("consensus", i, "html", sep = ".")
  )
} 

似乎默默地工作,但不保存任何东西。

这个有效:

sjt.corr(
  list[[1]],
  na.deletion = "pairwise",
  corr.method = "spearman",
  file = paste("consensus", 1, "html", sep = ".")
)

所以我朋友的解决方案是只使用 lapply。

i <- 0

lapply(list, function(x) {
  i <<- i + 1
  sjt.corr(
    x,
    na.deletion = "pairwise",
    corr.method = "spearman",
    file = paste("consensus", i, "html", sep = "."))
})

效果很好!仍然不确定为什么原来的 for 循环不起作用。任何对另一个类似场景的引用都会很棒。