Wolfram Research

Function Repository Resource:

MarkdownTableString

Source Notebook

Convert Wolfram Language data into Markdown-friendly table strings

Contributed by: Andrew Steinacher

ResourceFunction["MarkdownTableString"][data]

creates a Markdown table string of data.

ResourceFunction["MarkdownTableString"][data,{header1,header2,}]

creates a Markdown table string with column headers headeri.

ResourceFunction["MarkdownTableString"][assoc,k,v]

creates a Markdown table string for the values in assoc with headers k and v.

Examples

Basic Examples

Create a Markdown string for a simple table:

In[1]:=
ResourceFunction["MarkdownTableString"][
 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
 ]
Out[1]=

Create a Markdown string for a table with column headers:

In[2]:=
ResourceFunction["MarkdownTableString"][
 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
 {"A", "B", "C"}
 ]
Out[2]=

Scope

Simple Association formatted as labeled rows:

In[3]:=
ResourceFunction[
 "MarkdownTableString"][<|"A" -> 1, "B" -> 2, "C" -> 3|>]
Out[3]=

Add a header to label just the keys:

In[4]:=
ResourceFunction[
 "MarkdownTableString"][<|"A" -> 1, "B" -> 2, "C" -> 3|>, "Letter"]
Out[4]=

Add another header to label the values:

In[5]:=
ResourceFunction[
 "MarkdownTableString"][<|"A" -> 1, "B" -> 2, "C" -> 3|>, "Letter", "Number"]
Out[5]=

Options

Specify a specific string to use for values that are not present in the data:

In[6]:=
ResourceFunction["MarkdownTableString"][
 {{1, 2, 3}, {4, 5}, {7, 8, 9}},
 {"A", "B", "C"},
 "MissingTableElement" -> "MISSING"
 ]
Out[6]=

Applications

Create a Markdown table of food data:

In[7]:=
properties = {"Name", "FoodCount", "CommonOutsideColor", "CommonInsideColor", "ApproximateShape"};
data = EntityValue[EntityValue["FoodType", "SampleEntities"], properties];
markdown = ResourceFunction["MarkdownTableString"][
  data /. {e_Entity :> CommonName[e], _Missing -> "-"}, properties]
Out[8]=

Copy the table to the clipboard:

In[9]:=
CopyToClipboard[markdown]

Paste the table into a Markdown viewer (such as dillinger.io) to see the results formatted:

Out[9]=

Properties and Relations

Missing values are automatically padded:

In[10]:=
ResourceFunction["MarkdownTableString"][
 {{1, 2, 3}, {4, 5}, {7, 8, 9}},
 {"A", "B", "C"}
 ]
Out[10]=

Possible Issues

An Association with List values by default indicates labeled columns of data:

In[11]:=
a = <|"A" -> {1, 2, 3}, "B" -> {4, 5, 6}, "C" -> {7, 8, 9}|>;
ResourceFunction["MarkdownTableString"][a]
Out[12]=

Separate the keys and values to make the data correspond to rows:

In[13]:=
ResourceFunction["MarkdownTableString"][Values[a], Keys[a]]
Out[13]=

The option “MissingTableElement” does not cover values of the form Missing[…]:

In[14]:=
data = {{1, 2}, {3, 4}, {5, Missing[]}};
ResourceFunction["MarkdownTableString"][data, "MissingTableElement" -> "MISSING"]
Out[15]=

Use DeleteMissing to actually remove the data so that “MissingTableElement” will be used:

In[16]:=
ResourceFunction["MarkdownTableString"][DeleteMissing /@ data, "MissingTableElement" -> "MISSING"]
Out[16]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information