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 (3) 

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]=

Create a Markdown string for a Dataset:

In[3]:=
ResourceFunction["MarkdownTableString"][
 Dataset[
  {
   <|"a" -> 1, "b" -> 2, "c" -> 3|>,
   <|"b" -> 4, "a" -> 5, "c" -> 6|>,
   <|"b" -> 7, "a" -> 8, "c" -> 9|>
   }
  ]
 ]
Out[3]=

Scope (2) 

Simple Association formatted as labeled rows:

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

Add a header to label just the keys:

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

Add another header to label the values:

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

Label just the values:

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

Format a Dataset in Markdown with row and column headers:

In[8]:=
ResourceFunction["MarkdownTableString"][
 Dataset[
  <|
   "Row1" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>,
   "Row2" -> <|"b" -> 4, "a" -> 5, "c" -> 6|>
   |>
  ]
 ]
Out[8]=

Options (1) 

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

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

Applications (3) 

Create a Markdown table of food data:

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

Copy the table to the clipboard:

In[12]:=
CopyToClipboard[markdown]

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

Properties and Relations (2) 

Missing values are automatically padded:

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

Partial headers are padded:

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

Possible Issues (2) 

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

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

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

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

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

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

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

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

Publisher

Andrew Steinacher

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.1 – 12 November 2020
  • 2.0.0 – 16 June 2020
  • 1.0.0 – 08 March 2019

Related Resources

License Information