leeper / csvy

Import and Export CSV Data With a YAML Metadata Header
57 stars 3 forks source link

Date handling #6

Open leeper opened 7 years ago

leeper commented 7 years ago

From an email:

I could not make Date datatype to work as expected. After modifying line 80 on in "R/read_csvy.R"

    ## attributes(out[, i]) <- fields_this_col
    attributes(out[, i]) <- append( attributes(out[,i]), fields_this_col)

Date -datatype works as I would expect.

Documentation on https://stat.ethz.ch/R-manual/R-devel/library/base/html/attributes.html says

"Assigning attributes first removes all attributes, then sets any dim attribute and then the remaining attributes in the order given: this ensures that setting a dim attribute always precedes the dimnames attribute."

Is it possible that the original version removes class -attribute, which never gets set anymore?

I have made small test, which fails when running it using the version downloaded with the command "git clone https://github.com/leeper/csvy", and passes after the modification. The test is attached below.

test_that( "data-types", {
    context("CSVY imports/exports/data-types")
    test_that( "csvy_write/csvy_read",  {
        attrKey <- "myatt"
        attrValue <- "attribute value"
        df <- data.frame(
            d =c("1990-01-01", "1990-01-02"),
            n =c(1,2.5),
            i =c(1L,2L)            
        )
        df$d <- as.Date( df$d )
        attr( df$d, attrKey ) <- attrValue

        ## Start df
        expect_is( df, "data.frame" )
        expect_is( df$d, "Date" )
        expect_is( df$n, "numeric" )
        expect_is( df$i, "integer" )        
        expect_equal( attr( df$d, attrKey), attrValue )

        ## write/read
        filePath = file.path( "tmp", "csvy-data-types.csvy" )
        ret <- write_csvy( df, filePath  )
        df2 <- read_csvy( filePath )        

        expect_is( df2, "data.frame" )
        expect_is( df2$n, "numeric" )
        expect_is( df2$i, "integer" )        
        expect_is( df2$d, "Date" )
        expect_equal( attr( df2$d, attrKey ), attrValue )  
    })
})
jarjuk commented 7 years ago

I have uploaded a patched version to github https://github.com/jarjuk/csvy

Please refer to the updated test in https://github.com/jarjuk/csvy/blob/patch-colClasses/tests/testthat/test_csvy.R, which checks that attributes are retained (and also data is read correctly back after an import - the earlier version fails here :)

Please let me know, if I can make the patch better available for the trunk. Disclaimer: I am NOT an R -expert - just learning, and I may have misunderstanding somewhere.

Cheers, Jukka