简体   繁体   中英

How to save several plots from an own function into a list in R?

I have created one function that contains two types of plots and it will give you one image. However, the title of this image will change depending on one list, so, you will have several plots but different title. (The original function will change the numbers that the plot uses but, in essence, is what I need).

This is the example that I have created.

list_genes <- c("GEN1", "GEN2", "GEN3")


myfunction <- function(x,y){
  
  for(gene in list_genes){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      plot(x,y, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
  }
}

myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))

Since the list has 3 elements, we get 3 plots. 图 1

However, as I need to create a presentation with all the plots that I generate, I found this post with a solution to create slides for several plots inside a for loop. It is what I want, but for that, I need to save my plots into a list/variable.

object <- myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
> object
NULL

I found this post (which gives you an interesting solution) but, the plots still cannot be saved into an object.

calling_myfunc <- function(){
  myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
}

calling_myfunc()

object <- calling_myfunc()
> object
NULL

My final objective is to create a presentation (automatically) with all of the plots that I generate from my function. As I saw in this post . But I need to save the plots into a variable.

Could anyone help me with this?

Thanks very much in advance

Although I couldn't find the way to save the plots into an object, I found a way to create a presentation with those images thanks to this post and the export package.

library(export) 

list_genes <- c("GEN1", "GEN2", "GEN3")


myfunction <- function(x,y){
     for(gene in list_genes){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      plot(x,y, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
      graph2ppt(file="plots.pptx", width=6, height=5,append=TRUE)    } }

myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))

Of course, the width and height of the plots can be changed or put them as a parameter in the function.

Since the package is not available in CRAN for my current R version (4.1.2), I downloaded it from GitHub: devtools::install_github("tomwenseleers/export")

In addition, I have found another package that I can use for the same purpose (although it adds one extra slide at the beginning, I don't know why)

library(eoffice)

list_genes <- c("GEN1", "GEN2", "GEN3")


myfunction <- function(x,y){
  
  for(gene in list_genes){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      plot(x,y, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
      topptx(file="plots.pptx", width=6, height=5,append=TRUE) 
  }
}

myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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