Skip to content

Commit

Permalink
Merge pull request #141 from junder873/add-typst-support
Browse files Browse the repository at this point in the history
Add support for Typst tables
  • Loading branch information
junder873 authored Mar 18, 2024
2 parents b780081 + d590e97 commit 7062d92
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RegressionTables"
uuid = "d519eb52-b820-54da-95a6-98e1306fdade"
authors = ["Johannes Boehm <[email protected]>"]
version = "0.7.2"
version = "0.7.3"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ RegressionTables.add_blank
RegressionTables.missing_vars
RegressionTables.add_element!
RegressionTables.find_vertical_gaps
RegressionTables.extra_cell_space
```

## How Types are Displayed
Expand Down
88 changes: 86 additions & 2 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Setup for the following examples:
```@meta
DocTestSetup = quote # hide
using RegressionTables, DataFrames, RDatasets, FixedEffectModels, GLM, MixedModels;
df = dataset("datasets", "iris");
df = RDatasets.dataset("datasets", "iris");
df[!,:isSmall] = df[!,:SepalWidth] .< 2.9;
rr1 = reg(df, @formula(SepalLength ~ SepalWidth));
rr2 = reg(df, @formula(SepalLength ~ SepalWidth + PetalLength + fe(Species)));
Expand Down Expand Up @@ -1315,7 +1315,7 @@ Pseudo R2 0.006 0.006 0.297 0.297
Displays whether or not the standard errors are clustered and in what ways.

```jldoctest
df_cigar = dataset("plm", "Cigar");
df_cigar = RDatasets.dataset("plm", "Cigar");
rr_c1 = reg(df_cigar, @formula(Sales ~ NDI + fe(State) + fe(Year)), Vcov.cluster(:State));
rr_c2 = reg(df_cigar, @formula(Sales ~ NDI + Price + fe(State) + fe(Year)), Vcov.cluster(:State, :Year));
Expand Down Expand Up @@ -1429,4 +1429,88 @@ Subject | Prec & Load 855.252
N 1,789 1,789 1,789
Log Likelihood -14,685.198 -14,765.033 -14,711.866
---------------------------------------------------------------
```

## Typst Support

Similar to Latex, this package can produce Typst Tables. This requires Typst v0.11.


```jldoctest
regtable(rr1,rr2,rr3,rr4; render = TypstTable())
# output
#table(
columns: (auto, auto, auto, auto, auto),
align: (left, right, right, right, right),
column-gutter: 1fr,
stroke: none,
table.hline(),
[] , table.cell(colspan: 3, align: center)[SepalLength] , table.cell(colspan: 1, align: center)[SepalWidth],
table.hline(start: 1, end: 4, stroke: 0.5pt), table.hline(start: 4, end: 5, stroke: 0.5pt),
[] , [(1)], [(2)], [(3)], [(4)],
table.hline(stroke: 0.7pt),
[(Intercept)] , [6.526$""^(***)$], [], [], [],
[] , [(0.479)], [], [], [],
[SepalWidth] , [-0.223], [0.432$""^(***)$], [0.516$""^(***)$], [],
[] , [(0.155)], [(0.081)], [(0.104)], [],
[PetalLength] , [], [0.776$""^(***)$], [0.723$""^(***)$], [-0.188$""^(*)$],
[] , [], [(0.064)], [(0.129)], [(0.083)],
[PetalWidth] , [], [], [-0.625], [0.626$""^(***)$],
[] , [], [], [(0.354)], [(0.123)],
[PetalLength $times$ PetalWidth], [], [], [0.066], [],
[] , [], [], [(0.067)], [],
[SepalLength] , [], [], [], [0.378$""^(***)$],
[] , [], [], [], [(0.066)],
table.hline(stroke: 0.7pt),
[Species Fixed Effects] , [], [Yes], [Yes], [Yes],
[isSmall Fixed Effects] , [], [], [Yes], [],
table.hline(stroke: 0.7pt),
[_N_] , [150], [150], [150], [150],
[_R_$""^2$] , [0.014], [0.863], [0.868], [0.635],
[Within-_R_$""^2$] , [], [0.642], [0.598], [0.391],
table.hline(),
)
```

