【发布时间】:2018-01-14 06:12:09
【问题描述】:
我会简单地将其作为对帖子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)的窗口中运行时,并且当观察者事件调用中的 if 子句为 FALSE 时,一切正常。也就是说,第一个动态选项卡包含汽车数据集,第二个动态选项卡包含 iris 数据集,但是当 if 子句为 TRUE 时,两个动态选项卡都包含 iris 数据集。从功能上讲,我不明白为什么会这样???。这两种情况本质上都在做同样的事情——不是吗?
此外,如果应用程序在 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")
})
}
【问题讨论】: