R Shiny: how to bind a checkbox in the ui.R file to observe the function on the .R server?

I am trying to create a checkbox in a Shiny app that allows the user of a data product to select all available options, select / deselect one at a time, and throw an error message when all options in the checkbox group are deselected, I can get the input actionLink

in my ui.R file where all options are selected or deselected when the user clicks Select All and the plot is updated with the correct values. However, when the user deselects the options one at a time, I sometimes get an error and warning and the graph is not updated.

From what I can tell, every time the Social option is selected / deselected, the console will give an error and warning message and the plot is not updated, and then if the user selects / deselects “Social” again, the plot is updated and no error / warning messages are issued.

I wonder if this has to do with how I configured the function observe

in my server.R file with the data frame manipulations I did in reactive expression for 'ssDf', or how I defined the reactive expression for 'channels_input'.

Below is an abstract from my ui.R file.

channel_chr <- list("Display" = "Display", "Paid Search" = "Paid Search", "Organic Search" = "Organic Search", 
                "Email" = "Email", "Social" = "Social", "Direct" = "Direct", "Referral" = "Referral", 
                "Affiliate" = "Affiliate")

fluidPage(sidebarLayout(
  sidebarPanel(

  ## Other inputs

    checkboxGroupInput("channel", label = "Channel(s)", 
                       choices = channel_chr,
                       selected = as.character(channel_chr)),
    actionLink("selectall","Select All"),

  ## Other inputs

  ),
  mainPanel(
    uiOutput("plot_ui"),
    ggvisOutput("plot")

  )
))

      

And a relatively small section from my server.R file is below.

channel_chr <- list("Display" = "Display", "Paid Search" = "Paid Search", "Organic Search" = "Organic Search", 
                 "Email" = "Email", "Social" = "Social", "Direct" = "Direct", "Referral" = "Referral", 
                 "Affiliate" = "Affiliate")

shinyServer(function(input, output, session) {

    # If "Select All" is chosen in ui.R for input$selectall, select all checkboxes in input$channel
    observe({
            if (input$selectall == 0) { 
                ## I tried keeping this blank and return(NULL), but saw no difference
            } else if (input$selectall > 0 & input$selectall %% 2 == 0) {
                updateCheckboxGroupInput(session,"channel","Channel(s)",choices=channel_chr,
                                         selected=as.character(channel_chr))
            } else if (input$selectall %% 2 == 1) {
                updateCheckboxGroupInput(session,"channel","Channel(s)",choices=channel_chr,
                                     selected=c("Display"))
            } 
    })

    channels_input <- reactive ({ 
        temp_chr <- character(length=length(channel_chr))
        temp_channel <- input$channel
        temp_chr[1:length(temp_channel)] <- temp_channel
        return(temp_chr)
    })

# A subset of original data frame as reactive function
ssDf <- reactive({  

    ## contains dplyr, plyr, and ddply manipulations of data frame
    ## according to the user interactions with ui.R.

    ## One example of data frame manipulation in ssDf() reactive function
    ## based on user inputs, and using if/else condition statements

    else if (input$[inputB] == [option1] & all(sort(channels_input())!=sort(as.character(channel_chr)))) {

            ## If channels_input() != channel_chr { filter only those channels where Channel==channels_input() }
            filteredDat2 <- dplyr::filter(filteredDat, Channel==channels_input()[1])
            for (i in 2:length(channels_input())) {
                if (channels_input()[i] != "") {
                    filteredDat2 <- rbind(filteredDat2, dplyr::filter(filteredDat, 
                                                                      Channel==channels_input()[i]))
                }
            }
            filteredDat2 <- ddply(filteredDat2, .(Date, [Column2]), summarize, sum_[Variable1]=sum([Variable1]), 
                                 [Variable2]=weighted.mean([Variable2], [Variable1]), 
                                 [Variable3]=weighted.mean([Variable3], [Variable1]), 
                                 [Variable4]=weighted.mean([Variable4], [Variable1]), 
                                 [Variable5]=weighted.mean([Variable5], [Variable1]))
            ## Subsequent manipulations removed for brevity.        
            return(filteredDat2)
})

ssDf %>%
    ggvis(~Date, ~[Variable4], stroke = ~TimePeriod) %>%
    layer_lines() %>%
    add_axis("y", title = [Variable4], title_offset = 50) %>%
    scale_datetime("x", domain=c(startNowDate, endNowDate)) %>%
    add_tooltip(all_values, "hover") %>%
    bind_shiny("plot", "plot_ui")

      

Note that I have replaced some of my column, variable and input names with [Column2], [Variable4], etc. for this post. Also, my tooltip function is not working yet, so ignore the function all_values

in the function add_tooltip

in the pipeline ggvis

.

Any help figuring out where I went wrong, or advice on how to debug complex shiny applications with help ggvis

would be greatly appreciated.

Thank.

+3


source to share





All Articles