Export pivot table into R markdown (for final output in word)

Viewed 355

I would like help about pivot table (from pivottabler) and export into word document (by Rmarkdown step).

In my daily work, I export my tables in Excel file, but for the annual report I would like to export the summary tables in word document automatically and keeping the format. I tryed to export in html and latex or in converting pivottable values into dataframe but didn't work.

Could someone help me please? Either show me the technique with this package, or tell me with which other package easily exportable in R markdown I could easily reproduce (and identically) my tables ?

library(pivottabler)
pt <- PivotTable$new()
pt$addData(bhmtrains)
pt$addColumnDataGroups("TrainCategory")
pt$addRowDataGroups("TOC", 
                    outlineBefore=list(isEmpty=FALSE, 
                                       groupStyleDeclarations=list(color="blue")), 
                    outlineTotal=list(groupStyleDeclarations=list(color="blue")))
pt$addRowDataGroups("PowerType", addTotal=FALSE)
pt$addRowDataGroups("SchedSpeedMPH", addTotal=FALSE)
pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
pt$renderPivot()

enter image description here

1 Answers

There is a tutorial on using R Markdown to Word here: https://rmarkdown.rstudio.com/articles_docx.html

An example markdown file producing Word output:

---
title: "Example Output to Word"
output: word_document
---

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

## Example Pivot Table

```{r}
library(pivottabler)
pt <- PivotTable$new()
pt$addData(bhmtrains)
pt$addColumnDataGroups("TrainCategory")
pt$addRowDataGroups("TOC", 
                    outlineBefore=list(isEmpty=FALSE, 
                                       groupStyleDeclarations=list(color="blue")), 
                    outlineTotal=list(groupStyleDeclarations=list(color="blue")))
pt$addRowDataGroups("PowerType", addTotal=FALSE)
pt$addRowDataGroups("SchedSpeedMPH", addTotal=FALSE)
pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
pt$renderPivot()
```
Related