Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flextable and knitr/markdown/docx #175

Closed
HJAllen opened this issue Apr 6, 2018 · 12 comments
Closed

flextable and knitr/markdown/docx #175

HJAllen opened this issue Apr 6, 2018 · 12 comments

Comments

@HJAllen
Copy link

HJAllen commented Apr 6, 2018

flextable is a library for creating docx tables which adds word xml directly to the knitted document. I have been using pandoc-crossref in a Rstudio/rmarkdown/knitr/docx workflow with pandoc.table with great success. With flextable, there isn't the option to add a tag to the caption in a way similar to pandoc.table.
Questions:

  1. Has anyone developed a solution for use with pandoc-crossref?
  2. If not, might one be able to place the table in a
    tag similar to multi-figures or code blocks?

Below is a reproducible .Rmd snippet.

---
title: "flextableSnippet"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flextable)
```

## flextable table

```{r}
iris.t <-
  iris[1:5,] %>%
  regulartable() %>% 
  style(pr_c = officer::fp_cell(vertical.align = "bottom",
                                border.bottom = officer::fp_border(width = 2)), part = "header") %>% 
  rotate(j = names(iris)[-c(1:2)],
         rotation = "tbrl", part = "header", align = "bottom") %>% 
  height(height = max(dim_pretty(., part = "header")$widths), part = "header") %>% 
  width(width = dim_pretty(.,part = "body")$widths)%>% 
  add_footer(top = TRUE, Sepal.Length = "Sample Dates.")
iris.t
```
@HJAllen
Copy link
Author

HJAllen commented Apr 6, 2018

Upon further investigation, I see that when knitted, the flextable is written to the intermediate .md file as an openxml code chunk. When the code chunk is wrapped in <div id="fig:Label"> as described in the help for subfigures, a proper caption is produced and references are valid. This seems like a reasonable solution to me. So, the question is, can the <div> approach be expanded to include the tbl tag?

@lierdakil
Copy link
Owner

lierdakil commented Apr 6, 2018

Well... FWIW, there's #174, which doesn't really care what prefix you're using, so it should work for subfigures divs with id="tbl:label" (and it will produce the correct formatting in caption and references, of course). But it's work-in-progress and the behaviour is subject to change without advance warnings.

Apart from that... yes, definitely possible. Whether I'll find time to implement that is up in the air though -- #174 seems like a more promising direction, so it'll likely end up higher on the priority list.

@HJAllen
Copy link
Author

HJAllen commented Apr 10, 2018

Thanks for the pointer. I am using pandoc 2.1.3 and pandoc-crossref windows binary v0.3.1.0. The snippet in #84 as below. docx output does not create a reference for neither [@cl:addgp] nor [@dfn:ring] but does for the section reference [@sec:top]. I understand this is a work in progress. Is there any further documentation on this feature, or, do you see an obvious mistake in my code?

command
C:/Users/JALLEN/R/RStudio-1.1.423/bin/pandoc/pandoc test.md --to docx --from markdown --output test.docx --filter pandoc-crossref

document

---
title: Test Document
author: HJA
output:
  word_document:
    pandoc_args: [
      "--filter", "pandoc-crossref"
      ]
    keep_md: TRUE
prefixes:
  dfn:
    ref: ["dfn.", "dfns."]
    name: "Definition"
  cl:
    ref: ["cl.", "cls."]
    scope: ["dfn"]
---

# Section{#sec:top}

See test reference [@cl:addgp] or [@dfn:ring]

<div id="dfn:ring">
A _ring_ is a triple $(R,+,*)$ satisfying:

#. [$+$ is an abelian group]{#cl:addgp}
#. [$*$ is a monoid]{#cl:multmon}
#. [$*$ distributes over $+$]{#cl:distrib}
</div>


Make sure pandoc-crossref works by referencing [@sec:top]

@lierdakil
Copy link
Owner

#174 is unreleased as of yet, sorry if it wasn't clear. You could build it though if you wanted. Or I could make a tag from that, and CI would hopefully build it in a couple hours... which I might as well do, why not. Check back with https://github.com/lierdakil/pandoc-crossref/releases/tag/v0.4.0.0-alpha0 in an hour or so, there should be binaries available by then.

@HJAllen
Copy link
Author

HJAllen commented Apr 10, 2018

That's Great! v0.4.0.0-alpha0 works as advertised. For completeness, I have included the .Rmd file which demonstrates. One note, the `\n' linefeeds used in catting of the caption appear to be necessary as flextable does not append a linefeed to the end of its output nor does the get picked up.

