R Shiny:如何链接ui.R中的复选框输入以观察server.R中的功能?

时间:2015-05-28 03:01:34

标签: r checkbox shiny observers ggvis

我正在尝试在Shiny应用中创建一个复选框输入,允许数据产品用户选择所有可用选项,一次选择/取消选择一个选项,并在取消选中复选框组中的所有选项时抛出错误消息。我能够在我的ui.R文件中输入actionLink输入,当用户点击“全部选择”时,选择或取消选择所有选项。并使用正确的值更新绘图。但是,当用户一次取消选择一个选项时,我偶尔会收到错误和警告消息,并且图表不会更新。

据我所知,每隔一段时间"社交"如果选择/取消选择选项,控制台将抛出错误和警告消息并且图表不会更新,然后如果用户选择/取消选择"社交"再次选项,绘图更新并且不会抛出任何错误/警告消息。

我想知道这是否与我在server.R文件中设置observe函数的方式有关,我已经在反应式表达式中完成了数据框操作对于&ssDf',或者我如何定义' channels_input'反应性表达。

以下是我的ui.R档案中的摘要。

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")

  )
))

我的server.R文件中的一个相对较小的部分在下面。

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's 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")

请注意,为了这篇文章,我用[Column2],[Variable4]等替换了我的一些列,变量和输入名称。此外,我的工具提示功能尚未起作用,因此请忽略all_values管道中add_tooltip函数中的ggvis函数。

任何帮助确定我出错的地方或使用ggvis调试复杂的Shiny应用程序的提示都将非常感激。

谢谢。

0 个答案:

没有答案