mhannani
mhannani

Reputation: 502

How to render a tableoutput in another tab in Shiny?

I'm building a simple website using Shiny,that allow users to uplaod a csv,xls ... file within Getting the data tab and view it in another tab named Viewing the data and then plot that data in another tab visualizing the data . for instance i want just to render a table based on the data picked ,

Here's a snippet of what i tried :

ui :

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel("APRIORI INPUTS",id="panelTitle"),
    mainPanel(
      tabsetPanel(
        tabPanel(title = "Getting the data",icon = icon("database"), 
                   tags$div(id="uploadFiles",
                            fileInput("file1", "Choose CSV File",
                                      multiple = TRUE,
                                      accept = c("text/csv",
                                                 "text/comma-separated-values,text/plain",
                                                 ".csv"))
                   )

                 ), 
        tabPanel(title = "Viewing the data",icon = icon("eye"),
                 tableOutput("Viewing_the_data")), 
        tabPanel(title = "visualizing the data",icon = icon("chart-bar"),
                 tableOutput("visualizing_the_data"))
      )
    ),
  )
)

for the server logic :

server :

server <- function(input, output){
  output$Viewing_the_data <- renderTable({
    req(input$uploadFiles)
    read.csv(input$selection$datapath)

  })
}
shinyApp(ui = ui, server = server)

I tried that but doesn't work ...

PS : i tried that with shinydashboard and it works perfectly as that : r shiny - display data frame after uploading

Any suggestions or advice would be appreciated. Thanks.

Upvotes: 0

Views: 1039

Answers (1)

Valeri Voev
Valeri Voev

Reputation: 2242

I've tried to adapt the example I mentioned with your app, removing some of the complexity to make it a simpler app but still have the tabbed structure and it works when I run it. I can choose the file in one tab, select how many rows to show in the sidebar and show the data in another tab:

library(shiny)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel("APRIORI INPUTS",id="panelTitle",
                     numericInput("n", "Rows", value = 5, min = 1, step = 1)),
        mainPanel(
            tabsetPanel(
                tabPanel(title = "Getting the data", fileInput("file", NULL, accept = c(".csv", ".tsv")), icon = icon("database")), 
                tabPanel(title = "Viewing the data", tableOutput("head"), icon = icon("eye")))
        )
    )
)

server <- function(input, output, session) {
    data <- reactive({
        req(input$file)

        ext <- tools::file_ext(input$file$name)
        switch(ext,
               csv = vroom::vroom(input$file$datapath, delim = ","),
               tsv = vroom::vroom(input$file$datapath, delim = "\t"),
               validate("Invalid file; Please upload a .csv or .tsv file")
        )
    })

    output$head <- renderTable({
        head(data(), input$n)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions