title: Adding to a list object in a mapping function in R
body: <p>I am creating a scatter plot matrix using <code>GGally::ggpairs</code>. I am using a custom function (below called <code>my_fn</code>) to create the bottom-left non-diagonal subplots. In the process of calling that custom function, there is information about each of these subplots that is calculated, and that I would like to store for later.</p>
  
  <p>In the example below, each <code>h@cID</code> is a int[] structure with 100 values. In total, it is created 10 times in my_fn (once for each of the 10 bottom-left non-diagonal subplots). I am trying to store all 10 of these <code>h@cID</code> structures into the <code>listCID</code> list object.</p>
  
  <p>I have not had success with this approach, and I have tried a few other variants (such as trying to put <code>listCID</code> as an input parameter to <code>my_fn</code>, or trying to return it in the end).</p>
  
  <p>Is it possible for me to store the ten <code>h@cID</code> structures efficiently through <code>my_fn</code> to be used later? I feel there are several syntax issues that I am not entirely familiar with that may explain why I am stuck, and likewise I would be happy to change the title of this question if I am not using appropriate terminology. Thank you!</p>
  
  <pre><code>library(hexbin)
  library(GGally)
  library(ggplot2)
  
  set.seed(1)
  
  bindata &lt;- data.frame(
      ID = paste0("ID", 1:100), 
      A = rnorm(100), B = rnorm(100), C = rnorm(100), 
      D = rnorm(100), E = rnorm(100))
      bindata$ID &lt;- as.character(bindata$ID
  )
  
  maxVal &lt;- max(abs(bindata[ ,2:6]))
  maxRange &lt;- c(-1 * maxVal, maxVal)
  
  listCID &lt;- c()
  
  my_fn &lt;- function(data, mapping, ...){
    x &lt;- data[ ,c(as.character(mapping$x))]
    y &lt;- data[ ,c(as.character(mapping$y))]
    h &lt;- hexbin(x=x, y=y, xbins=5, shape=1, IDs=TRUE, 
                xbnds=maxRange, ybnds=maxRange)
    hexdf &lt;- data.frame(hcell2xy(h),  hexID=h@cell, counts=h@count)
    listCID &lt;- c(listCID, h@cID)
    print(listCID)
    p &lt;- ggplot(hexdf, aes(x=x, y=y, fill=counts, hexID=hexID)) + 
              geom_hex(stat="identity")
    p
  }
  
  p &lt;- ggpairs(bindata[ ,2:6], lower=list(continuous=my_fn))
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/gWS83.png"><img src="https://i.stack.imgur.com/gWS83.png" alt="enter image description here"></a></p>