I know it's a fairly old problem, and it has been discussed before, but I can't get it work as expected.
我知道这是一个相当古老的问题,之前已经讨论过,但我无法按预期工作。
I have a markdown document, and I would like to use knitr
and pander
to produce a .docx report with a consistent numeric format with two decimals, such as 0.12, 3.60, 14.00, or 163.21 for both inline and chunk outputs. I have read this thread How to avoid using round() in every \Sexpr{}? where it was suggested that pander
can do that automatically. However, it does not seem to work for me. Please let me know what I'm missing here.
我有一个markdown文档,我想使用knitr和pander来生成一个.docx报告,其中包含一个两位小数的数字格式,例如0.12,3.60,14.00或163.21,用于内联和块输出。我已阅读此主题如何避免在每个\ Sexpr {}中使用round()?有人建议pander可以自动完成。但是,它似乎对我不起作用。请让我知道我在这里失踪了什么。
The 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's 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):
但结果并不是我所期望的(注意小数范围几乎是0到7之间的所有内容):
2 个解决方案
#1
6
What about:
> library(pander)
> panderOptions('digits', 2)
> panderOptions('round', 2)
> panderOptions('keep.trailing.zeros', TRUE)
> pander(anova.m1)
----------------------------------------------------------
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)))
----------------------------------------------------------------
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 inline R chunks: also call pander
there or apply some hooks to do that automatically.
关于内联R块:也可以在那里调用pander或者应用一些钩子来自动完成。
Update: there's no need to set the number of digits here as you are after setting the number of decimals, sry:
更新:在设置小数位数后,不需要在此处设置位数,sry:
> library(pander)
> panderOptions('round', 2)
> panderOptions('keep.trailing.zeros', TRUE)
> model1 = lm(weight~feed, chickwts)
> anova.m1 = anova(model1)
> pander(anova.m1)
----------------------------------------------------------
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)))
----------------------------------------------------------------
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 set 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
on a column-basis so that the numbers in a column would have the same number of decimals (even trailing zeros with that option set to TRUE
) based on a user-request.
并且pandoc.table以列为基础运行格式,以便根据用户请求,列中的数字将具有相同的小数位数(即使是将该选项设置为TRUE的尾随零)。
Please open an issue at GitHub if this would look like a bug: https://github.com/Rapporter/pander
如果这看起来像一个bug,请在GitHub上打开一个问题:https://github.com/Rapporter/pander
#2
9
Have you tried
你有没有尝试过
options(scipen=1, digits=2)
as in http://yihui.name/knitr/demo/output/ ?
如http://yihui.name/knitr/demo/output/?
#1
6
What about:
> library(pander)
> panderOptions('digits', 2)
> panderOptions('round', 2)
> panderOptions('keep.trailing.zeros', TRUE)
> pander(anova.m1)
----------------------------------------------------------
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)))
----------------------------------------------------------------
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 inline R chunks: also call pander
there or apply some hooks to do that automatically.
关于内联R块:也可以在那里调用pander或者应用一些钩子来自动完成。
Update: there's no need to set the number of digits here as you are after setting the number of decimals, sry:
更新:在设置小数位数后,不需要在此处设置位数,sry:
> library(pander)
> panderOptions('round', 2)
> panderOptions('keep.trailing.zeros', TRUE)
> model1 = lm(weight~feed, chickwts)
> anova.m1 = anova(model1)
> pander(anova.m1)
----------------------------------------------------------
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)))
----------------------------------------------------------------
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 set 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
on a column-basis so that the numbers in a column would have the same number of decimals (even trailing zeros with that option set to TRUE
) based on a user-request.
并且pandoc.table以列为基础运行格式,以便根据用户请求,列中的数字将具有相同的小数位数(即使是将该选项设置为TRUE的尾随零)。
Please open an issue at GitHub if this would look like a bug: https://github.com/Rapporter/pander
如果这看起来像一个bug,请在GitHub上打开一个问题:https://github.com/Rapporter/pander
#2
9
Have you tried
你有没有尝试过
options(scipen=1, digits=2)
as in http://yihui.name/knitr/demo/output/ ?
如http://yihui.name/knitr/demo/output/?