(R)将Environment上的对象循环到函数中并导出结果



我想从几个数据帧导出绘图。

我尝试使用1(for循环,并将ls((传递给2(函数,但在任何情况下,我使用的函数(regplot(都只能达到字符向量:数据错误[,1]:维数不正确

  1. 伪数据示例:
cbind.data.frame(x = 0:6, y = c(1, 2, 3, 6.1, 5.9, 6, 6.1)) -> data1
cbind.data.frame(x = 0:6, y = c(2, 4, 6, 12.2, 11.8, 12, 12.2)) -> data2
  1. 对于循环:
require(easyreg)
for (i in base::ls()) { i |> easyreg::regplot(model = 3) }
  1. 尝试使用函数:
test_fun <- function(x) { x |> regplot(model=3) }
test_fun(ls())
  1. 我想做的事情:
for (i in base::ls()) {
  svglite::svglite(filename = paste0(i,".svg"))
  i |> easyreg::regplot(model = 3)
  dev.off()
}

您可以跳过步骤2。和3。

# require(svglite)
for (i in ls(pattern = "data")) { # Assuming that all the dataframes'names contain the word "data"
  svglite::svglite(filename = paste0(i,".svg"))
  get(i) |> easyreg::regplot(model = 3)
  dev.off()
}

受此评论启发

最新更新