0

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.

enter image description here

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 "government," I would like to be able to reuse this cached data frame in another visualization that also shows the word "government."

This is my problem: I have no idea how to:

a) efficiently generate cache for so many points (in total, I have thousands of points)

b) use this cache across visualizations.

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.

I would really appreciate insight to this problem.

Here is a sample of my data and code:

df <- structure(list(sentence_id = c("S1V0001P0_0", "S1V0001P0_11", 
                               "S1V0001P0_22", "S1V0001P0_33", "S1V0001P0_44", "S1V0001P0_55"
), text = c("moved that Lord Walsingham be appointed chairman of the committee of privileges for the present session.", 
            "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.", 
            "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.", 
            "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.", 
            "At present it was 5s.", "The period of he present bill was somewhat different from that of the above year, and was in its nature more definite."
)), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x55b3f5b1b030>)

vocab_list <- c("bill", "period", "committee")


quanteda_kwic <- function(df, kw) { 
  
  if (length(kw == 1)) {
    j <- as.data.table(kwic(df, kw, window = 300, valuetype = "fixed", separator = " ", case_insensitive = TRUE))
    
    return(j) } }

memo_quanteda_kwic <- memoise(quanteda_kwic, cache = cache_dir_quanteda_kwic)

for(word in vocab_list) {
  memo_quanteda_kwic(df, word) }

generic
  • 302
  • 1
  • 3
  • 14
  • Have you looked at `shiny::bindCache` (https://shiny.rstudio.com/articles/caching.html)? – r2evans Nov 28 '21 at 16:26
  • 1
    Yes, thank you -- really, I should clarify that my big problem is that I want to be able to generate all this cache with a for loop, instead of clicking every point on the visualization. – generic Nov 28 '21 at 16:41
  • Ahh, you want to pre-fill the cache. Gotcha. – r2evans Nov 28 '21 at 17:10
  • 1
    I asked the [same question](https://stackoverflow.com/questions/68840164/is-there-any-way-to-pre-cache-output-in-shiny) a while back – IceCreamToucan Nov 28 '21 at 17:18
  • Thank you, IceCream. So it looks like, however, that I may have to run the for loop in a way that simulates a user clicking on points, but I am still not sure if this would solve my problem where I would like to use the same cached data frame for multiple different search terms. So If I search for "harvest" and click on the word "government," I want to be able to use that same cached data frame if I search "bill" and click on "government." – generic Nov 28 '21 at 19:48
  • Just to clarify, do you need this pre-cache of every single point because of speed optimizations (eg loading the data.frame takes too long)? Because if the answer is yes, maybe you can share with us a minreprex of your filtering and selecting process to check if you can tackle that first. – Pabort Nov 29 '21 at 10:00
  • Yes, loading the data frame takes too long. Here is why: I am using the quanteda library to perform key-word-in-context (KWIC) on the point the user selects. This computation is really slow. quanteda does not seem to offer the ability to save the KWIC object to disk -- otherwise, I would do that instead. My solution, therefore, has been to cache the output. Caching makes the results appear in real time, but I have found it difficult to cache so many points. – generic Nov 29 '21 at 18:26
  • The KWIC code is included above, specifically this line: ``` j <- as.data.table(kwic(df, kw, window = 300, valuetype = "fixed", separator = " ", case_insensitive = TRUE)) ``` – generic Nov 29 '21 at 18:27

0 Answers0