Skip to content

Commit

Permalink
Merge 4c2edd4 into ebc1335
Browse files Browse the repository at this point in the history
  • Loading branch information
jsams committed Nov 21, 2017
2 parents ebc1335 + 4c2edd4 commit 186d1d6
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/RData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import FileIO: load
export
sexp2julia,
DictoVec,
load # export FileIO.load()
load, # export FileIO.load()
readRDS

include("config.jl")
include("sxtypes.jl")
Expand Down Expand Up @@ -87,4 +88,28 @@ end

load(s::Stream{format"RData"}; kwoptions...) = load(s, kwoptions)

# TODO:
# * maybe throw error instead of warning on conversion?
# * tests
# * load stuff (e.g. FileIO req on detect_rdata)
# * maybe return tuple of (object, attribute_dict) for
# https://github.com/JuliaStats/RData.jl/issues/30
function readRDS(f::AbstractString; kwoptions...)
io = open(f, "r")
try
gzipped = read(io, UInt8) == 0x1F && read(io, UInt8) == 0x8B # check GZip magic number
seekstart(io)
# if compressed, transcode gzipped stream
gzipped && (io = GzipDecompressorStream(io))
ctx = RDAContext(rdaio(io, chomp(readline(io))), kwoptions)
@assert ctx.fmtver == 2 # format version
convert2julia = get(ctx.kwdict,:convert,true)
return convert2julia ? sexp2julia(readitem(ctx)) : readitem(ctx)
catch
rethrow()
finally
close(io)
end
end

end # module
65 changes: 65 additions & 0 deletions test/RDS.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module TestRDS
using Base.Test
using DataFrames
using RData

testdir = dirname(@__FILE__)

@testset "RDS: Reading minimal rds" begin
df = DataFrame(num = [1.1, 2.2])
@test isequal(sexp2julia(readRDS("$testdir/data/minimal.rds",convert=false))["df"], df)
@test isequal(readRDS("$testdir/data/minimal.rds",convert=true)["df"], df)
@test isequal(readRDS("$testdir/data/minimal_ascii.rds")["df"], df)
end

@testset "RDS: Conversion to Julia types" begin
df = DataFrame(num = [1.1, 2.2],
int = Int32[1, 2],
logi = [true, false],
chr = ["ab", "c"],
factor = pool(["ab", "c"]),
cplx = Complex128[1.1+0.5im, 1.0im])
rdf = sexp2julia(readRDS("$testdir/data/types.rds",convert=false))["df"]
@test eltypes(rdf) == eltypes(df)
@test isequal(rdf, df)
rdf_ascii = sexp2julia(readRDS("$testdir/data/types_ascii.rds",convert=false))["df"]
@test eltypes(rdf_ascii) == eltypes(df)
@test isequal(rdf_ascii, df)
end


@testset "RDS: NAs conversion" begin
df = DataFrame(num = [1.1, 2.2],
int = Int32[1, 2],
logi = [true, false],
chr = ["ab", "c"],
factor = pool(["ab", "c"]),
cplx = Complex128[1.1+0.5im, 1.0im])

df[2, :] = NA
append!(df, df[2, :])
df[3, :num] = NaN
df[:, :cplx] = @data [NA, Complex128(1,NaN), NaN]
@test isequal(sexp2julia(readRDS("$testdir/data/NAs.rds",convert=false))["df"], df)
# ASCII format saves NaN as NA
df[3, :num] = NA
df[:, :cplx] = @data [NA, NA, NA]
@test isequal(sexp2julia(readRDS("$testdir/data/NAs_ascii.rds",convert=false))["df"], df)
end

@testset "RDS: Column names conversion" begin
rds_names = names(sexp2julia(readRDS("$testdir/data/names.rds",convert=false))["df"])
expected_names = [:_end, :x!, :x1, :_B_C_, :x, :x_1]
@test rds_names == expected_names
rds_names = names(sexp2julia(readRDS("$testdir/data/names_ascii.rds",convert=false))["df"])
@test rds_names == [:_end, :x!, :x1, :_B_C_, :x, :x_1]
end

@testset "RDS: Reading RDA with complex types (environments, closures etc)" begin
rds_envs = readRDS("$testdir/data/envs.rds",convert=false)
rds_pairlists = readRDS("$testdir/data/pairlists.rds",convert=false)
rds_closures = readRDS("$testdir/data/closures.rds",convert=false)
rds_cmpfuns = readRDS("$testdir/data/cmpfun.rds",convert=false)
end
end

Binary file added test/data/NAs.rds
Binary file not shown.
Binary file added test/data/NAs_ascii.rds
Binary file not shown.
Binary file added test/data/closures.rds
Binary file not shown.
Binary file added test/data/cmpfun.rds
Binary file not shown.
Binary file added test/data/envs.rds
Binary file not shown.
Binary file added test/data/envs_ascii.rds
Binary file not shown.
Binary file added test/data/minimal.rds
Binary file not shown.
Binary file added test/data/minimal_ascii.rds
Binary file not shown.
Binary file added test/data/names.rds
Binary file not shown.
Binary file added test/data/names_ascii.rds
Binary file not shown.
Binary file added test/data/pairlists.rds
Binary file not shown.
Binary file added test/data/types.rds
Binary file not shown.
Binary file added test/data/types_ascii.rds
Binary file not shown.
14 changes: 13 additions & 1 deletion test/generate_rda.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# R script to generate test .rda files
# R script to generate test .rda and .rds files

df <- data.frame(num = c(1.1, 2.2))
save(df, file = "data/minimal.rda")
Expand Down Expand Up @@ -48,3 +48,15 @@ test.cmpfun0 <- cmpfun( test.fun0 )
test.cmpfun1 <- cmpfun( test.fun1 )
test.cmpfun2 <- cmpfun( test.fun2 )
save(test.cmpfun0, test.cmpfun1, test.cmpfun2, file = "data/cmpfun.rda")


# for converting rda files to rds to test with readRDS
rdafiles = list.files("data/", pattern="*.rda", full.names=T)
for (rdafile in rdafiles) {
en = new.env()
load(rdafile, envir=en)
lst = as.list(en)
rdsfile = gsub("\\.rda$", ".rds", rdafile)
saveRDS(lst, file=rdsfile)
}

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ using RData
using Base.Test

include("RDA.jl")
include("RDS.jl")

0 comments on commit 186d1d6

Please sign in to comment.