R Shiny当一个修饰另一个时,我如何确定最近修改的反应表达式?

时间:2017-04-24 23:19:55

标签: r user-interface shiny reactive-programming

基本上我有一个多选输入的层次结构,称它们为a,b和c,大小为(a)>尺寸(b)>尺寸(C)。 b的每个单位属于a的单位,c的每个单位属于b的单位。当你从a中删除某些内容时,它会从b中删除相应的内容,从而从c中删除相应的内容。

我需要做的是确定用户上次修改的多选输入。我在这里跟踪了这个结果Selecting most recently changed reactive expressions in Shiny但它不起作用,因为如果我要修改a,它会修改b然后修改c,结果将是程序认为用户修改了c。

我的问题是,有没有办法区分用户输入和常规更新通话?

第二个问题可能是提前确定反应式表达式将解决的顺序吗?我已经注意到,例如,如果我从a或b中删除了很多值,它将一次解析这些值的删除,然后继续从较低层次结构中删除值。我以前期望它会交替(从b中删除,从c中删除相应的,从b中删除,从c中删除相应的等等)但事实并非如此。

这是一个例子,你可以运行代码,你会看到当你编辑一个或两个选择时它会快速闪烁“一个”或“两个”但它总是会重置为“三个”。例外情况是当您删除列表中的最后一项时,由于某种原因不会更新其下面的级别。

library("shiny")
one = vector(mode="list", length = 4)
names(one) = c("a","b","c","d")
two = vector(mode="list", length = 8)
names(two) = c("e","f","g","h","i","j","k","l")
three = vector(mode="list", length = 16)
three = c("m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","ab")
one[["a"]] = c("e","f")
one[["b"]] = c("g","h")
one[["c"]] = c("i","j")
one[["d"]] = c("k","l")
two[["e"]] = c("m","n")
two[["f"]] = c("o","p")
two[["g"]] = c("q","r")
two[["h"]] = c("s","t")
two[["i"]] = c("u","v")
two[["j"]] = c("w","x")
two[["k"]] = c("y","z")
two[["l"]] = c("aa", "ab")
ui = fluidPage(
    titlePanel("Example"),
    sidebarLayout(
        sidebarPanel(
            selectInput("selectOne", label="Select One:", choices=names(one), selected = names(one), multiple=TRUE),
            selectInput("selectTwo", label="Select Two:", choices=names(two), selected = names(two), multiple=TRUE),
            selectInput("selectThree", label="Select Three:", choices=three, multiple=TRUE),
            actionButton("reset",label = "Reset")
        ),
        mainPanel(
            textOutput("listOfThree"),
            textOutput("lastUpdated"),
            textOutput("updated")
        )
    )
)

server = shinyServer(function(input,output,session){

    output$listOfThree = renderText({
        paste("list of three:", paste(input$selectThree, collapse = ", "), sep = " ")
    })
    output$lastUpdated = renderText({
        paste("last update:",rv$lastUpdate, sep = " ")
    })
    rv = reactiveValues(lastUpdate = "four")
    observe({
        oneList = input$selectOne
        rv$lastUpdate = "one"
        twoList = c()
        for (thing in oneList){
            twoList = c(twoList, one[[thing]])
        }
        twoList = sort(twoList)
        updateSelectInput(session, "selectTwo", selected = twoList)

    })
    observe({
        twoList = input$selectTwo
        rv$lastUpdate = "two"
        threeList = c()
        for (thing in twoList){
            threeList = c(threeList, two[[thing]])
        }
        threeList = sort(threeList)
        updateSelectInput(session, "selectThree", selected = threeList)

    })
    observe({
        input$selectThree
        rv$lastUpdate = "three"
    })
    observeEvent(
        input$reset,
        {
            updateSelectInput(session, "selectOne", selected = names(one))
            updateSelectInput(session, "selectTwo", selected = names(two))
            updateSelectInput(session, "selectThree", selected = three)
            rv$updateTwo = FALSE
            rv$updateThree = FALSE
        }
    )
})

shinyApp(ui, server)

0 个答案:

没有答案