简体   繁体   English

如何将多个图从自己的函数保存到 R 中的列表中?

[英]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.由于列表有 3 个元素,我们得到 3 个图。 图 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.但是,由于我需要使用我生成的所有图创建演示文稿,我发现这篇文章提供了一个解决方案,可以为 for 循环内的多个图创建幻灯片。 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.尽管我找不到将绘图保存到对象中的方法,但由于这篇文章export包,我找到了一种使用这些图像创建演示文稿的方法。

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")由于我当前的 R 版本(4.1.2)在 CRAN 中没有该包,我从 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))

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

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