Disaggregate in the context of a time series

前端 未结 3 1301
遥遥无期
遥遥无期 2021-01-23 06:47

I have a dataset that I want to visualize overall and disaggregated by a few different variables. I created a flexdashboard with a toy shiny app to select the type of disaggrega

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 07:21

    I think you can make some gains by changing the order of your preparation. Right now the flow of your app is approximately:

    Data => prepare all combinations => select desired visualization => make plot

    Consider instead:

    Data => select desired visualization => prepare required combination => make plot

    This would make use of Shiny's reactivity to (re)prepare the data required for the requested plot in response to changes in the user's selection.

    By way of code snippets (Sorry, I don't have sufficient familiarity with flexdashboard and tibbletime to ensure this code runs, but I hope it is enough to highlight the approach):

    Your control selects the column you want to focus on (note we use "All" = "'1'" so this evaluates to a constant in the group-by, else it has to be handled separately):

    radioButtons("diss", label = "Disaggregation",
                 choices = list("All" = "'1'",
                                "By Sex" = "sex",
                                "By Language" = "lang",
                                "By other" = "column_name_of_'other'"), 
                 selected = 1)
    

    And then use this in your group by to prepare only the data required for the present visualization (you'll need to adjust the function suggested by @Jon_Spring in response to this earlier group-by):

    preped_dat = reactive({
      dat %>%
        group_by_(input$diss) %>%
        # etc
    })
    

    Before plotting (you'll need to adjust the plotting function in response to the possible change in data format):

    renderDygraph({
      totals = preped_data()
      dygraph(totals) %>%
          dySeries("total", label = ) %>%
          dyRangeSelector()
    })
    

    With regard to group_by you can use group_by_ if all your arguments are text strings, or group_by(!! sym(input$diss), other_column_name) if you want to mix the text string input from your control with other column names.

    One possible disadvantage of this change in approach is reduced responsiveness during interactivity if your data set is large. The present approach does all the computation up front and then minimal computation each selection - this may be preferable if you have a large amount of processing. My suggested approach will have minimal up front processing and moderate computation each selection.

提交回复
热议问题