---
title: "flextableSnippet"
output:
  word_document:
    pandoc_args: [
      "--filter", "pandoc-crossref"
      ]
    keep_md: TRUE
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flextable)
```

# flextable table

Reference to iris table [@tbl:iris]

```{r, results='asis'}
#output div
cat("<div id=\"tbl:iris\">")

iris.t <-
  iris[1:5,] %>%
  regulartable() %>% 
  style(pr_c = officer::fp_cell(vertical.align = "bottom",
                                border.bottom = officer::fp_border(width = 2)), part = "header") %>% 
  rotate(j = names(iris)[-c(1:2)],
         rotation = "tbrl", part = "header", align = "bottom") %>% 
  height(height = max(dim_pretty(.,  part = "header")$widths), part = "header") %>% 
  width(width = c(dim_pretty(. ,part = "all")$widths[c(1:2)],
                  dim_pretty(. ,part = "body")$widths[-c(1:2)]))
iris.t

#Output Caption
cat("\nIris table caption.\n")
# Output </div>
cat("</div>")

@HJAllen HJAllen closed this as completed Apr 10, 2018
@lierdakil
Copy link
Owner

I'm not familiar with RMD myself, but if it's Pandoc Markdown on the top level, I would expect you could simplify the table part to

::: {#tbl:iris}
```{r, results='asis'}
iris.t <-
  iris[1:5,] %>%
  regulartable() %>% 
  style(pr_c = officer::fp_cell(vertical.align = "bottom",
                                border.bottom = officer::fp_border(width = 2)), part = "header") %>% 
  rotate(j = names(iris)[-c(1:2)],
         rotation = "tbrl", part = "header", align = "bottom") %>% 
  height(height = max(dim_pretty(.,  part = "header")$widths), part = "header") %>% 
  width(width = c(dim_pretty(. ,part = "all")$widths[c(1:2)],
                  dim_pretty(. ,part = "body")$widths[-c(1:2)]))
iris.t
```

Iris table caption.
:::

@HJAllen
Copy link
Author

HJAllen commented Apr 10, 2018

Your last suggestion was not successful. Didn't really dig into it as the above solution is good enough for me.

@HJAllen
Copy link
Author

HJAllen commented Sep 3, 2020

It has been a while but trying to reuse the code snippet above to reference a div wrapped table but it is currently broken. Any idea why this may be?
Thanks.

@HJAllen HJAllen reopened this Sep 3, 2020
@lierdakil
Copy link
Owner

No idea, since you didn't specify what is "broken" exactly. Remember, I don't use R.

In case you've updated pandoc recently, the last v0.4.0.0 prerelease (alpha6d) is not compatible with pandoc v2.10.x. In that case, you might try a more recent build https://bintray.com/lierdakil/pandoc-crossref/any-prefix/40a8b12efce01cf46ff385875cf2d49e3cae6a91#files

@HJAllen
Copy link
Author

HJAllen commented Sep 4, 2020

Sorry, that was a really poor question...
pandoc 10.2.1
pandoc-crossref 0.3.8.1

Briefly, R will render a markdown document

---
title: "flextableSnippet"
output:
  word_document:
    pandoc_args: [
      "--filter", "pandoc-crossref"
      ]
    keep_md: TRUE
editor_options: 
  chunk_output_type: console
---

# pander table

Reference to iris table [@tbl:piris]

\footnotesize

Sepal.Length   Sepal.Width  
-------------- -------------
5.1            3.5          

Table: pander Iris table caption {#tbl:piris}

\normalsize

# flextable table

Reference to iris table [@tbl:iris]

<div id="tbl:iris">
```{=openxml}
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" ><w:tblPr>NA"/><w:tblLayout w:type="fixed"/><w:jc w:val="center"/><w:tblW w:type="dxa" w:w="3016"/><w:tblLook w:firstRow="1" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="1563"/><w:gridCol w:w="1453"/></w:tblGrid><w:tr><w:trPr><w:cantSplit/><w:trHeight w:val="1563" w:hRule="auto"/><w:tblHeader/></w:trPr><w:tc><w:tcPr><w:tcBorders><w:bottom w:val="single" w:sz="16" w:space="0" w:color="000000" /></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/><w:tcMar><w:top w:w="0" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:left w:w="0" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tcMar><w:vAlign w:val="bottom"/></w:tcPr><w:p><w:pPr><w:jc w:val="right"/><w:spacing w:after="40" w:before="40"/><w:ind w:firstLine="0" w:left="100" w:right="100"/></w:pPr><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsia="Arial" w:cs="Arial"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:color w:val="111111"/></w:rPr><w:t xml:space="preserve">Sepal.Length</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcBorders><w:bottom w:val="single" w:sz="16" w:space="0" w:color="000000" /></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/><w:tcMar><w:top w:w="0" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:left w:w="0" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tcMar><w:vAlign w:val="bottom"/></w:tcPr><w:p><w:pPr><w:jc w:val="right"/><w:spacing w:after="40" w:before="40"/><w:ind w:firstLine="0" w:left="100" w:right="100"/></w:pPr><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsia="Arial" w:cs="Arial"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:color w:val="111111"/></w:rPr><w:t xml:space="preserve">Sepal.Width</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:trPr><w:cantSplit/><w:trHeight w:val="360" w:hRule="auto"/></w:trPr><w:tc><w:tcPr><w:tcBorders><w:bottom w:val="single" w:sz="16" w:space="0" w:color="000000" /></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/><w:tcMar><w:top w:w="0" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:left w:w="0" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:jc w:val="right"/><w:spacing w:after="40" w:before="40"/><w:ind w:firstLine="0" w:left="100" w:right="100"/></w:pPr><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsia="Arial" w:cs="Arial"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:color w:val="111111"/></w:rPr><w:t xml:space="preserve">5.1</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcBorders><w:bottom w:val="single" w:sz="16" w:space="0" w:color="000000" /></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/><w:tcMar><w:top w:w="0" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:left w:w="0" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:jc w:val="right"/><w:spacing w:after="40" w:before="40"/><w:ind w:firstLine="0" w:left="100" w:right="100"/></w:pPr><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsia="Arial" w:cs="Arial"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:color w:val="111111"/></w:rPr><w:t xml:space="preserve">3.5</w:t></w:r></w:p></w:tc></w:tr></w:tbl>
```
div wrapped Iris table caption.
</div>

Then submit to pandoc with following command:
"C:/Users/JALLEN/R/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to docx --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.docx --highlight-style tango --lua-filter "C:/Users/JALLEN/R/library/rmarkdown/rmd/lua/pagebreak.lua" --filter pandoc-crossref

Output

Undefined cross-reference: tbl:iris

Output created: test.docx

So, crossref is able to process the markdown style table but not the div wrapped table. The produced docx contains properly produced tables but only the markdown style table results in the properly formatted reference.

@lierdakil
Copy link
Owner

Right. So, looking at the previous discussion here, I believe we established that pandoc-crossref 0.3.x doesn't really handle this, it really wants to see a table being referenced as tbl:* and doesn't recognize raw ooxml block as a table. Not a surprise that pandoc-crossref 0.3.8.1 doesn't handle this case.

Try getting the latest prerelease build of v0.4.0.0 (find it here), that should (hopefully) work. Or checkout the top of any-prefix branch on this repo if you want to build from source.

If you're not keen on using prerelease software, we can return to the idea of referencing arbitrary divs with current mainline release, but I can't promise I will implement anything in v0.3.x in a timely fashion.

@HJAllen
Copy link
Author

HJAllen commented Sep 4, 2020

Aha, yes it had been so long I forgot about your comment re v0.4 and missed it this time. I will try again. Thanks!

@HJAllen HJAllen closed this as completed Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants