Reputation: 156
Is there a way to provide parameters to reactive to make it work like normal functions. I am providing a piece of code of what I am trying to achieve. First, I create a reactive data where the user can select a project and a date range and the data gets updated upon click of the 'apply' button. Inside this reactive I wrap the data inside a function(x) which I call in the filter and most importantly group_by.
holidays <- reactive({input$apply
function(x){
isolate(
data() %>%
filter(x != non_holiday,
project %in% input$projects &
date >= input$sell_date[1] &
date <= input$sell_date[2]
) %>%
group_by(project, x) %>%
summarise(
Total= sum(quantity)
)
)
}
})
The idea is that I want to have a reactive data(holidays_data) used for plotting which is grouped by depending on what project was chosen by the user as shown below. For instance if the user selects project A, then the data will be grouped by column 1, B column 2 etc. I wanted to go for this way to avoid repetition of writing the same code for the different columns.
holidays_data <- reactive({input$button_g
switch (
input$projects,
"A" = holidays(column1),
"B" = holidays(column2),
"C" = holidays(column3)
)
})
With this method it gives me an error saying: 'Unused argument column1'. If someone has any suggestion that will take me in the right direction it will be greatly appreciated.
Upvotes: 1
Views: 1450
Reputation: 41260
You could try:
holidays <- function(input,data,x) {
reactive({
input$apply
#Create symbol from character
x <- rlang::sym(x)
isolate(
data() %>%
filter(!!x != 'non_holiday' &
projects %in% input$projects &
date >= input$sell_date[1] &
date <= input$sell_date[2]) %>%
group_by(project, !!x) %>%
summarise(
Total= sum(quantity)
)
)
})
}
This function can be called in a reactive context :
holidays(input,data,"DE_Holiday")
Upvotes: 2