r-markdownpandoc

Tables in R markdown


I would like to create a manual table in R markdown, I am aiming to have the final output as follow: enter image description here

I tried the following code but it did not work:

Authority   | Responsibility                 | Period
:-----      | :----                          | :-----
 MOIWR      |           Text 1               | 2010
   ^^       |           Text 2               | 2011 
   ^^       |           Text 3               | 2012  
   IWC      |           Text 4               | 2013 
   SGB      |           Text 5               | |

could you please help me to figure out how to do that !


Solution

  • Pandoc, the converter used in R Markdown, does not yet support Markdown tables with cells spanning multiple rows and/or columns. A good workaround is to write the table in HTML and to parse it in a Lua filter.

    The following filter detects HTML tables and makes sure they can be converted to different output formats:

    function RawBlock (raw)
      if raw.format:match 'html' and raw.text:match '^%s*%<table' then
        return pandoc.read(raw.text, 'html').blocks
      end
    end
    

    Use the filter like this:

    ---
    output:
      html_document:
        pandoc_args:
          - '--lua-filter=html-table.lua'
    ---
    
    ``` {=html}
    <table>
      <tr>
        <td>column 1</td>
        <td>column 2</td>
      </tr>
      <tr>
        <td colspan="2">column 1 and 2</td>
      </tr>
    </table>
    ```