2013-11-25 23 views
6

我知道這是一個相當老的問題,之前已經討論過,但我無法按預期工作。如何設置使用knitr/pander生成的報告中的小數位數?

我有一個降價的文件,我想用knitrpander產生的.docx報告與兩位小數一致的數字格式,如0.12,3.60,14.00,或163.21爲內聯和塊輸出。我已閱讀此線程How to avoid using round() in every \Sexpr{}?其中建議pander可以自動執行此操作。但是,它似乎並不適合我。請讓我知道我在這裏錯過了什麼。

腳本:

```{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")) 
``` 

但結果不是我所期望的(注意小數的範圍幾乎爲0之間的一切,7):

enter image description here

+0

參見[我的回答(https://stackoverflow.com/a/47402570/7196903)爲一種解決方法非常類似的問題對於一般問題,使pander始終顯示_相同的小數位數:) :) –

回答

6

什麼:

> 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 
---------------------------------------------------------------- 

關於直列[R塊:也叫pander有或塗抹一些掛鉤自動做到這一點。


更新:有沒有必要在這裏設置的位數爲你設置的小數位數後,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 
---------------------------------------------------------------- 

進一步更新:爲什麼它與第一次運行的第二個表中的集合digits一起工作:

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

pandoc.table以列爲基礎運行format,以便列中的數字具有基於用戶請求的相同小數位數(即使尾隨零也設置爲TRUE)。

請打開在GitHub上的問題,如果這看起來像一個bug:https://github.com/Rapporter/pander

+0

kedves @daroczig;)非常感謝和pander一起工作。但是,您的解決方案不適用於第一個表格(請參閱例如F值,該值應該是15.36)任何想法爲什麼......? – sparrow

+0

@sparrow謝謝您將我的注意力指向這個問題!我剛剛以快速的直覺更新了我的答案,但請給我一些時間讓它睡上一覺,我一定會回到這個問題上,考慮後臺發生了什麼 - 希望明天。 – daroczig

9

有你試用

options(scipen=1, digits=2) 

http://yihui.name/knitr/demo/output/

+0

是的,我有。問題在於(1)'pander()'表不受影響,(2)如果你指定了例如'model2 = lm(weight〜1,chickwts); AIC(model1,model2)''我得到'model1 7 778 model2 2 823'沒有小數,這不僅僅是一個美學問題。 – sparrow

+0

基本問題是pander不允許你提供'format()''nsmall'參數。請參閱[我的回答](https://stackoverflow.com/a/47402570/7196903)針對解決方法的一個非常類似的問題:) –