The pivottabler package can export a pivot table to an Excel file. Only Excel 2007 onwards (*.xlsx) files are supported. This export uses the openxlsx package.
This vignette starts with a basic (unformatted) export. Following this are various styled/formatted examples.
Exporting a pivot table to an Excel table is reasonably straightforward:
pivottabler
,writeToExcelWorksheet
method on the pivot table.library(pivottabler)
pt <- PivotTable$new()
pt$addData(bhmtrains)
pt$addColumnDataGroups("TrainCategory")
pt$addColumnDataGroups("PowerType")
pt$addRowDataGroups("TOC")
pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
pt$evaluatePivot()
library(openxlsx)
wb <- createWorkbook(creator = Sys.getenv("USERNAME"))
addWorksheet(wb, "Data")
pt$writeToExcelWorksheet(wb=wb, wsName="Data",
topRowNumber=1, leftMostColumnNumber=1, applyStyles=FALSE)
saveWorkbook(wb, file="C:\\test.xlsx", overwrite = TRUE)
Basic Excel Export (unstyled)
The Excel output from the pivottabler
package has been written so that, as much as possible, the same styling used for HTML output also works for the Excel output, i.e. most of the styling described in the Styling vignette can also be used when writing a pivot table to an Excel file.
More specifically, the styling described in the Styling vignette uses CSS (Cascading Style Sheet) definitions for styles. The pivottabler
package interprets these CSS definitions and maps them to the styling used in Excel/by the openxlsx package.
This means, once a pivot table has been styled as described in the Styling vignette, the pivot table in the Excel workbook can be styled simply by specifying applyStyles=TRUE
.
library(pivottabler)
pt <- PivotTable$new()
pt$addData(bhmtrains)
pt$addColumnDataGroups("TrainCategory")
pt$addColumnDataGroups("PowerType")
pt$addRowDataGroups("TOC")
pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
pt$evaluatePivot()
library(openxlsx)
wb <- createWorkbook(creator = Sys.getenv("USERNAME"))
addWorksheet(wb, "Data")
pt$writeToExcelWorksheet(wb=wb, wsName="Data",
topRowNumber=1, leftMostColumnNumber=1,
applyStyles=TRUE, mapStylesFromCSS=TRUE)
saveWorkbook(wb, file="C:\\test.xlsx", overwrite = TRUE)
Using CSS Styling
In general, the CSS mappings described above will simplify outputting to Excel. However, not all CSS definitions can be mapped to Excel. Excel also has some style settings that don’t map to CSS. There may also be occasions where different styling is desired in Excel vs. HTML. To support all of these scenarios, a second set of styling properties are also supported. These all begin with “xl-” and have roughly similar (but not exactly the same) names to their CSS counterparts, e.g. the property corresponding to CSS “font-family” is “xl-font-name”. If both the “xl-…” Excel property and the CSS property are specified, the Excel value is used. If mapStylesFromCSS=FALSE
is specified, then the CSS properties are ignored and only the “xl-…” properties are used.
The table at the bottom of this vignette details the full set of CSS and Excel style properties that are supported.
There are a few different ways for format the values written into the worksheet. These are controlled by the outputValuesAs
parameter. The following pivot table will be used to illustrate the different values for this parameter:
library(pivottabler)
library(dplyr)
library(lubridate)
# derive some additional data
trains <- mutate(bhmtrains,
ArrivalDelta=difftime(ActualArrival, GbttArrival, units="mins"),
ArrivalDelay=ifelse(ArrivalDelta<0, 0, ArrivalDelta))
# create the pivot table
pt <- PivotTable$new()
pt$addData(trains)
pt$addColumnDataGroups("TrainCategory")
pt$addColumnDataGroups("PowerType")
pt$addRowDataGroups("TOC", totalCaption="All TOCs")
pt$defineCalculation(calculationName="MeanArrivalDelay", caption="Mean Arr. Delay",
summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format="%.1f")
pt$renderPivot()
Specifying outputValuesAs="rawValue"
will output the raw unformatted values. This is also the default if no value is explicitly specified for the outputValuesAs
parameter.
library(pivottabler)
library(dplyr)
library(lubridate)
# derive some additional data
trains <- mutate(bhmtrains,
ArrivalDelta=difftime(ActualArrival, GbttArrival, units="mins"),
ArrivalDelay=ifelse(ArrivalDelta<0, 0, ArrivalDelta))
# create the pivot table
pt <- PivotTable$new()
pt$addData(trains)
pt$addColumnDataGroups("TrainCategory")
pt$addColumnDataGroups("PowerType")
pt$addRowDataGroups("TOC", totalCaption="All TOCs")
pt$defineCalculation(calculationName="MeanArrivalDelay", caption="Mean Arr. Delay",
summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format="%.1f")
pt$evaluatePivot()
library(openxlsx)
wb <- createWorkbook(creator = Sys.getenv("USERNAME"))
addWorksheet(wb, "Data")
pt$writeToExcelWorksheet(wb=wb, wsName="Data",
topRowNumber=1, leftMostColumnNumber=1,
outputValuesAs="rawValue",
applyStyles=TRUE, mapStylesFromCSS=TRUE)
saveWorkbook(wb, file="C:\\test.xlsx", overwrite = TRUE)
Rounding the values as part of the calculation definition is one option that could be used with outputValuesAs="rawValue"
.
Exporting raw unformatted values
Specifying outputValuesAs="formattedValueAsText"
will output the formatted values - i.e. the calculation results that have been formatted according to the format
parameter in the defineCalculation()
function. The formatted values are text however, so when exported to Excel this typically results in a warning in the corner of each cell that the number in the cell has been stored as text:
Exporting formatted values as text
Specifying outputValuesAs="formattedValueAsNumber"
will output the formatted values as numbers - i.e. the same values as shown in the screen shot above, but converted back to numerical values (where possible) - so eliminating the warnings shown above:
Exporting formatted values as numbers
The outputValuesAs
parameter provides a simple way to control value formatting. However, this applies to every cell in the pivot table, so is not a very fine grained control.
Another option is to output the raw unformatted values to Excel and then specify an Excel format string to allow Excel to format the values. The Excel format string is specified in the styling of cells, either as part of the theme (but this leaves little flexibility for different calculations) or more flexibly, by adding format codes to individual cells / groups of cells after the pivot table has been populated. To do this, a new style is applied to each cell, based on an existing style in the theme. In the example below, the new style is based on the existing “Cell” style, but with a value format string applied.
library(pivottabler)
library(dplyr)
library(lubridate)
# derive some additional data
trains <- mutate(bhmtrains,
ArrivalDelta=difftime(ActualArrival, GbttArrival, units="mins"),
ArrivalDelay=ifelse(ArrivalDelta<0, 0, ArrivalDelta))
# create the pivot table
pt <- PivotTable$new()
pt$addData(trains)
pt$addColumnDataGroups("TrainCategory")
pt$addColumnDataGroups("PowerType")
pt$addRowDataGroups("TOC", totalCaption="All TOCs")
pt$defineCalculation(calculationName="MeanArrivalDelay", caption="Mean Arr. Delay",
summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format="%.1f")
pt$evaluatePivot()
# style setting function
setStyle <- function(cell, baseStyleName, declarations) {
if(is.null(cell$style))
cell$style <- pt$createInlineStyle(baseStyleName=baseStyleName, declarations=declarations)
else cell$style$setPropertyValues(declarations=declarations)
}
# set the styling on the cells
cells <- pt$findCells(calculationNames="MeanArrivalDelay")
invisible(lapply(cells, setStyle, baseStyleName="Cell", declarations=list("xl-value-format"="##0.0")))
library(openxlsx)
wb <- createWorkbook(creator = Sys.getenv("USERNAME"))
addWorksheet(wb, "Data")
pt$writeToExcelWorksheet(wb=wb, wsName="Data",
topRowNumber=1, leftMostColumnNumber=1,
outputValuesAs="rawValue",
applyStyles=TRUE, mapStylesFromCSS=TRUE)
saveWorkbook(wb, file="C:\\test.xlsx", overwrite = TRUE)
Exporting raw values with an Excel format string
It is possible to specifying a minimum row height and/or column width as part of the styling. The relevant styling properties are “xl-min-row-height” and “xl-min-column-width”.
Rows/columns are sized to meet all of the minimum sizes specified. E.g. if three cells in the same row have minimum row heights of 40, 45 and 50 specified, the row height will be set to 50.
Creating Excel files is relatively effort intensive. Outputing pivot tables to Excel files requires more time than creating a HTML representation of a pivot table. In order of increasing time required:
The following table details the styling properties that are supported.
CSS Property | XL Property | XL Example | Notes |
---|---|---|---|
font-family | xl-font-name | Arial | Only the first CSS font is used in Excel. |
font-size | xl-font-size | 12 | In Points (4-72). See below for CSS units. |
font-weight | xl-bold | normal or bold | XL bold is CSS font-weight >= 600. |
font-style | xl-italic | normal or italic | italic and oblique map to italic. |
text-decoration | xl-underline | normal or underline | |
text-decoration | xl-strikethrough | normal or strikethrough | |
background-color | xl-fill-color | #FF0000 | See below for supported CSS colours. |
color | xl-text-color | #00FF00 | See below for supported CSS colours. |
text-align | xl-h-align | left or center or right | |
vertical-align | xl-v-align | top or middle or bottom | |
white-space | xl-wrap-text | normal or wrap | |
xl-text-rotation | 90 | 0 to 359, or 255 for vertical text. | |
xl-indent | 20 | 0 to 250. | |
border | xl-border | thin black | See below for supported CSS border values. |
border-left | xl-border-left | thin black | See below for supported CSS border values. |
border-right | xl-border-right | thin black | See below for supported CSS border values. |
border-top | xl-border-top | thin black | See below for supported CSS border values. |
border-bottom | xl-border-bottom | thin black | See below for supported CSS border values. |
xl-min-column-width | 50 | 0 to 255. | |
xl-min-row-height | 45 | 0 to 400. | |
xl-value-format | #,###.00 | See notes below for full details. |
Notes:
Note that the following CSS properties are NOT supported:
The full set of vignettes is: