Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown Table 1000 separator

Tags:

r-markdown

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

like image 590
Krishnan Avatar asked Aug 04 '15 15:08

Krishnan


People also ask

Can you make a table in R markdown?

By default, R Markdown displays data frames and matrixes as they would be in the R terminal (in a monospaced font). If you prefer that data be displayed with additional formatting you can use the knitr::kable function, as in the . Rmd file below.

How do I insert a table in R markdown?

To use it, open a Rmd or R document and select “Addins –> Insert Table”.

What does Kable do in R?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

How do I hide code in R markdown?

Chunk Optionsinclude = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file.


2 Answers

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.

like image 100
FvD Avatar answered Oct 14 '22 20:10

FvD


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 
----------------------------
like image 35
daroczig Avatar answered Oct 14 '22 18:10

daroczig