0

I would have simply put this as a comment to the post Dynamically creating tabs with plots in shiny without re-creating existing tabs, but I'm newbie and wasn't allowed. I have modified the server function from the original post for demonstrating how to rendering tables within dynamic tabs.

However I have ran into a perplexing problem with how the tables are rendered, within the observerEvent call:

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

When run in a window from Rstudio (version: 1.0.153) and when the if clause in the observerEvent call is FALSE everything works fine. That is, the first dynamic tab contains the cars data set and the 2nd dynamic tab contains the iris data set, but when the if clause is TRUE both dynamics tabs contain the iris data set. Functionally I can't see why this so???. Both conditions are essentially doing the same thing -- no?

Further if the app is launched in firefox under the TRUE condition no table shows up within either dynamic tab, but under the FALSE condition both tables show up as expected??

Can anyone suggest why there is difference between the rendering of the tables within a for loop condition as given here?

Below is the code for the server:

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

}

Ed Breen
  • 11
  • 2

1 Answers1

0

The solution is:

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

Now both branches from the if caluse give the same result :)

Ed Breen
  • 11
  • 2