I'm trying to improve my code on the server side of my R Shiny App. I have 5 different variables which are: dateFrom
, duration
, step
, length
, and format
that depend on an input condition. My code for defining dateFrom
is as follows :
dateFrom = reactive({
if (input$vue == "10 derniers jours"){
input$dateTo - 9
}else if (input$vue == "8 dernières semaines"){
ceiling_date(input$dateTo, "week") + 1 - 8*7
}else if (input$vue == "12 derniers mois"){
ceiling_date(input$dateTo, "month") - months(12)
}else if (input$vue == "5 dernières années"){
ceiling_date(input$dateTo, "year") - years(5)
}
})
Then for duration
is :
duration = reactive({
if (input$vue == "10 derniers jours"){
"10 days ago"
}else if (input$vue == "8 dernières semaines"){
"8 weeks ago"
}else if (input$vue == "12 derniers mois"){
"12 months ago"
}else if (input$vue == "5 dernières années"){
"5 years ago"
}
})
And so on for every variable in my list.
I have reduced my code length by grouping my variables into this list output, but this is still inefficient.
## setDateList
# [1]: dateFrom
# [2]: duration
# [3]: step
# [4]: length
# [5]: format
dateList = reactive({
if (input$vue == "10 derniers jours"){
list(input$dateTo - 9, "10 days ago", "day", 10, "%d-%m")
}else if (input$vue == "8 dernières semaines"){
list(ceiling_date(input$dateTo, "week") + 1 - 8*7, "8 weeks ago", "week", 8, "%U-%Y")
}else if (input$vue == "12 derniers mois"){
list(ceiling_date(input$dateTo, "month") - months(12), "12 months ago", "month", 12, "%m-%Y")
}else if (input$vue == "5 dernières années"){
list(ceiling_date(input$dateTo, "year") - years(5), "5 years ago", "year", 5, "%Y")
}
})
dateFrom = reactive({dateList()[1]})
duration = reactive({dateList()[2]})
dateStep = reactive({dateList()[3]})
dateLen = reactive({dateList()[4]})
dateFormat = reactive({dateList()[5]})
})
I have also tried to use %<-% from a package to assign multiple values from reactive/observe but didn't succeed in implementing a working solution.