r Shiny-上传后显示数据框

时间:2019-04-05 21:16:38

标签: r shiny

我正在尝试在R Shiny中建立一个唱片链接应用程序。我已经使用R已有一段时间了,但是对发亮还是陌生的。我的问题是我正在努力弄清楚如何显示通过fileInput上传的文件。我的代码在下面。

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 <- function(input, output, session) {
   output$contents <- renderTable({
      req(input$file1)
      read.csv(input$file1$datapath)

  })
}

shinyApp(ui, server)

我希望能够在“原始”标签中显示表格。

我的问题是:1.如果我只想显示表格,它是否需要为reactive
2. input$file1$datapath如何进入renderTable

任何建议都将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

我认为您从字面上看有点过时:您应该引用input$selection,而不是input$file1。我将您的服务器组件修改为此,并且可以正常工作:

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

顺便说一句:虽然没有什么害处,但是您正在使用mainPanel,它通常与shiny::sidebarLayout一起用于非仪表板应用程序中。不过,由于您使用的是shinydashboard,所以没有必要,而且您可以将最后一个tabItem减少为

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