jvalenti
jvalenti

Reputation: 640

r shiny - display data frame after uploading

I am trying to build a record linkage app in R shiny. I have been using R for a while but am new to shiny. My problem is I am struggling to figure out how to display the file I upload via fileInput. My code is below.

library(shiny)
library(shinydashboard)

ui

ui <- dashboardPage(
  dashboardHeader(title = "Record Linkage App"),
  dashboardSidebar(
    sidebarMenu(
      ## Tab 1 -- Specify Task
      menuItem("Select Task And Upload Files", tabName = "task", icon = 
    icon("file-text-o")),
  ## Tab 2 -- View Raw Data Files
  menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
  ## Tab 3 -- View Processed Data Files
  menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
  ## Tab 4 -- Select Training Set
  menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
  ## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
  menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
  ## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
  ## Want to be able to add checkboxes to select rows for inclusion in deletion later on
  menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))

)), # close dashboard sidebar

  #### Dashboard Body starts here

  dashboardBody(
    tabItems(
  ### Specify Task & Upload Files Tab
  tabItem(tabName = "task",
          radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
          fileInput("selection", "Upload Files:", multiple = T, 
                    accept = c(".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
          helpText(paste("Please upload a file.  Supported file types are:  .txt, .csv and .xls"))

          ), # close first tabItem

  tabItem(tabName = "raw",
          helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
          mainPanel(
            tableOutput("contents")

          )

) # close tabItems

  ) # close dashboardBody
) #close dashboardpage

)

server

server <- function(input, output, session) {
   output$contents <- renderTable({
      req(input$file1)
      read.csv(input$file1$datapath)

  })
}

shinyApp(ui, server)

I would like to be able to display the table in the tab "raw".

My question is: 1. If I just want to display the table does it need to be reactive?
2. How does input$file1$datapath enter into renderTable?

Any suggestions or advice would be appreciated. Thanks.

Upvotes: 0

Views: 1800

Answers (1)

r2evans
r2evans

Reputation: 160407

I think you're taking the documentation a little too literally: you should be referencing input$selection, not input$file1. I modified your server component to this, and it worked:

server <- function(input, output, session) {
  output$contents <- renderTable({
    req(input$selection)
    read.csv(input$selection$datapath)
  })
}

BTW: though not harming things, you are using mainPanel, which is usually used in a non-dashboard app along with shiny::sidebarLayout. Since you're using shinydashboard, though, it is unnecessary, and you can reduce the last tabItem to just

      tabItem(tabName = "raw",
              helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
              tableOutput("contents")
              )

Upvotes: 2

Related Questions