1
module StatFiles
2

3
using ReadStat, IteratorInterfaceExtensions, TableTraits, TableTraitsUtils
4
using DataValues, FileIO, TableShowUtils
5
import IterableTables
6

7
export load, File, @format_str
8

9
struct StatFile
10 3
    filename::String
11
end
12

13
function Base.show(io::IO, source::StatFile)
14 3
    file_ext = lowercase(splitext(source.filename)[2])
15 3
    filetype = if file_ext==".dta"
16 3
        "STATA file"
17 3
    elseif file_ext==".sav"
18 3
        "SPSS file"
19 3
    elseif file_ext==".sas7bdat"
20 3
        "SAS file"
21
    else
22 3
        "file"
23
    end
24 3
    TableShowUtils.printtable(io, getiterator(source), filetype)
25
end
26

27
function Base.show(io::IO, ::MIME"text/html", source::StatFile)
28 3
    TableShowUtils.printHTMLtable(io, getiterator(source))
29
end
30

31 0
Base.Multimedia.showable(::MIME"text/html", source::StatFile) = true
32

33
function Base.show(io::IO, ::MIME"application/vnd.dataresource+json", source::StatFile)
34 3
    TableShowUtils.printdataresource(io, getiterator(source))
35
end
36

37 0
Base.Multimedia.showable(::MIME"application/vnd.dataresource+json", source::StatFile) = true
38

39
function fileio_load(f::FileIO.File{FileIO.format"Stata"})
40 3
    return StatFile(f.filename)
41
end
42

43
function fileio_load(f::FileIO.File{FileIO.format"SPSS"})
44 3
    return StatFile(f.filename)
45
end
46

47
function fileio_load(f::FileIO.File{FileIO.format"SAS"})
48 3
    return StatFile(f.filename)
49
end
50

51 3
IteratorInterfaceExtensions.isiterable(x::StatFile) = true
52 3
TableTraits.isiterabletable(x::StatFile) = true
53

54
function IteratorInterfaceExtensions.getiterator(file::StatFile)
55 3
    filename, extension = splitext(file.filename)
56 3
    if extension==".dta"
57 3
        df = read_dta(file.filename)
58 3
    elseif extension==".por"
59 0
        df = read_por(file.filename)
60 3
    elseif extension==".sav"
61 3
        df = read_sav(file.filename)
62 3
    elseif extension==".sas7bdat"
63 3
        df = read_sas7bdat(file.filename)
64
    else
65 0
        error("Unknown file type.")
66
    end
67

68 3
    it = TableTraitsUtils.create_tableiterator(df.data, df.headers)
69

70 3
    return it
71
end
72

73
end # module

Read our documentation on viewing source code .

Loading