title: R error, $ operator is invalid for atomic vectors
body: <pre><code>makeVector &lt;- function(x = numeric()) {
          m &lt;- NULL
          set &lt;- function(y) {
                  x &lt;&lt;- y
                  m &lt;&lt;- NULL
          }
          get &lt;- function() x
          setmean &lt;- function(mean) m &lt;&lt;- mean
          getmean &lt;- function() m
          list(set = set, get = get,
               setmean = setmean,
               getmean = getmean)
  }
  
  cachemean &lt;- function(x, ...) {
          m &lt;- x$getmean()
          if(!is.null(m)) {
                  message("getting cached data")
                  return(m)
          }
          data &lt;- x$get()
          m &lt;- mean(data, ...)
          x$setmean(m)
          m
  }
  </code></pre>
  
  <p>I tried running this code in R and i got the error $ operator is invalid for atomic vectors, which is quite understandable since x there is not recursive.
  but as evident from the code i need to work on a cached value.
  As far as i know the first function is working fine.
  Is there any other way I can get the cached values from first function, a replacement for $ operator?
  <a href="https://i.stack.imgur.com/SEm25.png" rel="nofollow noreferrer">Attached is a screenshot of the output of first function and the error in second function</a></p>
  
  <p>output :</p>
  
  <pre><code> makeVector(c(2,4,6,8))
  $set
  function(y) {
      x &lt;&lt;- y
      m &lt;&lt;- NULL
    }
  &lt;environment: 0x0000019d4335e130&gt;
  
  $get
  function() x
  &lt;environment: 0x0000019d4335e130&gt;
  
  $setmean
  function(mean) m &lt;&lt;- mean
  &lt;environment: 0x0000019d4335e130&gt;
  
  $getmean
  function() m
  &lt;environment: 0x0000019d4335e130&gt;
  
  &gt; cachemean(c(2,4,6,8))
  Error in x$getmean : $ operator is invalid for atomic vectors
  </code></pre>