How to set the number of decimals in report produced with knitr/pander?

后端 未结 2 1842
暗喜
暗喜 2020-12-30 07:29

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

相关标签:
2条回答
  • 2020-12-30 08:05

    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.


    Update: there's no need to set the number of digits here as you are after setting the number of decimals, 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.

    Please open an issue at GitHub if this would look like a bug: https://github.com/Rapporter/pander

    0 讨论(0)
  • 2020-12-30 08:11

    Have you tried

     options(scipen=1, digits=2)
    

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

    0 讨论(0)
提交回复
热议问题