Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date handling #6

Open
leeper opened this issue Jun 1, 2017 · 1 comment
Open

Date handling #6

leeper opened this issue Jun 1, 2017 · 1 comment
Labels

Comments

@leeper
Copy link
Owner

leeper commented Jun 1, 2017

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 )  
    })
})
@leeper leeper added the bug label Jun 1, 2017
@jarjuk
Copy link

jarjuk commented Jun 1, 2017

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

leeper added a commit that referenced this issue Jun 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants