Highlight cells in markdown tables

[This article was first published on rapporter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Although I have always wanted to add such feature to pander, a recent question on SO urged me to create some helper functions so that users could easily highlight some rows, columns or even just a few cells in a table and export the result to markdown, docx, pdf or any other fancy formats – right from R.

The new helper functions can be found in the most recent version of pander on GitHub and will soon get to CRAN too. Until then, please update pander to 0.3.5 with devtools:

library(devtools)
install_github('pander', 'Rapporter')

But back to the topic, pandoc.table gained a few new arguments to highlight cells in a table. These are:
  • emphasize.rows
  • emphasize.cols
  • emphasize.cells
  • emphasize.strong.rows
  • emphasize.strong.cols
  • emphasize.strong.cells
emphasize would turn the affected cells to italic, while emphasize.strong would apply a bold style in the cell. Of course a cell can be italic and strong at the same time if both type of arguments would affect that.

Arguments ending in rows or cols take a vector (e.g. which columns or rows to emphasize in a table), while the cells argument take either a vector (for one dimensional “tables”) or an array-like data structure with two columns holding row and column indexes of cells to be emphasized — just like what which(..., arr.ind = TRUE) returns.

It might be easier to present this feature with a few examples:

> pandoc.table(iris[1:3, ], emphasize.rows = 1, emphasize.cols = 2)

-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
    *5.1*          *3.5*         *1.4*          *0.2*     *setosa* 

     4.9           *3.0*          1.4            0.2       setosa  

     4.7           *3.2*          1.3            0.2       setosa  
-------------------------------------------------------------------

> pandoc.table(iris[1:3, ], emphasize.strong.cells = which(iris[1:3, ] > 3, arr.ind = TRUE))

-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
   **5.1**        **3.5**         1.4            0.2       setosa  

   **4.9**          3.0           1.4            0.2       setosa  

   **4.7**        **3.2**         1.3            0.2       setosa  
-------------------------------------------------------------------

Or with some helper functions not touching the pandoc.table call:

> t <- mtcars[1:3, 1:5]
> emphasize.cols(1)
> emphasize.rows(1)
> pandoc.table(t)

----------------------------------------------------
                     mpg    cyl   disp   hp    drat 
------------------- ------ ----- ------ ----- ------
   **Mazda RX4**     *21*   *6*  *160*  *110* *3.9* 

 **Mazda RX4 Wag**   *21*    6    160    110   3.9  

  **Datsun 710**    *22.8*   4    108    93    3.85 
----------------------------------------------------

> emphasize.strong.cells(which(t > 20, arr.ind = TRUE))
> pander(t)

---------------------------------------------------------
                      mpg     cyl   disp     hp     drat 
------------------- -------- ----- ------- ------- ------
   **Mazda RX4**     **21**    6   **160** **110**  3.9  

 **Mazda RX4 Wag**   **21**    6   **160** **110**  3.9  

  **Datsun 710**    **22.8**   4   **108** **93**   3.85 
---------------------------------------------------------

As you can see above, there is no need to directly call pandoc.table in most of the cases as pander being a generic S3 method would already know if pandoc.table is to be called from the list of Pandoc-related functions.

So the functions can be also used without calling pandoc.table with the above described parameters, just like some other helpers that can be useful while creating a report – with knitr or any other backend. For example running Pandoc.brew on the following chunk:

<%=
set.seed(7)
m <- mtcars[sample(1:32, 5), 1:5]
set.caption("Fast cars")
set.alignment('center')
emphasize.strong.rows(which(m$hp > 100))
m
%>

Would result in a markdown dokument:

--------------------------------------------------------------
                  mpg     cyl    disp      hp      drat  
-------------------- -------- ----- --------- ------- --------
   **Volvo 142E**    **21.4** **4**  **121**  **109** **4.11**

   **Merc 450SL**    **17.3** **8** **275.8** **180** **3.07**

 **Hornet 4 Drive**  **21.4** **6**  **258**  **110** **3.08**

   **Datsun 710**      22.8     4      108      93      3.85  

   **Duster 360**    **14.3** **8**  **360**  **245** **3.21**
--------------------------------------------------------------

Table: Fast cars

Or directly in a docx, odt or pdf file if calling Pandoc.brew with the convert argument. See the relevant docs for more details.

To leave a comment for the author, please follow the link and comment on their blog: rapporter.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)