我正在尝试构建一个简单的闪亮应用程序,但我被日期转换困住了。我想要的主要目的是选择文件并选择日期格式。
现在,我正在使用 csv、数据文件示例中的一种日期格式,该示例看起来像“%d-%m-%Y”字符串。我尝试了几种方法来转换为正确的日期,并且 as.date 函数似乎可以解决问题。
但是当我在新的 data.frame 中显示转换后的日期时,结果似乎是数字。我也有以下错误:
Error in as.vector(x) : argument "x" is missing, with no default
Warning in matrix(align.tmp[(2 - pos):(ncol(x) + 1)], nrow = nrow(x), ncol = ncol(x) + : data length exceeds size of matrix
Warning in formatC(x = numeric(0), format = "f", digits = 2, decimal.mark = ".") : class of 'x' was discarded
Warning in formatC(x = c(9862, 9879, 10075, 10207, 9862, 9874, 9862, 9862, :
class of 'x' was discarded
server.R 代码是:
shinyServer(function(input, output){
datos <- reactive ({ #get the file
inFile <- input$file1
if (is.null(inFile))
return(NULL)
return( read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote))
})
output$filetable <- renderTable({ #shows the raw data
head(datos(),n=5)
})
fecha<-reactive ({ #format date
if(is.null(datos))
return(NULL)
return(as.Date(as.character(datos()$Date), "%d-%m-%Y" ))
})
datos2<-reactive ({ #reasembling the data file
if(is.null(datos))
return(NULL)
return(data.frame(datos()$ID,fecha(),datos()$Spend))
})
#now ddply
datos3<-reactive ({
if(is.null(datos))
return(NULL)
return(ddply(datos2(), c("datos...ID", "fecha.."), summarize,
n.Spend = length(as.numeric(gsub(",",".",(datos...Spend)))),
s.Spend = sum(as.numeric(gsub(",",".",(datos...Spend))))))
})
output$datosag<-renderTable({
head(as.data.frame(datos3()))
})
})
用户界面代码是:
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Seleccione un archivo .CSV',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
br(),
checkboxInput('header', 'Encabezado', TRUE),
radioButtons('sep', 'Separador',
c('Coma'=',',
'Punto y coma'=';',
'Tab'='\t'),
','),
radioButtons('quote', 'Comillas',
c('Ninguno'='',
'Comillas Dobles'='"',
'Comillas Simples'="'"),
'"')),
mainPanel(
tableOutput('filetable'),
br(),
tableOutput('datosag')
))))
而 global.R 是:
library("shiny")
library(plyr)
下面是我处理的数据示例:
ID<-c("1","1","1","1","1","2","2","3","3")
Date<-c("18-01-1997","01-01-1997","02-08-1997","12-12-1997","12-12-1997","01-01-1997","13-01-1997","01-01-1997","01-01-1997")
Spend <-c("29,73","29,33","14,96","26,48","50,46","63,34","11,77","6,79","8,79")
df<-data.frame(ID,Date,Spend)
write.csv(df, "c:/df.csv", sep=",")
我的语言环境是:
> Sys.getlocale()
[1] "LC_COLLATE=Spanish_Chile.1252;LC_CTYPE=Spanish_Chile.1252;LC_MONETARY=Spanish_Chile.1252;LC_NUMERIC=C;LC_TIME=Spanish_Chile.1252"
有什么建议吗?提前感谢奥斯卡