First off, this is a generic question that I'm hoping someone knowledgeable will be able to point me in the right direction of a method to achieve what I'm wanting to do. As such I don't have a reproducible example to share, but I will provide some example code that hopefully gets across what I'm trying to do.
I have an R Shiny dashboard app. This app uses a number of different datasets which are generated in the global environment (i.e. not within the server). Within the server, a filter needs to be applied to each dataset depending on user input. Hence, these datasets are reactive.
I currently have separate blocks of code for each dataset, like in the example code below. This works. However, I am looking to program this dynamically, so that I can apply the same code to a list of datasets without having to copy and edit the same chunks of code for each one individually.
Below is an example of what the chunks of code look like at the moment. They call a filter function which processes the dataset. Later parts of the code can makes calls to "filtered_data_apple()" for example, and it works as expected.
filtered_data_apple <- reactive({
data <- filter_data('apple',as.data.table(df_apple))
data
})
filtered_data_banana <- reactive({
data <- filter_data('banana',as.data.table(df_banana))
data
})
filtered_data_cherry <- reactive({
data <- filter_data('cherry',as.data.table(df_cherry))
data
})
What I want is to be able to provide a list (of fruits in this example) and for the server to loop through them and apply the same chunk of code to all of them and for other parts of the code to be able to call the datasets they produce without any errors.
The below code does NOT work, but hopefully demonstrates what I'm trying to do:
for (fruit in c('apple','banana','cherry')){
filtered_data_name <- paste('filtered_data_',fruit,sep="")
df_name <- paste('df_',fruit,sep="")
assign(filtered_data_name,
reactive({
data <- filter_data(fruit,as.data.table(get(df_name)))
data
})
)
}
The above method fails I believe because of when the code is evaluated in the Shiny server. I think the value of "fruit" ends up being the same (the last value in the list, "cherry") over each iteration. So it will work for the "cherry" dataset, but nothing else. I've tried encompassing the code within the loop within a local statement also, but that doesn't work because the resulting datasets remain contained within the local environment, and cannot be called from outside it. I've also tried using a repeat loop, but it too fails for the same reason as the for loop. The value of fruit would be "cherry" across all iterations.
Hopefully I've been clear enough to convey this problem, and I'm hoping someone will be able to provide the right method to get around this. There must be something, surely?
Thanks!
EDIT: For clarity, the datasets can contain completely different columns to one another. Hence why they are separate datasets. They are also very large datasets, so I wanted to limit the amount of filtering going on, so that it filters it once for that dataset, rather than filtering a larger dataset each time it is called which takes a lot longer to run.