使用Shiny with for循环动态创建带有绘图的选项卡

时间:2017-08-07 01:59:03

标签: r dynamic tabs shiny

我会简单地将此作为对Dynamically creating tabs with plots in shiny without re-creating existing tabs帖子的评论,但我是新手并且不允许。我已经修改了原始帖子中的服务器功能,以演示如何在动态选项卡中呈现表格。

然而,我在observerEvent调用中遇到了如何呈现表的一个令人困惑的问题:

    observeEvent(input$goCreate, {
    sheets = c("s1", "s2")
    data = list(cars, iris)
    newTabPanels = list()
    if(F) {
        for(k in 1:2) {
            newTabPanels[[k]] = tabPanel(sheets[k],
                                         renderTable(data[[k]]))
        }
    } else {
        newTabPanels[[1]] = tabPanel(sheets[1],
                                     renderTable(data[[1]]))
        newTabPanels[[2]] = tabPanel(sheets[2],
                                     renderTable(data[[2]]))
    }
    addTabToTabset(newTabPanels, "mainTabset")
})

当在Rstudio(版本:1.0.153)的窗口中运行时,当observerEvent调用中的 if 子句为FALSE时,一切正常。也就是说,第一个动态选项卡包含汽车数据集,第二个动态选项卡包含虹膜数据集,但当 if 子句为TRUE时,两个动态选项卡都包含虹膜数据集。在功能上我不明白为什么这样???。这两个条件基本上都在做同样的事情 - 不是吗?

此外,如果应用程序在TRUE条件下在firefox中启动,则在任一动态选项卡中都不显示任何表,但在FALSE条件下,两个表都按预期显示?

有人可以建议为什么在这里给出的 for 循环条件下表格的渲染之间存在差异吗?

以下是服务器的代码:

server <- function(input, output, session){
# Important! : creationPool should be hidden to avoid elements flashing before they are moved.
#              But hidden elements are ignored by shiny, unless this option below is set.
output$creationPool <- renderUI({})
outputOptions(output, "creationPool", suspendWhenHidden = FALSE)
# End Important
# Important! : This is the make-easy wrapper for adding new tabPanels.
addTabToTabset <- function(Panels, tabsetName){
    titles <- lapply(Panels, function(Panel){return(Panel$attribs$title)})
    Panels <- lapply(Panels, function(Panel){Panel$attribs$title <- NULL; return(Panel)})

    output$creationPool <- renderUI({Panels})
    session$sendCustomMessage(type = "addTabToTabset",
                              message = list(titles = titles,
                                             tabsetName = tabsetName))
}
# End Important 
observeEvent(input$goCreate, {
    sheets = c("s1", "s2")
    data = list(cars, iris)
    newTabPanels = list()
    if(F) {
        for(k in 1:2) {
            newTabPanels[[k]] = tabPanel(sheets[k],
                                         renderTable(data[[k]]))
        }
    } else {
        newTabPanels[[1]] = tabPanel(sheets[1],
                                     renderTable(data[[1]]))
        newTabPanels[[2]] = tabPanel(sheets[2],
                                     renderTable(data[[2]]))
    }
    addTabToTabset(newTabPanels, "mainTabset")
})

}

1 个答案:

答案 0 :(得分:0)

解决方案是:

    observeEvent(input$goCreate, {
    sheets = c("s1", "s2")
    data = list(cars, iris)
    newTabPanels = list()
    if(T) {
            newTabPanels = lapply(1:2, function(k){tabPanel(sheets[k],
                                         renderTable(data[[k]]))})
    } else {
        newTabPanels[[1]] = tabPanel(sheets[1],
                                     renderTable(data[[1]]))
        newTabPanels[[2]] = tabPanel(sheets[2],
                                     renderTable(data[[2]]))
    }
    addTabToTabset(newTabPanels, "mainTabset")
})

现在来自 if caluse的两个分支给出相同的结果:)