Home > database >  RMarkdown: knitr::kable::format.args does not work globally
RMarkdown: knitr::kable::format.args does not work globally

Time:08-09

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:

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.

CodePudding user response:

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.

  • Related