1

I'm triyng to build a simple shiny app and I'm stuck with a date transformation. The main purpose I want is to choose the file and choose the date format.

For now I'm working with one date format in an csv,datafile example that looks like a "%d-%m-%Y" string. I tried several ways to transform into a proper date and the as.date function seems to do the trick.

But when I displayed the transformed date in a new data.frame the results seems to be numbers. Also I've got the following errors:

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

The server.R code is:

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()))
  })
}) 

The UI code is:

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')
      )))) 

and the global.R is:

library("shiny")
library(plyr)

Below there is a sample of the data I working on:

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=",")

My locale is:

> 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"

Any suggestion? Thanks in advance Oscar

Oscar Benitez
  • 255
  • 3
  • 13

0 Answers0