r - 在 R 闪亮的反应环境中使用 read_xlsx 函数更改日期格式
问题描述
我使用 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 和服务器使用闪亮来读取表格
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)
表格输出给出了一系列数字,而不是年份月份日期。我曾尝试使用 lubridate 的 ymd 但这没有帮助。有没有办法在 R 的反应环境中将数据哄骗到 ymd
解决方案
由于您fileInput
只接受 .csv 格式,我建议您使用read.table
来读取数据。colClasses
您可以使用函数内的参数指定列格式read.table
。
df <- read.table(input$file$datapath, header = TRUE, sep = ","
colClasses = c("Date", "factor" # ... etc
))
)