16

I am trying to publish a table with 1000 separators and I am not having any luck with it. I followed the link here: Set global thousand separator on knitr but am not having much success.

My sample dataset is here: https://goo.gl/G7sZhr

The RMarkdown code is here:

---
title: "Table Example"
author: "Krishnan Viswanathan"
date: "August 4, 2015"
output: html_document
---

Load Data

{r, results='asis', message = FALSE, tidy=TRUE} load("i75_from_flow.RData") library(data.table)

{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE} i75_from_flow <- i75_from_flow[order(-Tons),] knitr::kable(i75_from_flow)

However, when I include this chunk of code (knit_hook$set) in the RMarkdown document, i get errors.

```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
knitr::kable(i75_from_flow)
```

Error:

# object knit_hooks not found.

Any insights on what I am doing wrong and how to fix this is much appreciated.

Thanks,

Krishnan

Community
  • 1
  • 1
Krishnan
  • 1,265
  • 2
  • 13
  • 24

3 Answers3

24

The easiest is to use the format arguments of the kable() function itself, where you can specify the big number mark like so:

kable(df, format.args = list(big.mark = ","))

So your example would look like:

```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}

i75_from_flow <- i75_from_flow[order(-Tons),]
knitr::kable(i75_from_flow, format.args = list(big.mark = ","))
```

without any need for knitr hooks.

FvD
  • 3,697
  • 1
  • 35
  • 53
7

What about using pander with bunch of options to fine-tune your markdown table:

> pander::pander(i75_from_flow, big.mark = ',')

----------------------------
 ORIGFIPS   TERMFIPS   Tons 
---------- ---------- ------
  12,023     12,117   5,891 

  12,119     12,105   4,959 

  12,001     12,057   3,585 

  12,001     12,113   3,083 

  12,047     12,047   1,517 
----------------------------
daroczig
  • 28,004
  • 7
  • 90
  • 124
4

The reason that the knit_hooks object is not found is that you either need to load the knitr package or use the knitr:: prefix in order to set the knit_hooks options. For example:

knitr::knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
W. Joel Schneider
  • 1,711
  • 13
  • 14