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.
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) }