动态清除selectizeInput

时间:2018-07-26 06:53:22

标签: r shiny selectize.js

我使用由“ renderUI”创建的多个selectizeInput字段。

    output$groupe <- renderUI ({
    if(input$numbergroups > 4) {
    KG = c("control group", "")
    TV = c("1 x 1st position in ad break", "1 x PreSplit before ad break", "1 x TV spot within ad break", "2 x TV spot within ad break", "TV spot within ad break & PreSplit before ad break", "1st position in ad break & PreSplit before ad break", "")
    YT = c("1 x YouTube Trueview Desktop", "2 x YouTube Trueview Desktop", "1 x YouTube PreRoll Desktop", "2 x YouTube PreRoll Desktop", "1 x YouTube TrueView Mobile", "2 x YouTube TrueView Mobile",
           "1 x YouTube Bumper Mobile", "2 x YouTube Bumper Mobile", "YouTube Trueview Desktop & YouTube TrueView Mobile", "YouTube Trueview Desktop & YouTube Bumper Mobile",
           "YouTube PreRoll Desktop & YouTube Bumper Mobile", "YouTube TrueView Mobile & YouTube Bumper Mobile", "")
    FB = c("1 x Facebook Post Video", "2 x Facebook Post Video", "")
    TVYTFB = c("TV spot within ad break & YouTube Trueview Desktop", "TV spot within ad break & YouTube PreRoll Desktop", "TV spot within ad break & YouTube TrueView Mobile", "TV spot within ad break & YouTube Bumper Mobile", "TV spot within ad break & Facebook Post Video", "")
    YTFB = c("YouTube Trueview Desktop & Facebook Post Video", "YouTube PreRoll Desktop & Facebook Post Video", "YouTube TrueView Mobile & Facebook Post Video", "YouTube Bumper Mobile & Facebook Post Video", "")

    TV <- TV[!TV %in% input$groupa & !TV %in% input$groupb & !TV %in% input$groupc & !TV %in% input$groupd & !TV %in% input$groupf]
    YT <- YT[!YT %in% input$groupa & !YT %in% input$groupb & !YT %in% input$groupc & !YT %in% input$groupd & !YT %in% input$groupf]
    FB <- FB[!FB %in% input$groupa & !FB %in% input$groupb & !FB %in% input$groupc & !FB %in% input$groupd & !FB %in% input$groupf]
    TVYTFB <- TVYTFB[!TVYTFB %in% input$groupa & !TVYTFB %in% input$groupb & !TVYTFB %in% input$groupc & !FB %in% input$groupd & !FB %in% input$groupf]
    KG <- KG[!KG %in% input$groupa & !KG %in% input$groupb & !KG %in% input$groupc & !KG %in% input$groupd & !KG %in% input$groupf]
    YTFB <- YTFB[!YTFB %in% input$groupa & !YTFB %in% input$groupb & !YTFB %in% input$groupc & !YTFB %in% input$groupd & !YTFB %in% input$groupf]

    selected <- input$groupe
    selectizeInput("groupe", "Experimental group E", list(
        "control group" = KG,
        "TV" = TV,
        "YouTube" = YT,
        "Facebook" = FB,
        "TV & (YouTube | Facebook)" = TVYTFB,
        "YouTube & Facebook" = YTFB
    ), multiple = TRUE, selected = selected, options = list(placeholder = "Please choose at least one channel", plugins = list('remove_button')))
} else {
    shinyjs::reset(input$groupe)
    shinyjs::hide(input$groupe)
}
})

这是仅创建元素之一的代码。当input$numbergroups为4或更少时,将不会呈现此输入。但是,当用户选择一个或多个元素并将input$numbersgroups更改为3时,当我再次将input $ numbergroups更改为5时,仍然选择了所选择的元素。 ObserveEventobserveupdateSelectizeInput结合使用仅在渲染用户界面且重置输入的Shinyjs代码也不起作用时有效。

是否有一种方法可以在每次未呈现输入字段时动态重置输入字段?

谢谢:-)

1 个答案:

答案 0 :(得分:0)

我使用了一种解决方法:

我删除了条件:

   if(input$numbergroups > 4) {
    ...
}

然后,我读出input$numbergroups的值,并使用计划Javascript动态显示和隐藏它们:-)