R Shiny-查找创建上传文件的时间

时间:2019-03-25 03:57:21

标签: r file-upload shiny

我试图找到在Shiny中上传的csv文件的创建日期。

在下面的示例中,我尝试了'file.info(userFile()$ datapath)$ ctime'。这将返回包含已上传数据的临时文件的创建日期,而不是文件本身的创建日期:

file.info(“ example.csv”)$ ctime

[1]“ 2019-01-08 18:34:47 PST”

这是一个可重复的示例:

library("shiny")

# Module UI function
csvFileInput <- function(id, label = "CSV file") {
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(
    fileInput(ns("file"), label),
    checkboxInput(ns("heading"), "Has heading"),
    selectInput(
      ns("quote"),
      "Quote",
      c(
        "None" = "",
        "Double quote" = "\"",
        "Single quote" = "'"
      )
    )
  )
}

# Module server function
csvFile <- function(input,
                    output,
                    session,
                    stringsAsFactors) {
  # The selected file, if any
  userFile <- reactive({
    # If no file is selected, don't do anything
    validate(need(input$file, message = FALSE))
    input$file
  })

  # The user's data, parsed into a data frame
  dfUpload <- reactive({
    read.csv(
      userFile()$datapath,
      header = input$heading,
      quote = input$quote,
      stringsAsFactors = stringsAsFactors
    )
  })

  # input$file data frame
  dfInfo <- reactive({
    input$file
  })

  # userfile() creation date
  txtFileDate <- reactive({
    msg1 <- sprintf("File Date: %s",
                    as.Date(file.info(userFile()$datapath)$ctime))
    cat(msg1, "\n")
  })

  # We can run observers in here if we want to
  observe({
    msg <- sprintf("File %s was uploaded", userFile()$name)
    cat(msg, "\n")
  })

  # Return list of reactive outputs
  return(list(
    df.Id01 = reactive({
      dfUpload()[1,]
    }),
    df.Id02 = reactive({
      dfInfo()
    }),
    txt.Id01 = reactive({
      txtFileDate()
    })
  ))
}

ui <- fluidPage(sidebarLayout(
  sidebarPanel(csvFileInput("datafile", "User data (.csv format)")),
  mainPanel(
    tableOutput("table01"),
    tableOutput("table02"),
    verbatimTextOutput("text01")
  )
))

server <- function(input, output, session) {
  lstOut <- callModule(csvFile, "datafile",
                       stringsAsFactors = FALSE)

  output$table01 <- renderTable({
    lstOut$df.Id01()
  })

  output$table02 <- renderTable({
    lstOut$df.Id02()
  })

  output$text01 <- renderPrint({
    lstOut$txt.Id01()
  })

}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案