简体   繁体   English

返回绘图而不是绘图列表的函数

[英]Function returning a plot not list of plots

I'm trying to run pvclust as a test Im running it in small subset of files .我正在尝试运行 pvclust 作为测试我在文件的一小部分中运行它。 The issue is it works but instead of list which i would have used downstream to print it to individual files it gets printed.问题是它有效,但不是列表,我将使用下游将其打印到打印的单个文件中。

My code我的代码

list_of_files <- list.files('Model_pvclust/',pattern = '\\.txt$', full.names = TRUE)


cmplx_ht<-function(file_list){
  start_time <- Sys.time()
  df_list<-list()
  #heat_list<-list()
  pv_list<-list()
  require(pvclust)
  
  for(f in file_list){
    message(paste0("Making pvclust for: ",f))
    
    #fname <- gsub("Model_hmap\/\/|\.txt","",f)
    df <- read.csv(f, header = TRUE, sep = "\t", check.names = FALSE)
    mat <- t(scale(t(as.matrix(df[,grepl("TCGA-",colnames(df))]))))
    rownames(mat)<-df$Symbol
    df_list[[f]]<-mat
    #print(head(mat))
   hm <-  pvclust(as.data.frame(t(mat)), method.hclust="complete",
            method.dist="euclidian",nboot = 10, parallel=T)
    
    #heat_list[[f]]<-hm 
    plot(hm)
   b <-pvrect(hm, alpha=.95)
   pv_list[[f]] <-b 
   #dev.off()
   
  # dev.off()
    message("Done")
  }
  end_time <- Sys.time()
  time_taken<- end_time-start_time
  message(paste0(time_taken,"s"))
  return(pv_list)
}

hm_lst<-cmplx_ht(list_of_files) 

To print it to individual files this is what Im doing要将其打印到单个文件,这就是我正在做的

for (i in 1:length(hm_lst)) {
  file_name = paste(names(hm_lst)[[i]],".pdf", sep="")
  #file_name = paste(names(hm_lst),".pdf", sep="")
  
  pdf(file_name,width = 15,height = 10)
  draw(hm_lst[[i]])
  dev.off()
}

But my hm_list is coming empty the output is getting printed which i don't want.但是我的 hm_list 变空了,输出正在打印,这是我不想要的。 I want to store the output as lists the want to print it to files我想将输出存储为想要将其打印到文件的列表

Im not sure what exactly I'm doing wrong.我不确定我到底做错了什么。 Any suggestion or help would be really appreciated任何建议或帮助将不胜感激

You can use recordPlot to store a base plot in a object.您可以使用recordPlot将基本图存储在对象中。

x = 1:10

plot(x)

# record the plot
g <- recordPlot()

# clean the device
plot.new()

# plot is saved to g
g

在对象 g 中绘图

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

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