1

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.

1 Answers1

1

It would probably be easier if you created reactiveVal() for each of your values and then just observed the change on the input. For example


dateFrom = reactiveVal()
duration = reactiveVal()
dateStep = reactiveVal()
dateLen = reactiveVal()
dateFormat = reactiveVal()

observe({
    if (input$vue == "10 derniers jours"){
        dateFrom(input$dateTo - 9)
        duration("10 days ago")
        dateStep("day")
        dateLen(10)
        dateFormat("%d-%m")
    } else if (input$vue == "8 dernières semaines"){
        dateFrom(ceiling_date(input$dateTo, "week") + 1 - 8*7)
        duration("8 weeks ago")
        dateStep("week")
        dateLen(8)
        dateFormat("%U-%Y")
    } else if (input$vue == "12 derniers mois"){
        dateFrom(ceiling_date(input$dateTo, "month") - months(12))
        duration("12 months ago")
        dateStep("month")
        dateLen(12)
        dateFormat("%m-%Y")
    } else if (input$vue == "5 dernières années"){
        dateFrom(ceiling_date(input$dateTo, "year") - years(5))
        duration("5 years ago")
        dateStep("year")
        dateLen(5)
        dateFormat("%Y")
    }
})
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you very much. I didn't know about this technique. I work pretty fine. I wonder if your code lines checks input$vue anytime an new input is send (for example from another input) ? –  Dec 20 '22 at 17:17
  • I'm not quite sure what you are asking. But you should be able to use any reactive values inside of the `observe()` – MrFlick Dec 20 '22 at 18:11