Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add space between thousands in Rmarkdown?

Tags:

html

r

r-markdown

Good morning,

I am writing a document in Rmarkdown and I would like to format all the output tables so that I can have "10 000" rather than "10000" in my HTML rendering.

I work with the kableExtra package mainly. Do you know if there is a special function to do this ?

I tried this :

kable(tableau_ST_CST, big.mark = " ") %>% kable_styling(full_width = F)

But it doesn't work.

like image 295
Zaggamim Avatar asked Sep 07 '25 08:09

Zaggamim


1 Answers

You can use prettyNum:

library(tidyverse)
library(knitr)

tibble(nums = 10000:10010) %>% 
  mutate(nums = prettyNum(nums, big.mark = " ")) %>%
  kable()
|nums   |
|:------|
|10 000 |
|10 001 |
|10 002 |
|10 003 |
|10 004 |
|10 005 |
|10 006 |
|10 007 |
|10 008 |
|10 009 |
|10 010 |

Other options include formatC, format (both have the same format), or simply using regex (str_replace_all("100000", pattern = "(\\d)(?=(\\d{3})+$)", "\\1 ")).

like image 107
Mark Avatar answered Sep 09 '25 05:09

Mark