tables in pander, style="multiline"
Asked Answered
F

1

8

I'm using RStudio 0.98.1056 on Windows 7, and whatever the current version of pander is on CRAN as of today (I just installed the package).

I'm trying to use the knitr->Markdown->.docx literate-programming workflow to create a table in Word. Some of the cells need to contain hard line breaks. I believe this should be possible based on info in the following link:

http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html

It says, "A backslash followed by a newline is also a hard line break. Note: in multiline and grid table cells, this is the only way to create a hard line break, since trailing spaces in the cells are ignored."

I'm trying to create a table with cells containing line breaks in the following way:

library(pander)
a <- c(1:10)
b <- c(11:20)
ab <- paste(a,b,sep='\n')
N <- ceiling(rpois(10,9))
labels <- paste("Question",c(1:10))
mytable <- data.frame(labels,ab,N)
pandoc.table(mytable, style="multiline")

But the newline just gets stripped. Same if I use sep="< newline>" or sep="< br>" (without leading spaces). I have also tried adding an extra backslash, in case R was "eating" the first one; and switching up the "paste" function to include each of these possible separators as a term in their own right, instead of in the sep= argument. None of these things worked.

I went to try out the example code in the pander help documentation, just as a sanity check, and it seems the issue is general -- sample code that I thought should produce multiline tables did not, on my machine.

Has anyone else gotten multiline tables in pander to work?

Febrile answered 15/9, 2014 at 21:55 Comment(0)
B
7

Yup, please install the development version from GitHub -- this issue was fixed a few months ago. E.g.:

> panderOptions('keep.line.breaks', TRUE)
> pander(mytable)

--------------------
labels      ab   N  
----------- ---- ---
Question 1  1    11 
            11      

Question 2  2    6  
            12      

Question 3  3    13 
            13      

Question 4  4    12 
            14      

Question 5  5    6  
            15      

Question 6  6    6  
            16      

Question 7  7    11 
            17      

Question 8  8    19 
            18      

Question 9  9    10 
            19      

Question 10 10   9  
            20      
--------------------
Breaststroke answered 15/9, 2014 at 22:12 Comment(3)
Thanks for this. I got the devel version of pander installed (after resolving some issues with Rtools), and when I run pander(mytable) I get the same output you saw in R. On the other hand, that output doesn't knit properly to either HTML or docx: the linebreaks are removed. What am I missing? I'm still using pandoc.table(mytable, style="multiline") instead of the simpler syntax, so that's not the issue. I've also tried with style="grid": no dice.Febrile
@ErinMcJ that's a Pandoc issue. Quote from its documentation: "A backslash followed by a newline is also a hard line break. Note: in multiline and grid table cells, this is the only way to create a hard line break, since trailing spaces in the cells are ignored." So you not only need to add the manual line breaks (\n), but also a hard line break. See a demo by running pander(descr::CrossTable(mtcars$am, mtcars$gear), split.table = Inf)Breaststroke
Aha! Yes! I was really confused about what a hard line break is, if it isn't the backslash-n sequence. But it turns out that in order for \\n to render appropriately, I needed an extra backslash as an escape character: so, \\\n.Febrile

© 2022 - 2024 — McMap. All rights reserved.