R: opts_chunk is not valid

I am working on my first R record, which works very well, except for one problem. I would like to be the numbers that I output with

`r realbignumber` 

have commas as a separator and max 2 decimal points: 123 456 789.12

To achieve this, I added a snippet at the beginning of my document that contains ...

 ```{r setup} knitr::opts_chunk$set(echo = FALSE, warning=FALSE, cache = TRUE, message = FALSE) knitr::opts_chunk$set(inline = function(x){if(!is.numeric(x)){x}else{prettyNum(round(x,1), big.mark = ",")}}) options(scipen=999) ``` 

Suppressing scientific numbers works like a charm, so the piece is definitely fulfilled. However, formatting the built-in output of numbers does not work.

Any ideas why this could be? Do these settings usually not work with R laptops?

Edit:

The solution proposed here also does not affect the output number format.

+5
source share
1 answer

Here is an example that illustrates two ways to print large quantities in an R Markdown document. Firstly, the code for using the prettyNum() function in the built-in part of R.

 Sample document where we test printing a large number. First set the number in an R chunk. ```{r initializeData} theNum <- 1234567891011.03 options(scipen=999,digits=16) ``` The R code we'll use to format the number is: `prettyNum(theNum,width=23,big.mark=",")`. Next, print the large number. `r prettyNum(theNum,width=23,big.mark=",")`. 

An alternative to using chunk options is as follows.

  Now, try an alternative using knitr chunks. ```{r prettyNumHook } knitr::knit_hooks$set(inline = function(x) { if(!is.numeric(x)){ x }else{ prettyNum(x, big.mark=",",width=23) } }) ``` Next, print the large number by simply referencing the number in an inline chunk as `theNum`: `r theNum`. 

When both pieces of code are embedded in the Rmd file and knit, the output is as follows, showing that both technologies give the same result.

enter image description here

Yours faithfully,

Len

+1
source

Source: https://habr.com/ru/post/1261393/


All Articles