简体   繁体   English

多个文件用于读取和保存

[英]multiple files for reading and saving

I have multiple files to read and also do heatmap after which the outputs will be saved. 我有多个文件要读取,也要进行热图绘制,之后将保存输出。 Somehow, there is a problem(s) with my code below and I can't figure out why it is not working. 不知何故,下面的代码存在问题,我无法弄清楚为什么它不起作用。 Files: mxn.dat, scu.dat, emun.dat, ser.dat 文件:mxn.dat,scu.dat,emun.dat,ser.dat

files <- list.files(pattern=".dat")
for (i in length(files)){
data <-read.table(files[i],row.names=1,header=T,sep='\t')
  for in length(files){
  png('i.png') 
  pheatmap(t(data[i]), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames =  T, cluster_cols = FALSE)
  dev.off()
  } 
}

Any help will be appreciated to get the code working. 任何帮助将使代码正常工作。

Thanks 谢谢

Rob

Not a reproducible example so I have no way to test if this will work (and somehow I think that this is too localised to be useful to future visitors), but perhaps try this... 这不是一个可复制的示例,因此我无法测试这种方法是否可行(而且我认为这过于本地化而无法对未来的访问者有用),但是也许尝试一下...

files <- list.files(pattern=".dat")

for (i in 1:length(files)){
  data <-read.table( files[i],row.names=1,header=T,sep='\t')
  png( paste0( i , '.png' ) )
    pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames =  T, cluster_cols = FALSE)
  dev.off()
  } 

Variation of SimonO101's solution: SimonO101解决方案的变化:

files <- list.files(pattern=".dat")

for( f in files )
{
  data <-read.table( f, row.names = TRUE, header = TRUE, sep = '\t' )
  png( gsub( "pdf", "png", f ) )
  pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames =  T,    cluster_cols = FALSE)
  dev.off()
} 

Easier to read (I believe) and has the advantage (?) of preserving the original file names, only changing the extension. 易于阅读(我相信)并且具有保留原始文件名(仅更改扩展名)的优点(?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM