Skip to content

Commit b780081

Browse files
authored
Merge pull request #151 from jmboehm/Add-functions-to-find-vertical-gaps
Add vertical gaps function
2 parents 829e2bb + 1265346 commit b780081

File tree

4 files changed

+70
-158
lines changed

4 files changed

+70
-158
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RegressionTables"
22
uuid = "d519eb52-b820-54da-95a6-98e1306fdade"
33
authors = ["Johannes Boehm <[email protected]>"]
4-
version = "0.7.1"
4+
version = "0.7.2"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ RegressionTables.drop_names!
7272
RegressionTables.add_blank
7373
RegressionTables.missing_vars
7474
RegressionTables.add_element!
75+
RegressionTables.find_vertical_gaps
7576
```
7677

7778
## How Types are Displayed

src/regressiontable.jl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
render::T
88
breaks::Vector{Int}
99
colwidths::Vector{Int}
10+
vertical_gaps::Vector{Int}
1011
end
1112
1213
RegressionTable(header::Vector, body::Matrix, args...; vargs...)
@@ -32,6 +33,8 @@ lines (e.g., `\\midrule` in LaTeX).
3233
the line number is printed (breaks = [5] will print a break after the 5th line is printed).
3334
- `colwidths`: A vector of integers, one for each column, indicating the width of the column. Can calculate the widths
3435
automatically using [`calc_widths`](@ref) and update them with [`update_widths!`](@ref).
36+
- `vertical_gaps`: A vector of integers, indicating where to put vertical gaps in the table. These are places where two underlined cells
37+
are not connected. This is necessary for Excel integration.
3538
3639
The `RegressionTable` is a subtype of the `AbstractMatrix{String}` type (though this functionality is somewhat experimental).
3740
Importantly, this implements `getindex` and `setindex!`, which means individual elements of the table can be modified after
@@ -88,18 +91,47 @@ mutable struct RegressionTable{T<:AbstractRenderType} <: AbstractMatrix{String}
8891
align::String,
8992
breaks=[length(data)],
9093
colwidths::Vector{Int}=zeros(Int, length(data[1])),
91-
vertical_gaps::Vector{Int}=Int[]
94+
vertical_gaps::Union{Nothing, Vector{Int}}=nothing
9295
) where {T<:AbstractRenderType}
9396
if all(colwidths .== 0)
9497
colwidths = calc_widths(data)
9598
end
9699
update_widths!.(data, Ref(colwidths))
100+
if vertical_gaps === nothing
101+
vertical_gaps = find_vertical_gaps(data)
102+
end
97103
@assert all(length.(data) .== length(colwidths)) && length(colwidths) == length(align) "Not all the correct length"
98104
@assert length(data) .>= maximum(breaks) "Breaks must be less than the number of rows"
99105
new{T}(data,align, T(), breaks, colwidths, vertical_gaps)
100106
end
101107
end
102108

109+
"""
110+
find_vertical_gaps(data::Vector{DataRow{T}}) where T
111+
112+
Finds locations in a vector of `DataRow` objects where there are vertical gaps. A "vertical gap" is a place
113+
where two underlined cells are not connected. If there is a gap, then there needs to be a space between
114+
the current column and the next column. This makes it unnecessary to have a final vertical gap since
115+
the table ends there.
116+
"""
117+
function find_vertical_gaps(data::Vector{DataRow{T}}) where T
118+
out = Set{Int}()
119+
for row in data
120+
i = 0
121+
for (j, x) in enumerate(row.data)
122+
if isa(x, Pair)
123+
i += length(last(x))
124+
else
125+
i += 1
126+
end
127+
if row.print_underlines[j] && i < length(row)
128+
push!(out, i)
129+
end
130+
end
131+
end
132+
sort(collect(out))
133+
end
134+
103135
Base.size(tab::RegressionTable) = (length(data(tab)), length(data(tab)[1]))
104136
Base.size(tab::RegressionTable, i::Int) = size(tab)[i]
105137
Base.getindex(tab::RegressionTable, i::Int, j::Int) = data(tab)[i][j]

0 commit comments

Comments
 (0)