在 R shiny 的反应环境中使用 read_xlsx 函数更改日期格式

Date format changes using read_xlsx function within reactive environment in R shiny

我使用 R 创建了以下数据框

datelist<-seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 1)

df<-as.data.frame(datelist)
df$New<-1:nrow(df)

接下来我创建了一个 UI 和使用 shiny 读取 table

的服务器
library(shiny)
UI<-fluidPage(fileInput("file", "Browse",
                    accept = c("text/csv",
                               "text/comma-separated-values,text/plain",
                               ".csv")),dateInput(inputId = "Cutoffdate", 
    label = "Cut Off Date"),
          tableOutput(outputId = "Table1"))

Server<-function(input, output, session){

   options(shiny.maxRequestSize=100*1024^2) 

   output$Table1<-renderTable({

   infile <- input$file
   if (is.null(infile)) {
   # User has not uploaded a file yet
   return(NULL)
   }

    df<-readxl::read_xlsx(input$file$datapath)
   })

shinyApp(UI, Server)

table 输出给出了一系列数字,而不是年月日。我试过使用 lubridate 中的 ymd,但这没有帮助。有没有办法在 R

的反应环境中将数据哄骗到 ymd

由于您的 fileInput 仅接受 .csv 格式,我建议使用 read.table 读取数据。您可以使用 read.table 函数中的 colClasses 参数指定列格式。

df <- read.table(input$file$datapath, header = TRUE, sep = ","
                 colClasses = c("Date", "factor" # ... etc
                 ))
    )