Home > Enterprise >  select field based on checkBoxGroupInput()
select field based on checkBoxGroupInput()

Time:10-03

I have a checkbox group input:

                checkboxGroupInput('siteconvs', 'Site conversions based on ...', 
                                   choices = c(
                                       'Trials' = 'Site Conversion Rate solo Trials', 
                                       'D2P' = 'Site Conversion Rate solo D2P'
                                       ), selected = c('Trials', 'D2P'),
                    inline = T, width = NULL, choiceNames = NULL, choiceValues = NULL
                )

Separately, I have a data frame:

summary_data %>% glimpse
Rows: 30
Columns: 12
$ date                               <date> 2021-09-01, 2021-09-02, 2021-09-03, 2021-09-04, 2021-09-05, 2021-09-06, 2021-09-07, 2021-09-08,$ Sessions                           <int> 3534, 3362, 3154, 2569, 2718, 3044, 3453, 3310, 3548, 3128, 2892, 3553, 3894, 3652, 3399, 3344,$ `Trial Sign Ups`                   <int> 86, 69, 76, 77, 66, 103, 93, 102, 137, 119, 116, 147, 146, 165, 133, 96, 45, 52, 46, 71, 85, 82,$ `Direct to Paid`                   <int> 4, 8, 2, 1, 1, 5, 3, 6, 5, 8, 4, 6, 11, 1, 3, 7, 8, 8, 13, 6, 8, 10, 11, 7, 12, 8, 14, 10, 19, 11
$ `Total Site Conversions`           <dbl> 90, 77, 78, 78, 67, 108, 96, 108, 142, 127, 120, 153, 157, 166, 136, 103, 53, 60, 59, 77, 93, 92$ `Expired Trials`                   <int> 81, 66, 69, 74, 61, 96, 81, 94, 122, 92, 105, 113, 98, 138, 100, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0,$ `Trial to Paid`                    <int> 7, 5, 7, 4, 7, 5, 4, 7, 6, 5, 4, 4, 7, 9, 5, 3, 1, 3, 4, 2, 8, 2, 0, 5, 1, 6, 3, 4, 4, 4
$ `Trial to Paid (End Date Passed)`  <int> 5, 4, 4, 3, 6, 4, 2, 5, 1, 0, 2, 1, 1, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$ `Site Conversion Rate`             <dbl> 0.02546689, 0.02290303, 0.02473050, 0.03036201, 0.02465048, 0.03547963, 0.02780191, 0.03262840,$ `Trial to Paid Conversion Rate`    <dbl> 0.08139535, 0.07246377, 0.09210526, 0.05194805, 0.10606061, 0.04854369, 0.04301075, 0.06862745,$ `Site Conversion Rate solo D2P`    <dbl> 0.0011318619, 0.0023795360, 0.0006341154, 0.0003892565, 0.0003679176, 0.0016425756, 0.0008688097$ `Site Conversion Rate solo Trials` <dbl> 0.02433503, 0.02052350, 0.02409639, 0.02997275, 0.02428256, 0.03383706, 0.02693310, 0.03081571,

Based on the users checkbox selection, I would like to ggplot accordingly:

first a variable that tells me which metric to use in geom_line based on the

trend_metric <- reactive({
    if(input$siteconvs == 'Site Conversion Rate solo Trials') {
        'Site Conversion Rate solo Trials'
        } else if (input$siteconvs == 'Site Conversion Rate solo D2P') {
            'Site Conversion Rate solo D2P'
            } else {
                'Trial to Paid Conversion Rate'
                }
    })

In English, if they select one or the other checkbox for trials or d2p only, then the corresponding string either 'Site Conversion Rate solo Trials' or 'Site Conversion Rate solo D2P', otherwise, assume they have both selected or none selected, in which case use 'Trial to Paid Conversion Rate'.

I tried to ggplot based on this:

output$trend_plot <- renderPlot(
    if(! 'Sessions' %in% names(summary_data())) {
        ggplot()
    } else {
        summary_data() %>%
            ggplot(aes(date, Sessions))  
            geom_col(fill = '#39cccc', na.rm = T)  
            geom_line(aes(y =  .data[[trend_metric()]] * max_sess()), color = 'tomato', na.rm = T, size = 1)  
            theme(axis.title.x = element_blank(),
                  axis.title.y = element_blank())
    })

When I run this I get console errors:

Warning: Error in if: argument is of length zero                                                                                            0s
  133: <reactive:trend_metric> [/home/rstudio/Volume/Projects/analytics-nz-marketing-dashboard/app.R#340]
  117: trend_metric
  115: <reactive:rawd> [/home/rstudio/Volume/Projects/analytics-nz-marketing-dashboard/app.R#147]
   99: rawd
   96: renderUI [/home/rstudio/Volume/Projects/analytics-nz-marketing-dashboard/app.R#177]
   95: func
   82: renderFunc
   81: output$evg_device
    1: runApp
Warning: Error in if: argument is of length zero
  101: <Anonymous>

If I edit the ggplot to be a hard coded value for y in geom_line, e.g.

geom_line(aes(y = Site Conversion Rate solo Trials...

Then it works and the plot renders. But not if I try to make it dynamic based on the checkbox inputs.

How can I tell ggplot to use for y in geom_line either of Site Conversion Rate solo Trials in the case of the trials checkbox being selected only, Site Conversion Rate solo D2P in the case of D2P being selected only else Trial to Paid Conversion Rate as the default if multiple or no checkboxes are selected?

Have tried !! sym(trend_metric()) too with similar errors.

CodePudding user response:

Difficult to reproduce as working code was not provided by OP. This error Warning: Error in if: argument is of length zero generally means you have NULL value in your input. Try to wrap it in if(!is.null(input$yourid)) {...}

Try to debug with --

observe({
  print(input$yourid)
})
  • Related