library(testthat) # most common expectations: # equality: expect_equal() and expect_identical() # regexp: expect_match() # catch-all: expect_true() and expect_false() # console output: expect_output() # messages: expect_message() # warning: expect_warning() # errors: expect_error() escapeString <- function(s) { t <- gsub("(\\\\)", "\\\\\\\\", s) t <- gsub("(\n)", "\\\\n", t) t <- gsub("(\r)", "\\\\r", t) t <- gsub("(\")", "\\\\\"", t) return(t) } prepStr <- function(s) { t <- escapeString(s) u <- eval(parse(text=paste0("\"", t, "\""))) if(s!=u) stop("Unable to escape string!") t <- paste0("\thtml <- \"", t, "\"") utils::writeClipboard(t) # clipr::write_clip(t) return(invisible()) } evaluationMode <- "sequential" processingLibrary <- "dplyr" description <- "test: sequential dplyr" countFunction <- "n()" isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) testScenarios <- function(description="test", releaseEvaluationMode="batch", releaseProcessingLibrary="dplyr", runAllForReleaseVersion=FALSE) { isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) if(isDevelopmentVersion||runAllForReleaseVersion) { evaluationModes <- c("sequential", "batch") processingLibraries <- c("dplyr", "data.table") } else { evaluationModes <- releaseEvaluationMode processingLibraries <- releaseProcessingLibrary } testCount <- length(evaluationModes)*length(processingLibraries) c1 <- character(testCount) c2 <- character(testCount) c3 <- character(testCount) c4 <- character(testCount) testCount <- 0 for(evaluationMode in evaluationModes) for(processingLibrary in processingLibraries) { testCount <- testCount + 1 c1[testCount] <- evaluationMode c2[testCount] <- processingLibrary c3[testCount] <- paste0(description, ": ", evaluationMode, " ", processingLibrary) c4[testCount] <- ifelse(processingLibrary=="data.table", ".N", "n()") } df <- data.frame(evaluationMode=c1, processingLibrary=c2, description=c3, countFunction=c4, stringsAsFactors=FALSE) return(df) } context("THEMING TESTS") scenarios <- testScenarios("theming tests: legacy test") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { # define the colours orangeColors <- list( headerBackgroundColor = "rgb(237, 125, 49)", headerColor = "rgb(255, 255, 255)", cellBackgroundColor = "rgb(255, 255, 255)", cellColor = "rgb(0, 0, 0)", totalBackgroundColor = "rgb(248, 198, 165)", totalColor = "rgb(0, 0, 0)", borderColor = "rgb(198, 89, 17)" ) # create the pivot table library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$theme <- getSimpleColoredTheme(parentPivot=pt, colors=orangeColors, fontName="Garamond, arial") pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) # prepStr(as.character(pt$getCss())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" css <- ".Table {display: table; border-collapse: collapse; border: 2px solid rgb(198, 89, 17); }\r\n.ColumnHeader {font-family: Garamond, arial; font-size: 0.75em; padding: 2px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: center; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(237, 125, 49); }\r\n.RowHeader {font-family: Garamond, arial; font-size: 0.75em; padding: 2px 8px 2px 2px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: left; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(237, 125, 49); }\r\n.Cell {font-family: Garamond, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); }\r\n.OutlineColumnHeader {font-family: Garamond, arial; font-size: 0.75em; padding: 2px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: center; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(237, 125, 49); }\r\n.OutlineRowHeader {font-family: Garamond, arial; font-size: 0.75em; padding: 2px 8px 2px 2px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: left; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(237, 125, 49); }\r\n.OutlineCell {font-family: Garamond, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-weight: bold; }\r\n.Total {font-family: Garamond, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(198, 89, 17); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(248, 198, 165); }\r\n" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) expect_identical(pt$getCss(), css) }) } scenarios <- testScenarios("theming tests: basic test") for(i in 1:nrow(scenarios)) { evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { # define the theme simpleGreenTheme <- list( fontName="Helvetica, arial", fontSize="0.75em", headerBackgroundColor = "rgb(112, 173, 71)", headerColor = "rgb(255, 255, 255)", cellBackgroundColor="rgb(255, 255, 255)", cellColor="rgb(0, 0, 0)", outlineCellBackgroundColor = "rgb(182, 216, 158)", outlineCellColor = "rgb(0, 0, 0)", totalBackgroundColor = "rgb(182, 216, 158)", totalColor="rgb(0, 0, 0)", borderColor = "rgb(84, 130, 53)" ) # create the pivot table library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$theme <- simpleGreenTheme pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) # prepStr(as.character(pt$getCss())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" css <- ".Table {display: table; border-collapse: collapse; border: 2px solid rgb(84, 130, 53); }\r\n.ColumnHeader {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: center; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(112, 173, 71); }\r\n.RowHeader {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px 8px 2px 2px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: left; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(112, 173, 71); }\r\n.Cell {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); }\r\n.OutlineColumnHeader {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: center; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(112, 173, 71); }\r\n.OutlineRowHeader {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px 8px 2px 2px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: left; font-weight: bold; color: rgb(255, 255, 255); background-color: rgb(112, 173, 71); }\r\n.OutlineCell {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(182, 216, 158); font-weight: bold; }\r\n.Total {font-family: Helvetica, arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid rgb(84, 130, 53); vertical-align: middle; text-align: right; color: rgb(0, 0, 0); background-color: rgb(182, 216, 158); }\r\n" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) expect_identical(pt$getCss(), css) }) } scenarios <- testScenarios("theming tests: styling data groups") for(i in 1:nrow(scenarios)) { evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory", styleDeclarations=list("color"="red", "font-weight"="bold", "background-color"="yellow")) pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) # prepStr(as.character(pt$getCss())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" css <- ".Table {display: table; border-collapse: collapse; }\r\n.ColumnHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px; text-align: center; }\r\n.RowHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px 8px 2px 2px; text-align: left; }\r\n.Cell {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; }\r\n.OutlineColumnHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px; text-align: center; }\r\n.OutlineRowHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px 8px 2px 2px; text-align: left; }\r\n.OutlineCell {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; background-color: #F8F8F8; font-weight: bold; }\r\n.Total {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; }\r\n" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) expect_identical(pt$getCss(), css) }) } scenarios <- testScenarios("theming tests: styling cells") for(i in 1:nrow(scenarios)) { evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$styles$addStyle(styleName="NewHeadingStyle1", list( "font-family"="Arial", "font-size"="0.75em", padding="2px", border="1px solid lightgray", "vertical-align"="middle", "text-align"="center", "font-weight"="bold", "background-color"="Gold", "xl-wrap-text"="wrap" )) pt$styles$addStyle(styleName="CellStyle1", list( "font-family"="Arial", "font-size"="0.75em", padding="2px 2px 2px 8px", border="1px solid lightgray", "vertical-align"="middle", "background-color"="Yellow", "text-align"="right" )) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains1", summariseExpression=countFunction, headingBaseStyleName="NewHeadingStyle1", cellBaseStyleName="CellStyle1") pt$defineCalculation(calculationName="TotalTrains2", summariseExpression=countFunction, headingStyleDeclarations=list("color"="red", "font-weight"="bold"), cellStyleDeclarations=list("color"="blue")) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) # prepStr(as.character(pt$getCss())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
TotalTrains1TotalTrains2TotalTrains1TotalTrains2TotalTrains1TotalTrains2
Arriva Trains Wales3079307983083039093909
CrossCountry228652286563632292822928
London Midland144871448733792337924827948279
Virgin Trains8594859485948594
Total490254902534685346858371083710
" css <- ".Table {display: table; border-collapse: collapse; }\r\n.ColumnHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px; text-align: center; }\r\n.RowHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px 8px 2px 2px; text-align: left; }\r\n.Cell {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; }\r\n.OutlineColumnHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px; text-align: center; }\r\n.OutlineRowHeader {font-family: Arial; font-size: 0.75em; border: 1px solid lightgray; vertical-align: middle; font-weight: bold; background-color: #F2F2F2; padding: 2px 8px 2px 2px; text-align: left; }\r\n.OutlineCell {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; background-color: #F8F8F8; font-weight: bold; }\r\n.Total {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; text-align: right; }\r\n.NewHeadingStyle1 {font-family: Arial; font-size: 0.75em; padding: 2px; border: 1px solid lightgray; vertical-align: middle; text-align: center; font-weight: bold; background-color: Gold; }\r\n.CellStyle1 {font-family: Arial; font-size: 0.75em; padding: 2px 2px 2px 8px; border: 1px solid lightgray; vertical-align: middle; background-color: Yellow; text-align: right; }\r\n" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 669680) expect_identical(as.character(pt$getHtml()), html) expect_identical(pt$getCss(), css) }) } scenarios <- testScenarios("theming tests: styling basics 1") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addColumnDataGroups("Status") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$setStyling(rowNumbers=2:3, declarations=list("background-color"="yellow")) pt$setStyling(rFrom=4, rTo=5, columnNumbers=5:7, declarations=list("background-color"="pink")) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
ACRTotalACRTotal 
Arriva Trains Wales30185923079815158303909
CrossCountry22270569262286560216322928
London Midland14133336181448732851914273379248279
Virgin Trains8359226985948594
Total477801190554902533726931283468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: styling basics 2") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addColumnDataGroups("Status") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$setStyling(rowNumbers=c(2, 4), columnNumbers=c(1, 3), declarations=list("background-color"="pink")) pt$setStyling(columnNumbers=5:7, declarations=list("background-color"="yellow")) pt$setStyling(rFrom=1, rTo=2, cFrom=8, cTo=9, declarations=list("background-color"="lightgreen")) pt$setStyling(cells=pt$getCell(5, 9), declarations=list("background-color"="lightblue")) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
ACRTotalACRTotal 
Arriva Trains Wales30185923079815158303909
CrossCountry22270569262286560216322928
London Midland14133336181448732851914273379248279
Virgin Trains8359226985948594
Total477801190554902533726931283468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: applying styling multiple times to the same cell", runAllForReleaseVersion=TRUE) for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(totalStyleIsCellStyle=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() grps <- pt$rowGroup$childGroups pt$setStyling(groups=grps, declarations=list("font-weight"="normal")) pt$setStyling(groups=grps, declarations=list("color"="blue")) cells <- pt$getCells(rowNumbers=4) pt$setStyling(cells=cells, declarations=list("font-weight"="bold")) pt$setStyling(cells=cells, declarations=list("color"="green")) pt$setStyling(2, 1, declarations=list("color"="red")) pt$setStyling(2, 1, declarations=list("font-weight"="bold")) # pt$renderPivot() # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: applying styling with applyBorderToAdjacentCells", runAllForReleaseVersion=FALSE) for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$setStyling(rowNumbers=3, columnNumbers=2, declarations=list("border"="1px solid red"), applyBorderToAdjacentCells=TRUE) # pt$renderPivot() # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: inheriting cell style names from row groups") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) style <- pt$styles$copyStyle(styleName="Cell", newStyleName="RedCell") style$setPropertyValue(property="color", value="red") style <- pt$styles$copyStyle(styleName="Cell", newStyleName="BlueCell") style$setPropertyValue(property="color", value="blue") pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") grps <- pt$addRowDataGroups("TOC") grps[[2]]$cellBaseStyleName <- "RedCell" grps <- pt$addRowDataGroups("PowerType") # D/EMU = Diesel/Electric Multiple Unit, HST=High Speed Train grps[[4]]$cellBaseStyleName <- "BlueCell" pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
Total30798303909
CrossCountryDMU221336322196
HST732732
Total228656322928
London MidlandDMU5638559111229
EMU88492820137050
Total144873379248279
Virgin TrainsDMU21372137
EMU64576457
Total85948594
Total 490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: inheriting cell style declarations from row groups") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") grps <- pt$addRowDataGroups("TOC") grps[[2]]$cellStyle <- list("color"="red") grps <- pt$addRowDataGroups("PowerType") # D/EMU = Diesel/Electric Multiple Unit, HST=High Speed Train grps[[4]]$cellStyle <- list("color"="blue") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
Total30798303909
CrossCountryDMU221336322196
HST732732
Total228656322928
London MidlandDMU5638559111229
EMU88492820137050
Total144873379248279
Virgin TrainsDMU21372137
EMU64576457
Total85948594
Total 490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("theming tests: intersecting styles from rows, columns, calcs and cells") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") cgrps <- pt$addColumnDataGroups("PowerType") colorText <- function(grp) { if(isTRUE(grp$values=="DMU")) grp$cellStyle <- list(color="blue") else if(isTRUE(grp$values=="EMU")) grp$cellStyle <- list(color="green") else if(isTRUE(grp$values=="HST")) grp$cellStyle <- list(color="red") } invisible(lapply(cgrps, colorText)) rgrps <- pt$addRowDataGroups("TOC", atLevel=1, addTotal=FALSE) colorText <- function(grp) { if(isTRUE(grp$values=="Arriva Trains Wales")) grp$cellStyle <- list("background-color"="aliceblue") else if(isTRUE(grp$values=="CrossCountry")) grp$cellStyle <- list("background-color"="cornsilk") else if(isTRUE(grp$values=="London Midland")) grp$cellStyle <- list("background-color"="lightgreen") else if(isTRUE(grp$values=="Virgin Trains")) grp$cellStyle <- list("background-color"="lavenderblush") } invisible(lapply(rgrps, colorText)) pt$addRowGroup(caption="Total", isOutline=TRUE, isTotal=TRUE, isLevelTotal=TRUE, styleDeclarations=list(color="blue"), cellStyleDeclarations=list("background-color"="plum")) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction, caption="Train Count", format = "%.0f", cellStyleDeclarations=list("font-family"="serif", "font-style"="italic")) pt$defineCalculation(calculationName="AvgSchSpeed", summariseExpression="mean(SchedSpeedMPH, na.rm=TRUE)", caption="Avg Speed", format = "%.0f", cellStyleDeclarations=list("font-family"="Lucida Console", "xl-value-format"="#,##0")) pt$addRowCalculationGroups() pt$evaluatePivot() pt$setStyling(rFrom=4, rTo=5, cFrom=6, cTo=8, declarations=list("background-color"="yellow", "font-weight"="bold")) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
DMUEMUHSTTotalDMUEMUTotal 
Arriva Trains WalesTrain Count307930798308303909
Avg Speed9090898990
CrossCountryTrain Count2213373222865636322928
Avg Speed113125113100100113
London MidlandTrain Count56388849144875591282013379248279
Avg Speed921019876949193
Virgin TrainsTrain Count2137645785948594
Avg Speed125125125125
TotalTrain Count3298715306732490256484282013468583710
Avg Speed108111125109789491101
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 505332.016147847) expect_identical(as.character(pt$getHtml()), html) }) }