maximilian-sprengholz / mat2html

Stata .ado to export matrices as HTML tables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mat2html

Stata .ado to export matrices as HTML tables

Description

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.

Installation

net install mat2html, from("https://raw.githubusercontent.com/maximilian-sprengholz/mat2html/master/pkg/")

Example

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 1Model 2Model 3Model 4
Subpop1Subpop2Subpop1Subpop2Subpop1Subpop2Subpop1Subpop2
b0.1450.3700.2570.1550.8700.7090.4390.324
se(0.895)(0.978)(0.802)(0.349)(0.488)(0.609)(0.724)(0.565)
N3,0006,0009,00012,00015,00018,00021,00024,000
b0.9170.5490.7290.5690.9660.1690.0760.781
se(0.153)(0.735)(0.084)(0.414)(0.828)(0.552)(0.664)(0.490)
N6,00012,00018,00024,00030,00036,00042,00048,000
My table note

Documentation

Please use the help-file installed with the package for details on syntax and options.

Usage within dynamic documents

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"

Author

Maximilian Sprengholz
Humboldt-Universität zu Berlin
maximilian.sprengholz@hu-berlin.de

About

Stata .ado to export matrices as HTML tables

License:The Unlicense


Languages

Language:Stata 99.7%Language:TeX 0.3%