RShiny DataTableOutput overwritting views

Viewed 17

I'm writing a small Application to upload multiple dataframes and visualizing them in different tabs. In order to visualize the .csv files given by the user, I'm using one DataTableOutput per Tab as you will see in the code.

The problem that I have not been able to solve is: When I click the 'Refresh tables' button, I should get the a different DataTable render on each tab, but instead I get the last uploaded data in all tabs.

Attached I sent a dummy version of the app. It lacks lots of verifications and messages that I have in the original app, but depics the problem that I talk about.

Thank you very much for your help.

library(shiny)
library(DT)

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
          numericInput(inputId = 'numsel',
                      label = 'Total tabs',
                      value = 0,
                      min=0
                      ),
          uiOutput( outputId = 'TextInputs' ),
          actionButton(inputId = 'RenderTabsButton',
                       label = 'Render Tabs'
                       ),
          actionButton(inputId = 'RefreshTablesButton',
                       label = 'Refresh Tables'
          )
        ),

        mainPanel(
          h3('Make sure selector is at least 2'),
          tabsetPanel( id= 'myTabsetpanel')
        )
    )
)


server <- function(input, output) {
  observeEvent( input$numsel, {
    
    numsel <- isolate(input$numsel)
    
    text_inputs <- NULL 
    
    if(numsel>0){
      text_inputs <- lapply(1:numsel, function(x){
          textInput(inputId =  paste( 'TextInput_', x, sep = '' ),
                    label = 'Tab Name'
                    )
      })
      output$TextInputs <- renderUI(text_inputs)
    }
    
  } )
  
  
  observeEvent( input$RenderTabsButton, {
    
    numsel <- isolate(input$numsel)
    
    myTabs <- list()
    
    for( index in 1: numsel){
      
      myTabs[[index]] <- tabPanel(
        title = input[[ paste( 'TextInput_', index, sep = '' ) ]],
        fileInput(inputId = paste('FileInput_', index , sep = ''), label = '.csv file'),
        dataTableOutput( outputId = paste('DataTableOutput_', index , sep = ''))
      )
      
      appendTab( inputId = 'myTabsetpanel' , tab = myTabs[[index]] ,select = TRUE )
    }
    
  } )
  
  observeEvent( input$RefreshTablesButton, {
    
    numsel <- isolate(input$numsel)
    
    myData <-list()
    
    for( index in 1: numsel){
      datapath <- input[[ paste('FileInput_', index , sep = '') ]]$datapath
      myData[[index]] <- read.csv(datapath)
      output[[ paste('DataTableOutput_', index , sep = '') ]] <- renderDataTable( myData[[index]] )
    }
    
  } )

}


shinyApp(ui = ui, server = server)
0 Answers
Related