Rapporter / pander

An R Pandoc Writer: Convert arbitrary R objects into markdown
http://rapporter.github.io/pander/
Open Software License 3.0
294 stars 66 forks source link

pandoc.table should avoid four leading spaces when generating table info #289

Closed billdenney closed 7 years ago

billdenney commented 7 years ago

With the following data.frame, I'm having difficulty with pander formatting the resulting table. I'm also working within knitr/rmarkdown which likely has an interaction.

mytable <-
structure(list(AIC = c(-34.7459717260402, -36.0103992256577, 
-34.3998771508311, -36.63207635103, -34.7193636140829, -32.778778379272
), df = c(2L, 3L, 4L, 3L, 4L, 5L), indentation = c(0, 0, 0, 0, 
0, 0), isBest = c("", "", "", "Best Model", "", "")), .Names = c("AIC", 
"df", "indentation", "isBest"), row.names = c("E0=0, Emax=1, hill=1 (gnls model)", 
"E0=0, Emax=estimated, hill=1 (gnls model)", "E0=estimated, Emax=estimated, hill=1 (gnls model)", 
"E0=0, Emax=1, hill=estimated (gnls model)", "E0=0, Emax=estimated, hill=estimated (gnls model)", 
"E0=estimated, Emax=estimated, hill=estimated (gnls model)"), class = "data.frame")

When doing a standard pander(mytable), I get a table that then mixes pre-formatted and other info within the markdown due to the prefixed spaces.

---------------------------------------------------------------------
             &nbsp;               AIC    df   indentation    isBest  
-------------------------------- ------ ---- ------------- ----------
  **E0=0, Emax=1, hill=1 (gnls   -34.75  2         0                 
            model)**                                                 

 **E0=0, Emax=estimated, hill=1  -36.01  3         0                 
         (gnls model)**                                              

        **E0=estimated,          -34.4   4         0                 
  Emax=estimated, hill=1 (gnls                                       
            model)**                                                 

 **E0=0, Emax=1, hill=estimated  -36.63  3         0       Best Model
         (gnls model)**                                              

    **E0=0, Emax=estimated,      -34.72  4         0                 
 hill=estimated (gnls model)**                                       

        **E0=estimated,          -32.78  5         0                 
 Emax=estimated, hill=estimated                                      
         (gnls model)**                                              
---------------------------------------------------------------------

When I do pander(mytable, justify="left"), it appears to generate correct markdown, but it doesn't work with knitr/rmarkdown. When I do pander(mytable, justify="left", style="grid"), it both generates a good markdown table and works with knitr/rmarkdown.

daroczig commented 7 years ago

Not sure what's the problem, can you please add more details and screenshots? When I run pandoc on the above markdown table, I get this result, which looks OK to me:

pander in pdf

Also, what do you mean by "it doesn't work with knitr/rmarkdown"? A minimal Rmd reproducing the problem would be highly appreciated.

billdenney commented 7 years ago

With further investigation, this falls under "my fault, but maybe you can still help". :)

It turns out that the issue occurs when results="asis" and there is no blank line before the beginning of the table. File that under my fault. Perhaps a way that it can be prevented for me and other users in the future is to insert a blank line at the beginning (and end?) of the markdown created by pandoc.table?

Rename this text file to .Rmd, and it is a reproducible example.

pander289.txt

Here is a screen shot of the result:

image

daroczig commented 7 years ago

Ah, I see, thanks for the feedback -- that's actually makes sense.

So pander already adds a blank line before and after the table, see eg your example with pander_return (which shows what is written to the stdout when calling pander):

> pander_return(mytable)
 [1] ""                                                                         
 [2] "-------------------------------------------------------------------------"
 [3] "             &nbsp;                AIC     df   indentation     isBest   "
 [4] "-------------------------------- -------- ---- ------------- ------------"
 [5] "  **E0=0, Emax=1, hill=1 (gnls    -34.75   2         0                   "
 [6] "            model)**                                                     "
 [7] ""                                                                         
 [8] " **E0=0, Emax=estimated, hill=1   -36.01   3         0                   "
 [9] "         (gnls model)**                                                  "
[10] ""                                                                         
[11] "        **E0=estimated,           -34.4    4         0                   "
[12] "  Emax=estimated, hill=1 (gnls                                           "
[13] "            model)**                                                     "
[14] ""                                                                         
[15] " **E0=0, Emax=1, hill=estimated   -36.63   3         0        Best Model "
[16] "         (gnls model)**                                                  "
[17] ""                                                                         
[18] "    **E0=0, Emax=estimated,       -34.72   4         0                   "
[19] " hill=estimated (gnls model)**                                           "
[20] ""                                                                         
[21] "        **E0=estimated,           -32.78   5         0                   "
[22] " Emax=estimated, hill=estimated                                          "
[23] "         (gnls model)**                                                  "
[24] "-------------------------------------------------------------------------"
[25] ""          

So I think what you are requesting is already done for the convenience of pander users so that you do not have to add an extra line break before and after the table, but if you use cat in R, that writes to stdout without auto adding a newline -- you have to do that manually. So here calling cat and then pander means that you want to continue on the very same line, and although pander adds one line-break -- but in markdown, 2 breaks are required for a "hard break" / new paragraph.

Does it make sense? I'm closing this ticket as I don't think pander should start with two line breaks, but please feel free to reopen if you don't agree and I'm happy to discuss.