Stata .ado to export matrices as HTML tables
This program exports tables stored as matrix to valid HTML tables. Labeling and some tweaking/styling is possible, however, styling is assumed to be done via CSS classes assigned to the written HTML table. Useful in dynamic documents where some table output styling is desired. For Stata V13.0 or greater.
net install mat2html, from("https://raw.githubusercontent.com/maximilian-sprengholz/mat2html/master/pkg/")
This Stata code...
// example matrix
mat M = matuniform(6,8)
forvalues c=1/8 {
foreach r in 3 6 {
mat M[`r',`c']=`c'*`r'*1000 // example N
}
}
// export HTML table
local rowno = rowsof(M)
mat2html M using "mytable.html", ///
f(rowm, flist(3 %9.0gc) panel (3)) /// every third row (N) formatted as %9.0gc
par(par, rows(2(3)6)) /// enclose every second row (se) in the 3-row panel in parentheses
rowl("b" "se" "N") /// add row labels
coll("Model 1" "Model 2" "Model 3" "Model 4") colspan(2) /// add first set of col labels
coltwol("Subpop1" "Subpop2") rep /// 2nd set
class(my-example-class) /// you can see the class when looking at the HTML code
note("My table note")
...creates the following table output of mytable.html
(of course, the random cell contents will vary with reproduction):
Model 1 | Model 2 | Model 3 | Model 4 | |||||
---|---|---|---|---|---|---|---|---|
Subpop1 | Subpop2 | Subpop1 | Subpop2 | Subpop1 | Subpop2 | Subpop1 | Subpop2 | |
b | 0.145 | 0.370 | 0.257 | 0.155 | 0.870 | 0.709 | 0.439 | 0.324 |
se | (0.895) | (0.978) | (0.802) | (0.349) | (0.488) | (0.609) | (0.724) | (0.565) |
N | 3,000 | 6,000 | 9,000 | 12,000 | 15,000 | 18,000 | 21,000 | 24,000 |
b | 0.917 | 0.549 | 0.729 | 0.569 | 0.966 | 0.169 | 0.076 | 0.781 |
se | (0.153) | (0.735) | (0.084) | (0.414) | (0.828) | (0.552) | (0.664) | (0.490) |
N | 6,000 | 12,000 | 18,000 | 24,000 | 30,000 | 36,000 | 42,000 | 48,000 |
Please use the help-file installed with the package for details on syntax and options.
There are several possibilities to combine written text and Stata output within dynamic documents. Usually, the Stata output is directly included into the document as a code block: However, such tables look exactly like the ouput in the Stata results window. This might not be desired. Using mat2html, you write tables into extra HTML files which are then included into the HTML files generated by, for example, Stata's own dyndoc or Ben Jann's webdoc environment:
// Using dyndoc:
<<dd_include: "mytable.html">>
// Using webdoc:
webdoc append "mytable.html"
Personally, I prefer to write Markdown/HTML and Stata code within editors like Atom providing syntax highlighting for multiple languages within a single file. You could, for example, use the package markdown-preview-enhanced within Atom to include your HTML files at any place in the document, adding it to the DOM via:
@import "mytable.html"
Maximilian Sprengholz
Humboldt-Universität zu Berlin
[email protected]