我在R中的功能未绘制图

时间:2018-11-21 10:00:18

标签: r

我有一个计算库大小并绘制直方图的函数。

函数看起来像这样

 plotLibrarySize <- function(t, cutoffPoint) {
        options(repr.plot.width=4, repr.plot.height=4)

        hist(
            t$total_counts,
            breaks = 100
        )
        abline(v = cutoffPoint, col = "red")
    }

我从t_1到t_n的环境中有一个对象列表,我可以对其进行遍历以获取文件的大小。

for (i in 1:length(paths))
print(sum(get(t[i])$total_counts))

现在正常绘制它,我将使用

plotLibrarySize(t_1,2500)

但是,由于我有很多对象在使用循环

for (i in 1:5)
plotLibrarySize(get(t[i]), 2500)

这不会产生图或引发错误。有点令人困惑。

1 个答案:

答案 0 :(得分:2)

由于没有示例,因此很难发现问题。但是,下面的示例为我生成了三个图。

bar_1 <- data.frame(total_counts=rnorm(1000))
bar_2 <- data.frame(total_counts=rnorm(1000,1))
bar_3 <- data.frame(total_counts=rnorm(1000,2))

foo = function(t, cutoffPoint) {
  options(repr.plot.width=4, repr.plot.height=4)
  x=hist(t$total_counts,breaks=100)
  abline(v=cutoffPoint, col="red")
}

for(i in 1:3){
  foo(get(paste0("bar_",i))["total_counts"], 2)
}  

或者,引用您的列表(?),这也有效:

bars = list(bar_1, bar_2, bar_3)
for(i in 1:3){
  foo(get("bars")[[i]]["total_counts"], 2)
}

如前所述,对于列表,get是不必要的:

bars = list(bar_1, bar_2, bar_3)
for(i in 1:3){
  foo(bars[[i]]["total_counts"], 2)
}