How to set the number of decimal places in a report created with knitr / pander?

I know this is a pretty old problem, and it was discussed earlier, but I cannot get it to work properly.

I have a markup document, and I would like to use knitr and pander to create a .docx report with a consistent number format with two decimal places, such as 0.12, 3.60, 14.00 or 163.21 for inline and block outputs. I read this topic. How to avoid using round () in every \ Sexpr {}? where it was suggested that the pander can do this automatically. However, this does not seem to work for me. Please let me know what I'm missing here.

script:

 ```{r, echo=FALSE} library(knitr) opts_chunk$set(echo = FALSE, message = FALSE, results = 'asis') ``` ```{r} require(pander) panderOptions('digits' , 2) #this should do the trick, right? ``` Test ===== Let produce some test stats: ```{r} model1 = lm(weight~feed, chickwts) anova.m1 = anova(model1) pander(anova.m1) pander(coef(summary(model1))) ``` In-line R codes: "Type of food affects body mass of the chicks (F~`r anova.m1$Df[1]`,`r anova.m1$Df[2]`~ = `r anova.m1$F[1]`, p = `r anova.m1$Pr[1]`)." ```{r} FILE <- "Test" system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md")) ``` 

But the results are not what I would expect (note that the range of decimals is almost everything between 0 and 7):

enter image description here

+6
source share
2 answers

What about:

 > library(pander) > panderOptions('digits', 2) > panderOptions('round', 2) > panderOptions('keep.trailing.zeros', TRUE) > pander(anova.m1) ---------------------------------------------------------- &nbsp; Df Sum Sq Mean Sq F value Pr(>F) --------------- ---- -------- --------- --------- -------- **feed** 5 231129 46226 15 0 **Residuals** 65 195556 3009 ---------------------------------------------------------- Table: Analysis of Variance Table > pander(coef(summary(model1))) ---------------------------------------------------------------- &nbsp; Estimate Std. Error t value Pr(>|t|) ------------------- ---------- ------------ --------- ---------- **(Intercept)** 323.58 15.83 20.44 0.00 **feedhorsebean** -163.38 23.49 -6.96 0.00 **feedlinseed** -104.83 22.39 -4.68 0.00 **feedmeatmeal** -46.67 22.90 -2.04 0.05 **feedsoybean** -77.15 21.58 -3.58 0.00 **feedsunflower** 5.33 22.39 0.24 0.81 ---------------------------------------------------------------- 

About the built-in R-fragments: also call pander or apply some interceptors to do this automatically.


Update : there is no need to indicate the number of digits here, as you are after setting the number of decimal places, sry:

 > library(pander) > panderOptions('round', 2) > panderOptions('keep.trailing.zeros', TRUE) > model1 = lm(weight~feed, chickwts) > anova.m1 = anova(model1) > pander(anova.m1) ---------------------------------------------------------- &nbsp; Df Sum Sq Mean Sq F value Pr(>F) --------------- ---- -------- --------- --------- -------- **feed** 5 231129 46226 15.36 0 **Residuals** 65 195556 3009 ---------------------------------------------------------- Table: Analysis of Variance Table > pander(coef(summary(model1))) ---------------------------------------------------------------- &nbsp; Estimate Std. Error t value Pr(>|t|) ------------------- ---------- ------------ --------- ---------- **(Intercept)** 323.58 15.83 20.44 0.00 **feedhorsebean** -163.38 23.49 -6.96 0.00 **feedlinseed** -104.83 22.39 -4.68 0.00 **feedmeatmeal** -46.67 22.90 -2.04 0.05 **feedsoybean** -77.15 21.58 -3.58 0.00 **feedsunflower** 5.33 22.39 0.24 0.81 ---------------------------------------------------------------- 

Further update: and why it worked with installing digits in the second table for the first run:

 > format(c(0.01, 15.36 ), digits = 2) [1] " 0.01" "15.36" > format(15.36, digits = 2) [1] "15" 

And pandoc.table runs format based on the column, so the numbers in the column will have the same number of decimal places (even trailing zeros with this option set to TRUE ) based on the user request.

Please open the problem on GitHub if it looks like an error: https://github.com/Rapporter/pander

+6
source

You tried

  options(scipen=1, digits=2) 

like in http://yihui.name/knitr/demo/output/ ?

+9
source

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


All Articles