将R中的对象名称作为文件名扩展为R中的pdf函数

时间:2019-07-02 20:34:42

标签: r pdf lapply pheatmap

我正试图遍历文件堆,对于每个文件,我想使用pheatmap函数制作一个pdf文件。我写了这样的东西:

lapply(files, function(x) {
  a <- basename(x)
  a <- gsub(".txt","",a)

  d <- read.table(gzfile(x),header = FALSE, sep="\t",skip=1)
  d<- d[,c(4,7:306)]
  dmat <- d[,-1]
  rownames(dmat) <- d[,1]
  pdf(file = "eval(a).pdf",height=25,width = 10)
    x <- pheatmap(dmat,
                  scale="none",
                  cluster_rows = FALSE,
                  cluster_cols = FALSE,
                  annotation_names_col = FALSE,
                  show_colnames= FALSE,
                  color = colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(500),
                  main = a
                  )
  dev.off()
})

pdf(file = "eval(a).pdf",height=25,width = 10)行,我想评估变量 a 的值,以便为生成的每个pdf取一个不同的名称。我该怎么办?

谢谢

1 个答案:

答案 0 :(得分:0)

lapply(files, function(x) {
  a <- basename(x)
  a <- gsub(".txt","",a)

  d <- read.table(gzfile(x),header = FALSE, sep="\t",skip=1)
  d<- d[,c(4,7:306)]
  dmat <- d[,-1]
  rownames(dmat) <- d[,1]
  pdf(file = paste(a,".pdf",sep = "",collapse = NULL),height=25,width = 10)
    x <- pheatmap(dmat,
                  scale="none",
                  cluster_rows = FALSE,
                  cluster_cols = FALSE,
                  annotation_names_col = FALSE,
                  show_colnames= FALSE,
                  color = colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(500),
                  main = a
                  )
  dev.off()
})