title: Efficent Way to Generate Cache in R or Shiny R
body: <p>I have a shiny app that visualizes a scatter plot. The user can type a different word in the text box, and a different scatter plot will render. When the user clicks on a point, a data frame appears with information about the selected point.</p>
  <p><a href="https://i.stack.imgur.com/9YBNc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9YBNc.png" alt="enter image description here" /></a></p>
  <p>Right now I am using memoise to cache the data frame, that way the next time someone clicks on a point the data frame appears faster. I would like, however, to generate cache for every point in every data frame, and to be able to use this cache across visualizations. For example, if someone clicks on a point with the word &quot;government,&quot; I would like to be able to reuse this cached data frame in another visualization that also shows the word &quot;government.&quot;</p>
  <p>This is my problem: I have no idea how to:</p>
  <p>a) efficiently generate cache for so many points (in total, I have thousands of points)</p>
  <p>b) use this cache across visualizations.</p>
  <p>Right now a user can click on a point an the cache will be stored, but it would be impractical for me (or anyone) to click every point. I tried writing a for loop to cycle through each word and store a cached data frame for that word, but when I provide the cache to my Shiny app, it (unsurprisingly) does not work -- probably because the cache key is different for my app than the for loop that generates the cached data frames.</p>
  <p>I would really appreciate insight to this problem.</p>
  <p>Here is a sample of my data and code:</p>
  <pre><code>df &lt;- structure(list(sentence_id = c(&quot;S1V0001P0_0&quot;, &quot;S1V0001P0_11&quot;, 
                                 &quot;S1V0001P0_22&quot;, &quot;S1V0001P0_33&quot;, &quot;S1V0001P0_44&quot;, &quot;S1V0001P0_55&quot;
  ), text = c(&quot;moved that Lord Walsingham be appointed chairman of the committee of privileges for the present session.&quot;, 
              &quot;The subject, in his contemplation, was such as not only that house, but many thousands of his Majesty's subjects must regard with the utmost concern.&quot;, 
              &quot;He agreed with the noble earl as to the great importance of the subject; it was one therefore which naturally attracted the serious attention of his Majesty's government: their lordships, however, would be at the same time aware of the complexity and intricate nature of the general subject, the variety of details which it embraced, and the correspondent difficulty of forming adequate regulations.&quot;, 
              &quot;He was convinced, for his own part, that the contrary was the fact; and, though die abuse had not been flagrant or excessive, that the Bank had yielded, in a certain degree, to the temptation, and extended the quantity of their notes beyond the proper limits.&quot;, 
              &quot;At present it was 5s.&quot;, &quot;The period of he present bill was somewhat different from that of the above year, and was in its nature more definite.&quot;
  )), row.names = c(NA, -6L), class = c(&quot;data.table&quot;, &quot;data.frame&quot;
  ), .internal.selfref = &lt;pointer: 0x55b3f5b1b030&gt;)
  
  vocab_list &lt;- c(&quot;bill&quot;, &quot;period&quot;, &quot;committee&quot;)
  
  
  quanteda_kwic &lt;- function(df, kw) { 
    
    if (length(kw == 1)) {
      j &lt;- as.data.table(kwic(df, kw, window = 300, valuetype = &quot;fixed&quot;, separator = &quot; &quot;, case_insensitive = TRUE))
      
      return(j) } }
  
  memo_quanteda_kwic &lt;- memoise(quanteda_kwic, cache = cache_dir_quanteda_kwic)
  
  for(word in vocab_list) {
    memo_quanteda_kwic(df, word) }
  
  </code></pre>