I'm writing a Shiny application where a large dataset is read and some analyses are performed depending on the user's input. These analyses are based on a variable y
that can be transformed from another variable x
, depending on a value k
entered by the user.
The problem here is that that conversion step is time demanding, so right now all possible values of y
are precomputed. Consider the following simplified example:
set.seed(1234)
data <- tibble (x = rnorm(n = 10, mean = 5, sd = 1) )
k_vector <- seq(from=1,to=3,by=1)
for (k in k_vector) {
new_col = stringr::str_c("y",k)
data <- dplyr::mutate(data, !!new_col := x*k)
}
Which results in the following table:
x y1 y2 y3
<dbl> <dbl> <dbl> <dbl>
1 3.79 3.79 7.59 11.4
2 5.28 5.28 10.6 15.8
3 6.08 6.08 12.2 18.3
4 2.65 2.65 5.31 7.96
5 5.43 5.43 10.9 16.3
6 5.51 5.51 11.0 16.5
7 4.43 4.43 8.85 13.3
8 4.45 4.45 8.91 13.4
9 4.44 4.44 8.87 13.3
10 4.11 4.11 8.22 12.3
I'd next save that table as .rda
, and read it from the shiny application. Then I would include a command like dplyr::transmute(data,x=x,y=y1)
(if k=1 in this case) in a reactive scope so that anytime the user changes the value of k
a new variable y
is selected. As you can imagine, this solved the problem of converting form x
to y
given k
.
But if the real dataset and/or the number of possible values of k
is large then the stored table will be HUGE, so it becomes a problem not just for storing but also of time when reading it. I'm avoiding the creation of N databases, one per value of k
, in the hope that there is a more efficient way to accomplish this task. Any ideas?