Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RMarkdown: knitr::kable::format.args does not work globally

I have an R code (RMarkdown) to generate a table with knitr.kable. As I will need to generate several tables with the same formatting, from several data.frames, placed as settings globally. However the format.args option does not work globally.

This way it works:

---
title: "Indicadores Fiscais e Gerenciais da Câmara de Vereadores"
author: "Everton da Rosa"
date: "`r Sys.Date()`"
output: 
  pdf_document: 
    toc: yes
    toc_depth: 4
    fig_caption: yes
    number_sections: yes
  html_document: 
    toc: yes
    toc_depth: 4
    theme: readable
    number_sections: yes
    df_print: kable
    fig_caption: yes
---

```{r setup, include=FALSE}
options(
  encoding = 'ISO-8859-1',
  knitr.kable.NA = '',
  knitr.kable.digits = 2,
  knitr.kable.format='latex'
)

knitr_kable_format_args = list(big.mark='.', decimal.mark=',', nsmall=2)

knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(result = 'hold')


#if (!require("devtools")) install.packages('devtools')
#library(devtools)

if(!require('tidyverse')) install.packages('tidyverse')
library(tidyverse)

if(!require('stargazer')) install.packages('stargazer')
library(stargazer)
```

```{r}
# Caminho para o arquivo com os dados
ds_file <- 'indicadores cm.xlsx'
```

## Resultado Orçamentário

```{r}
df_ro <- readxl::read_excel(ds_file, sheet='ResultadoOrcamentario')
knitr::kable(df_ro, align=c('l', 'r', 'r'), caption='Resultado Orçamentário', format.args = knitr_kable_format_args)
```

The Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output

However, if I remove format.args from the knitr.kable() call and put it in the option() call, it stops working:

---
title: "Indicadores Fiscais e Gerenciais da Câmara de Vereadores"
author: "Everton da Rosa"
date: "`r Sys.Date()`"
output: 
  pdf_document: 
    toc: yes
    toc_depth: 4
    fig_caption: yes
    number_sections: yes
  html_document: 
    toc: yes
    toc_depth: 4
    theme: readable
    number_sections: yes
    df_print: kable
    fig_caption: yes
---

```{r setup, include=FALSE}
options(
  encoding = 'ISO-8859-1',
  knitr.kable.NA = '',
  knitr.kable.digits = 2,
  knitr.kable.format='latex',
  knitr.kable.format.args = list(big.mark='.', decimal.mark=',', nsmall=2)
)

knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(result = 'hold')


#if (!require("devtools")) install.packages('devtools')
#library(devtools)

if(!require('tidyverse')) install.packages('tidyverse')
library(tidyverse)

if(!require('stargazer')) install.packages('stargazer')
library(stargazer)
```

```{r}
# Caminho para o arquivo com os dados
ds_file <- 'indicadores cm.xlsx'
```

## Resultado Orçamentário

```{r}
df_ro <- readxl::read_excel(ds_file, sheet='ResultadoOrcamentario')
knitr::kable(df_ro, align=c('l', 'r', 'r'), caption='Resultado Orçamentário')
```

Number formatting no longer works:
The output fails

I looked for help in the documentation and on the internet, but I didn’t find anything about it.

>Solution :

I don’t see anything in the documentation that suggests putting that value in options() should work. The default for format.args is list(), not getOption("knitr.kable.format.args").

You might want to make a suggestion to Yihui to make that change. In the meantime, you can do it yourself:

kable <- function(..., format.args = getOption("knitr.kable.format.args", list())
  knitr::kable(..., format.args = format.args)

After this, call kable() for your version, and knitr::kable() for the original one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading