Skip to content

Commit

Permalink
Better display of Nullables (#1084)
Browse files Browse the repository at this point in the history
* Better display of Nullables

* Don't write trailing space in Latex output

Also fix missing newline in show test
  • Loading branch information
amellnik authored and nalimilan committed Oct 5, 2016
1 parent e1c5014 commit 725a226
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/abstractdataframe/io.jl
Expand Up @@ -184,7 +184,7 @@ end
write(io, "<tr>")
write(io, "<th>$row</th>")
for column_name in cnames
cell = string(df[row, column_name])
cell = sprint(ourshowcompact, df[row, column_name])
write(io, "<td>$(html_escape(cell))</td>")
end
write(io, "</tr>")
Expand Down Expand Up @@ -232,8 +232,8 @@ function Base.show(io::IO, ::MIME"text/latex", df::AbstractDataFrame)
write(io, "\t& ")
header = join(map(c -> latex_escape(string(c)), cnames), " & ")
write(io, header)
write(io, "\\\\ \n")
write(io, "\t\\hline \n")
write(io, "\\\\\n")
write(io, "\t\\hline\n")
for row in 1:nrows
write(io, "\t")
write(io, @sprintf("%d", row))
Expand All @@ -249,7 +249,7 @@ function Base.show(io::IO, ::MIME"text/latex", df::AbstractDataFrame)
end
end
end
write(io, " \\\\ \n")
write(io, " \\\\\n")
end
write(io, "\\end{tabular}\n")
end
Expand Down
3 changes: 2 additions & 1 deletion src/abstractdataframe/show.jl
Expand Up @@ -62,8 +62,9 @@ end
#' ourshowcompact(STDOUT, "abc")
#' ourshowcompact(STDOUT, 10000)
ourshowcompact(io::IO, x::Any) = showcompact(io, x) # -> Void
ourshowcompact(io::IO, x::AbstractString) = showcompact(io, x) # -> Void
ourshowcompact(io::IO, x::AbstractString) = print(io, x) # -> Void
ourshowcompact(io::IO, x::Symbol) = print(io, x) # -> Void
ourshowcompact(io::IO, x::Nullable{String}) = isnull(x) ? showcompact(io, x) : print(io, get(x)) # -> Void

#' @description
#'
Expand Down
35 changes: 21 additions & 14 deletions test/io.jl
Expand Up @@ -333,7 +333,7 @@ module TestIO
df = DataFrame(A = NullableArray(Nullable{Int}[1,Nullable()]),
B = NullableArray(Nullable{String}["b", Nullable()]))
writetable(tf, df)
@test readcsv(tf) == ["A" "B"; 1 "b"; "NULL" "NULL"]
@test readcsv(tf) == ["A" "B"; 1 "b"; "NULL" "NULL"]

# Test writetable with nastring set and compare to the results
isfile(tf) && rm(tf)
Expand Down Expand Up @@ -493,20 +493,27 @@ module TestIO
@test_throws ArgumentError eval(:(csv"foo,bar"a))

# Test LaTeX export
df = DataFrame(A = 1:4,
B = ["\$10.0", "M&F", "A~B", "\\alpha"],
df = DataFrame(A = 1:4,
B = ["\$10.0", "M&F", "A~B", "\\alpha"],
C = [L"\alpha", L"\beta", L"\gamma", L"\sum_{i=1}^n \delta_i"],
D = [1.0, 2.0, Nullable(), 3.0]
)
@test isequal(reprmime(MIME("text/latex"), df),
"""
\\begin{tabular}{r|cccc}
\t& A & B & C & D\\\\
\t\\hline
\t1 & 1 & \\\$10.0 & \$\\alpha\$ & 1.0 \\\\
\t2 & 2 & M\\&F & \$\\beta\$ & 2.0 \\\\
\t3 & 3 & A\\textasciitilde{}B & \$\\gamma\$ & \\\\
\t4 & 4 & \\textbackslash{}alpha & \$\\sum_{i=1}^n \\delta_i\$ & 3.0 \\\\
\\end{tabular}
""")
str = """
\\begin{tabular}{r|cccc}
\t& A & B & C & D\\\\
\t\\hline
\t1 & 1 & \\\$10.0 & \$\\alpha\$ & 1.0 \\\\
\t2 & 2 & M\\&F & \$\\beta\$ & 2.0 \\\\
\t3 & 3 & A\\textasciitilde{}B & \$\\gamma\$ & \\\\
\t4 & 4 & \\textbackslash{}alpha & \$\\sum_{i=1}^n \\delta_i\$ & 3.0 \\\\
\\end{tabular}
"""
@test reprmime(MIME("text/latex"), df) == str

#Test HTML output for IJulia and similar
df = DataFrame(Fish = ["Suzy", "Amir"], Mass = [1.5, Nullable()])
io = IOBuffer()
show(io, "text/html", df)
str = takebuf_string(io)
@test str == "<table class=\"data-frame\"><tr><th></th><th>Fish</th><th>Mass</th></tr><tr><th>1</th><td>Suzy</td><td>1.5</td></tr><tr><th>2</th><td>Amir</td><td>#NULL</td></tr></table>"
end
13 changes: 13 additions & 0 deletions test/show.jl
@@ -1,6 +1,7 @@
module TestShow
using DataFrames
using Compat
using Base.Test
import Compat.String
df = DataFrame(A = 1:3, B = ["x", "y", "z"])

Expand Down Expand Up @@ -35,4 +36,16 @@ module TestShow
show(io, A)
A = DataFrames.RepeatedVector([1, 2, 3], 1, 5)
show(io, A)

#Test show output for REPL and similar
df = DataFrame(Fish = ["Suzy", "Amir"], Mass = [1.5, Nullable()])
io = IOBuffer()
show(io, df)
str = takebuf_string(io)
@test str == """
2×2 DataFrames.DataFrame
│ Row │ Fish │ Mass │
├─────┼──────┼───────┤
│ 1 │ Suzy │ 1.5 │
│ 2 │ Amir │ #NULL │"""
end

0 comments on commit 725a226

Please sign in to comment.