闪亮的反应性日期格式

时间:2015-01-29 21:16:10

标签: r format shiny as.date

我正在构建一个简单的闪亮应用程序并且我坚持使用日期转换。我想要的主要目的是选择文件并选择日期格式。

现在我在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()))
  })
}) 

UI代码是:

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"

有什么建议吗? 提前致谢 奥斯卡

0 个答案:

没有答案
相关问题