0

I am trying to loop through bunch of files and for each file I want to make a pdf file with pheatmap function. I have written something like this:

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()
})

At the pdf(file = "eval(a).pdf",height=25,width = 10) line, I want to evaluate the value of variable a so that I can get a different name for each pdf generated. How can I do this?

Thanks

user3138373
  • 519
  • 1
  • 6
  • 18

1 Answers1

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()
})
user3138373
  • 519
  • 1
  • 6
  • 18