fileInput没有返回任何数据帧

时间:2017-10-18 07:06:28

标签: r shiny

我试图输入两个文件,执行一些转换并返回数据表。如果我硬编码文件路径,它可以正常工作,但如果我使用fileInput则不行。我使用obersveEvent因为选项和操作比我在可重现的示例中显示的要复杂得多。 有关代码可能出错的任何建议吗?

library(shiny)
library(shinydashboard)
library(data.table)
library(DT)

##
ui <- shinyUI(pageWithSidebar(
  headerPanel("Test app"),
  sidebarPanel(
      fileInput("Env_db1", "Choose CSV File for DB1",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv") 
      ),

      fileInput("Env_db2", "Choose CSV File for DB2",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv") 
      )
    ,
    actionButton(
      inputId = "submit_loc",
      label = "Submit")
  ),

  mainPanel(
    DT::dataTableOutput("table"))

  )
)


##
server <- shinyServer(function(input, output) {
  observeEvent(
    eventExpr = input$submit_loc,
    handlerExpr = 
    {
        req(input$file1)
        inFile1 <- input$file1
        if(is.null(input$file1))     return(NULL) 
        Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")


        req(input$file2)
        inFile2 <- input$file2
        if(is.null(input$file2))     return(NULL) 
        Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")


      Difference <-rbind(Env_1,Env_2)

      output$table = DT::renderDataTable(server = TRUE,{
        DT::datatable(Difference,
                      extensions=c("Buttons",'Scroller'),
                      options = list(dom = 'Bfrtip',
                                     buttons = c('copy', 'csv', 
                                                 'excel', 'pdf', 
                                                 'print'),
                                     scrollY = 500,
                                     scroller = TRUE)
        )
      })
    })
})  
##
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

fileInput变量的名称错误,而不是input$file1,而input$file2应该是input$Env_db1input$Env_db2。如下所示替换变量,您的代码可以正常工作。

library(shiny)
library(shinydashboard)
library(data.table)
library(DT)

##
ui <- shinyUI(pageWithSidebar(
  headerPanel("DBC Comparison"),
  sidebarPanel(
    fileInput("Env_db1", "Choose CSV File for DB1",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv") 
    ),

    fileInput("Env_db2", "Choose CSV File for DB2",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv") 
    )
    ,
    actionButton(
      inputId = "submit_loc",
      label = "Submit")
  ),

  mainPanel(
    DT::dataTableOutput("table"))

)
)


##
server <- shinyServer(function(input, output) {
  observeEvent(
    eventExpr = input$submit_loc,
    handlerExpr = 
    {
      req(input$Env_db1)
      inFile1 <- input$Env_db1
      if(is.null(input$Env_db1))     return(NULL) 
      Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")


      req(input$Env_db2)
      inFile2 <- input$Env_db2
      if(is.null(input$Env_db2))     return(NULL) 
      Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")


      Difference <-rbind(Env_1,Env_2)

      output$table = DT::renderDataTable(server = TRUE,{
        DT::datatable(Difference,
                      extensions=c("Buttons",'Scroller'),
                      options = list(dom = 'Bfrtip',
                                     buttons = c('copy', 'csv', 
                                                 'excel', 'pdf', 
                                                 'print'),
                                     scrollY = 500,
                                     scroller = TRUE)
        )
      })
    })
})  
##
shinyApp(ui = ui, server = server)

希望它有所帮助!

答案 1 :(得分:1)

您错误地在服务器中使用了file1。将您正在阅读的输入文件对象称为Env_db1

更新的代码:

  library(shiny)
  library(shinydashboard)
  library(data.table)
  library(DT)

  ##
  ui <- shinyUI(pageWithSidebar(
    headerPanel("DBC Comparison"),
    sidebarPanel(
      fileInput("Env_db1", "Choose CSV File for DB1",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv") 
      ),

      fileInput("Env_db2", "Choose CSV File for DB2",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv") 
      )
      ,
      actionButton(
        inputId = "submit_loc",
        label = "Submit")
    ),

    mainPanel(
      DT::dataTableOutput("table"))

  )
  )


  ##
  server <- shinyServer(function(input, output) {
    observeEvent(
      eventExpr = input$submit_loc,
      handlerExpr = 
      {
        req(input$Env_db1)
        inFile1 <- input$Env_db1
        if(is.null(input$Env_db1))     return(NULL) 
        Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")


        req(input$Env_db2)
        inFile2 <- input$Env_db2
        if(is.null(input$Env_db2))     return(NULL) 
        Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")


        Difference <-rbind(Env_1,Env_2)

        output$table = DT::renderDataTable(server = TRUE,{
          DT::datatable(Difference,
                        extensions=c("Buttons",'Scroller'),
                        options = list(dom = 'Bfrtip',
                                       buttons = c('copy', 'csv', 
                                                   'excel', 'pdf', 
                                                   'print'),
                                       scrollY = 500,
                                       scroller = TRUE)
          )
        })
      })
  })  
  ##
  shinyApp(ui = ui, server = server)