```jldoctest
regtable(rr1, rr6, rr7; render = TypstTable())
# output
#table(
columns: (auto, auto, auto, auto),
align: (left, right, right, right),
column-gutter: 1fr,
stroke: none,
table.hline(),
[] , table.cell(colspan: 1, align: center)[SepalLength], table.cell(colspan: 2, align: center)[isSmall],
table.hline(start: 1, end: 2, stroke: 0.5pt), table.hline(start: 2, end: 4, stroke: 0.5pt),
[] , [(1)], [(2)], [(3)],
table.hline(stroke: 0.7pt),
[(Intercept)] , [6.526$""^(***)$], [-1.917], [10.189$""^(***)$],
[] , [(0.479)], [(1.242)], [(2.607)],
[SepalWidth] , [-0.223], [], [],
[] , [(0.155)], [], [],
[PetalLength] , [], [-0.773], [3.580$""^(***)$],
[] , [], [(0.554)], [(0.708)],
[PetalWidth] , [], [-3.782$""^(**)$], [-3.637$""^(**)$],
[] , [], [(1.256)], [(1.127)],
[Species: versicolor], [], [10.441$""^(***)$], [],
[] , [], [(1.957)], [],
[Species: virginica] , [], [13.230$""^(***)$], [],
[] , [], [(2.636)], [],
[SepalLength] , [], [], [-3.519$""^(***)$],
[] , [], [], [(0.697)],
table.hline(stroke: 0.7pt),
[Estimator] , [OLS], [Binomial], [Binomial],
table.hline(stroke: 0.7pt),
[_N_] , [150], [150], [150],
[_R_$""^2$] , [0.014], [], [],
[Pseudo _R_$""^2$] , [0.006], [0.347], [0.297],
table.hline(),
)
```
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To install the package, type in the Julia command prompt
```@example main_data
using RegressionTables, DataFrames, FixedEffectModels, RDatasets, GLM
df = dataset("datasets", "iris")
df = RDatasets.dataset("datasets", "iris")
rr1 = reg(df, @formula(SepalLength ~ SepalWidth + fe(Species)))
rr2 = reg(df, @formula(SepalLength ~ SepalWidth + PetalLength + fe(Species)))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/keep_drop_order.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
DocTestSetup = quote # hide
using RegressionTables, DataFrames, FixedEffectModels, RDatasets
df = dataset("datasets", "iris")
df = RDatasets.dataset("datasets", "iris")
df[!,:SpeciesDummy] = df[!,:Species]
rr1 = reg(df, @formula(SepalLength ~ SepalWidth + fe(SpeciesDummy)))
Expand Down
3 changes: 2 additions & 1 deletion src/RegressionTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module RegressionTables
##############################################################################

export regtable, LatexTable, LatexTableStar, AsciiTable, HtmlTable, AbstractRenderType,
AbstractAscii, AbstractLatex, AbstractHtml
AbstractAscii, AbstractLatex, AbstractHtml, TypstTable, AbstractTypst
export Nobs, R2, R2McFadden, R2CoxSnell, R2Nagelkerke,
R2Deviance, AdjR2, AdjR2McFadden, AdjR2Deviance, DOF, LogLikelihood, AIC, BIC, AICC,
FStat, FStatPValue, FStatIV, FStatIVPValue, R2Within, PseudoR2, AdjPseudoR2
Expand Down Expand Up @@ -65,6 +65,7 @@ module RegressionTables
include("rendersettings/ascii.jl")
include("rendersettings/latex.jl")
include("rendersettings/html.jl")
include("rendersettings/typst.jl")


# main settings
Expand Down
7 changes: 5 additions & 2 deletions src/datarow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function calc_widths(rows::Vector{DataRow{T}}) where {T<:AbstractRenderType}
end
end
else
out_lengths[i] = max(out_lengths[i], length(s))
out_lengths[i] = max(out_lengths[i], length(s) + extra_cell_space(render))
end
end
end
Expand All @@ -280,7 +280,10 @@ end
Updates the widths of each column in the row. If lengths are provided, then it should equate to the total number
of columns in the table, not the number of elements in the row.
"""
function update_widths!(row::DataRow{T}, new_lengths=length.(repr.(T(), row.data))) where {T}
function update_widths!(
row::DataRow{T},
new_lengths=length.(repr.(T(), row.data)) .+ extra_cell_space(T())
) where {T}
#@assert length(row) == length(new_lengths) "Wrong number of lengths"
render = T()
if length(row.data) == length(new_lengths)
Expand Down
7 changes: 7 additions & 0 deletions src/rendersettings/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ RegressionTables.interaction_combine(render::AbstractLatex) = " \\& "
"""
interaction_combine(render::AbstractRenderType) = " & "

"""
extra_cell_space(render::AbstractRenderType)
Used to add extra space to the cell and defaults to 0.
"""
extra_cell_space(::AbstractRenderType) = 0

"""
categorical_equal(render::AbstractRenderType)
Expand Down
91 changes: 91 additions & 0 deletions src/rendersettings/typst.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
abstract type AbstractTypst <: AbstractRenderType end
struct TypstTable <: AbstractTypst end

function tablestart(::AbstractTypst, align)
cols = [c == 'l' ? "left" : c == 'r' ? "right" : "center" for c in align]
cols_align = join(cols, ", ")
cols = join(fill("auto", length(cols)), ", ")

"""
#table(
columns: ($cols),
align: ($cols_align),
column-gutter: 1fr,
stroke: none,
"""
end

wrapper(::AbstractTypst, x) = "\$\"\"^($x)\$"

tableend(::AbstractTypst) = ")"

function Base.repr(rndr::AbstractTypst, val::Pair; align="c", args...)
s = Base.repr(rndr, first(val); args...)
if length(s) == 0 && length(last(val)) == 1
s
else
multicolumn(rndr, s, length(last(val)), align)
end
end
function multicolumn(rndr::AbstractTypst, s, cols::Int, align="c")
x = if align == "c" || align == 'c'
"center"
elseif align == "l" || align == 'l'
"left"
elseif align == "r" || align == 'r'
"right"
end
"table.cell(colspan: $cols, align: $x)[$s]"
end

underline(::AbstractTypst, colmin::Int, colmax::Int) = "table.hline(start: $(colmin-1), end: $(colmax), stroke: 0.5pt)"

toprule(x::AbstractTypst) = linestart(x) * "table.hline(), "
midrule(x::AbstractTypst) = linestart(x) * "table.hline(stroke: 0.7pt), "
bottomrule(x::AbstractTypst) = linestart(x) * "table.hline(), "
linestart(::AbstractTypst) = " "
lineend(::AbstractTypst) = ","
tablestart(tab::RegressionTable{<:AbstractTypst}) = tablestart(tab.render, tab.align)
colsep(::AbstractTypst) = ", "

label(::AbstractTypst, x::Type{Nobs}) = "_N_"
label(::AbstractTypst, x::Type{R2}) = "_R_\$\"\"^2\$"
label(::AbstractTypst, x::Type{FStat}) = "_F_"
label_p(::AbstractTypst, ) = "_p_"

extra_cell_space(::AbstractTypst) = 2 # to account for "[]"

interaction_combine(::AbstractTypst) = " \$times\$ "

function Base.print(io::IO, row::DataRow{T}) where {T<:AbstractTypst}
render = T()
print(io, linestart(render))
for (i, x) in enumerate(row.data)
s = if isa(x, Pair)
repr(render, x; align = row.align[i])
else
"[" * repr(render, x; align = row.align[i]) * "]"
end
print(io, make_padding(s, row.colwidths[i], row.align[i]))
if i < length(row.data)
print(io, colsep(render))
end
end
print(io, lineend(render))
if any(row.print_underlines)
println(io)
print(io, linestart(render))
for (i, x) in enumerate(row.data)
s = isa(x, Pair) ? repr(render, first(x)) : repr(render, x)
if length(s) == 0 || !row.print_underlines[i]
continue
end
if isa(x, Pair)
print(io, underline(render, first(last(x)), last(last(x))))
else
print(io, underline(render, i,i))
end
print(io, colsep(render))
end
end
end
2 changes: 1 addition & 1 deletion test/GLFixedEffectModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using GLFixedEffectModels, GLM, RDatasets, Test

# include("src/RegressionTables.jl")
df = dataset("datasets", "iris")
df = RDatasets.dataset("datasets", "iris")
df[!, :isSmall] = df[!, :SepalWidth] .< 2.9
df[!, :isWide] = df[!, :SepalWidth] .> 2.5
df[!, :binary] = @. ifelse(df[!, :PetalWidth] .> 1.0, 1.0, 0.0)
Expand Down
2 changes: 1 addition & 1 deletion test/RegressionTables.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using RegressionTables
using FixedEffectModels, GLM, RDatasets, Test

df = dataset("datasets", "iris")
df = RDatasets.dataset("datasets", "iris")
df[!, :isSmall] = df[!, :SepalWidth] .< 2.9
df[!, :isWide] = df[!, :SepalWidth] .> 2.5

Expand Down
2 changes: 1 addition & 1 deletion test/default_changes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ own test set
using RegressionTables
using FixedEffectModels, GLM, RDatasets, Test

df = dataset("datasets", "iris")
df = RDatasets.dataset("datasets", "iris")
df[!, :isSmall] = df[!, :SepalWidth] .< 2.9
df[!, :isWide] = df[!, :SepalWidth] .> 2.5

Expand Down

2 comments on commit 7062d92

@junder873
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103127

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.3 -m "<description of version>" 7062d92aadea5c2e2041d18d5562117349e286c6
git push origin v0.7.3

Please sign in to comment.