如何在闪亮的环境中显示上传的csv的内容

时间:2017-07-12 06:32:37

标签: shiny

我的ui.R

library(shiny)
library(stats)
library(caret)

shinyUI(fluidPage(
   titlePanel("Predicting Resources for Vessel"),
   title = "Resource Prediction",

   sidebarLayout(
    sidebarPanel(
        fileInput("file1", "Choose a Import BAPLE(.CSV) file to upload:",
                  accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),

        fileInput("file2", "Choose a Export BAPLE(.csv) file to upload:",
                  accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),

        fileInput("file3", "Choose a Import/Export containers yard location(.CSV) file to upload:",
                  accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),

        tags$hr(),
        h4("Manual Input:"),
        numericInput("Restow_40","Total Restows for 40ft Container:", 0, min = 0, max = 999999, step = 1),
        textInput("Berth","Vessel Berth Location (CB3/CB4)"),
        actionButton("submit", "Submit")

        ),
    mainPanel(
        tabsetPanel(
          tabPanel("Raw Data", dataTableOutput("data")),
          tabPanel("Output", verbatimTextOutput("pred_output"))

        )
  )
    )
))

这是我的server.r文件

library(shiny)
library(stats)
#library(caret)
library(mlr)
library(data.table)

shinyServer(function(input, output) {

 ######################### Reading the required files ###################################

  import_baple <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    read.csv(inFile$datapath)
})

  export_baple <- reactive({
    inFile <- input$file2
    if (is.null(inFile)) return(NULL)
    read.csv(inFile$datapath)
  })

  import_export_yard <- reactive({
    inFile <- input$file3
    if (is.null(inFile)) return(NULL)
    read.csv(inFile$datapath)
  })

  output$data <- renderDataTable({
    import_baple()
  })

  output$data <- renderDataTable({
    export_baple()
  })

  output$data <- renderDataTable({
    import_export_yard()
  })
})

我希望上传的所有三个文件都显示在Raw Data标签中。当我上传前两个文件时,Raw Tab中没有显示任何内容,但是当我上传第三个文件时,内容会显示在选项卡中。我没有得到我做错的地方。

1 个答案:

答案 0 :(得分:2)

每个输入/输出元素都需要一个唯一的标识符,否则Shiny不知道具有给定标识符的哪个元素要使用。所以你在哪里:

tabPanel("Raw Data", dataTableOutput("data")) 

在您的用户界面中:

output$data <- renderDataTable({
  import_baple()
})

output$data <- renderDataTable({
  export_baple()
})

output$data <- renderDataTable({
  import_export_yard()
})

在您的服务器中,您实际需要的更像是:

# UI
tabPanel("Raw Data", 
    dataTableOutput("import_baple_data"),
    dataTableOutput("explort_baple_data"),
    dataTableOutput("import_export_data")
)

# Server
output$import_baple_data <- renderDataTable({
  import_baple()
})

output$export_baple_data <- renderDataTable({
  export_baple()
})

output$import_export_data <- renderDataTable({
  import_export_yard()
})