title: R Get objects from environment and feed to function
body: <p>This is probably a pretty trivial question, but I'm much more used to python than to R (the fact I'm mostly a biologist might also play a role...)</p>
  
  <p>What the code below does is plot the counts for each gene in the provided data in an independent panel, and rearrange the legends in order to have a single one for all the panels in the plot.</p>
  
  <pre><code># function to rearrange plot legend, from here:
  # http://rpubs.com/sjackman/grid_arrange_shared_legend. Give credit where credit is due ;)
  
  grid_arrange_shared_legend &lt;- function(...) {
      plots &lt;- list(...)
      g &lt;- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
      legend &lt;- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
      lheight &lt;- sum(legend$height)
      grid.arrange(
          do.call(arrangeGrob, lapply(plots, function(x)
              x + theme(legend.position="none"))),
          legend,
          ncol = 1,
          heights = unit.c(unit(1, "npc") - lheight, lheight))
  } 
  
  # make plot for the given gene and assign it to a named object
  plot_genes &lt;- function(gene, gID){
      name&lt;-paste0("plotted_counts_for_", gene)
      counts = plotCounts("whatever") # get data using plotCounts from DESeq2 package. the gID is used in here
      return(assign(name, ggplot(counts,
                                  # + a bunch of plotting aestetics
                                  envir = .GlobalEnv)) #make plot available outside function. Probably I can also use parent.frame()
  }                                
  
  # call plot_genes() for each cluster of genes, adjust the legend for multiple plots with  grid_arrange_shared_legend()
  plot_cluster_count &lt;- function(cluster,name) { 
      genes = as.vector(as.data.frame(cluster)$Symbol)
      gIDs = as.vector(as.data.frame(cluster)$EMSEMBL)
  
      pdf(paste0(name,"_counts.pdf"))
      plt = lapply(seq_along(genes), function(x) plot_genes(genes[x], gIDs[x]))
      grid_arrange_shared_legend(plotted_counts_for_gene1, plotted_counts_for_gene2, plotted_counts_for_gene3,plotted_counts_for_gene4)
      dev.off()
  }  
  
  # call the whole thing
  plot_cluster_count(Cluster_1,"Cluster_1")
  </code></pre>
  
  <p>This code works.
  The issue is that it works only when I explicitely hard-code the names of the plots as in <code>grid_arrange_shared_legend(plotted_counts_for_gene1, plotted_counts_for_gene2, plotted_counts_for_gene3,plotted_counts_for_gene4)</code>.
  However I have plenty of clusters to plot, with different number of genes with different names, so I need to automate the selection of objects to feed to <code>grid_arrange_shared_legend()</code>.</p>
  
  <p>I tried to play around with <code>ls()/objects()</code>, <code>mget()</code> and Google but I can't find a way to get it working, I always end up with
  <code>Error in plot_clone(plot) : attempt to apply non-function</code>.
  I traced the error back with <code>options(error=recover)</code> and indeed it comes from <code>grid_arrange_shared_legend()</code>, so to me it looks like I'm not able to feed the objects to the function.</p>
  
  <p>The ultimate goal would be to be able to call <code>plot_cluster_count()</code> within a   <code>lapply()</code> statement feeding a list of clusters to iterate through. This should result in one pdf per cluster, each containing one panel per gene.</p>
  
  <p>PS
  I'm aware that getting the object names from the environment is not the most elegant way to go, it just seemed more straightforward. Any alternative approach is more than welcome</p>
  
  <p>Thanks!</p>