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) 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("CALCULATION CUSTOM FORMATTING TESTS") if (requireNamespace("lubridate", quietly = TRUE)) { scenarios <- testScenarios("calculation tests: formatting using base::sprintf() and base::format()") 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) 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(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(trains) pt$addRowDataGroups("TOC", totalCaption="All TOCs") pt$defineCalculation(calculationName="TotalTrains", caption="Total Trains", summariseExpression=countFunction) pt$defineCalculation(calculationName="MeanArrivalDelay1", caption="Mean Arr. Delay 1", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format="%.2f") pt$defineCalculation(calculationName="MeanArrivalDelay2", caption="Mean Arr. Delay 2", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format=list(digits=2, nsmall=2)) pt$evaluatePivot() # pt$renderPivot() # sprintf("%.6f", sum(pt$cells$asMatrix())) # 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
 Total TrainsMean Arr. Delay 1Mean Arr. Delay 2
Arriva Trains Wales39092.302.30
CrossCountry229283.523.52
London Midland482792.272.27
Virgin Trains85942.982.98
All TOCs837102.712.71
" expect_equal(sum(pt$cells$asMatrix()), 167447.538966) expect_identical(as.character(pt$getHtml()), html) }) } } if (requireNamespace("lubridate", quietly = TRUE)) { scenarios <- testScenarios("calculation tests: formatting using simple custom format function") 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) library(dplyr) library(lubridate) # derive some additional data trains <- mutate(bhmtrains, ArrivalDelta=difftime(ActualArrival, GbttArrival, units="mins"), ArrivalDelay=ifelse(ArrivalDelta<0, 0, ArrivalDelta)) # custom format function fmtAddComment <- function(x) { formattedNumber <- sprintf("%.1f", x) comment <- "-" if (x < 2.95) comment <- "Below 3: " else if ((2.95 <= x) && (x < 3.05)) comment <- "Equals 3: " else if (x >= 3.05) comment <- "Over 3: " return(paste0(comment, " ", formattedNumber)) } # create the pivot table pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(trains) pt$addRowDataGroups("TOC", totalCaption="All TOCs") pt$defineCalculation(calculationName="TotalTrains", caption="Total Trains", summariseExpression=countFunction) pt$defineCalculation(calculationName="MeanArrivalDelay1", caption="Mean Arr. Delay 1", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format="%.1f") pt$defineCalculation(calculationName="MeanArrivalDelay2", caption="Mean Arr. Delay 2", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format=fmtAddComment) pt$evaluatePivot() # pt$renderPivot() # sprintf("%.6f", sum(pt$cells$asMatrix())) # 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
 Total TrainsMean Arr. Delay 1Mean Arr. Delay 2
Arriva Trains Wales39092.3Below 3: 2.3
CrossCountry229283.5Over 3: 3.5
London Midland482792.3Below 3: 2.3
Virgin Trains85943.0Equals 3: 3.0
All TOCs837102.7Below 3: 2.7
" expect_equal(sum(pt$cells$asMatrix()), 167447.538966) expect_identical(as.character(pt$getHtml()), html) }) } } if (requireNamespace("lubridate", quietly = TRUE)) { scenarios <- testScenarios("calculation tests: formatting using simple custom format function with params") 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, { skip_on_cran() 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)) # custom format function fmtNumDP <- function(x, numDP) { formatCode <- paste0("%.", numDP, "f") formattedNumber <- sprintf(formatCode, x) return(formattedNumber) } # create the pivot table pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(trains) pt$addRowDataGroups("TOC", totalCaption="All TOCs") pt$defineCalculation(calculationName="TotalTrains", caption="Total Trains", summariseExpression=countFunction) # define calculations - note the use of the same custom format function (fmtNumDP) but specifying different decimal places pt$defineCalculation(calculationName="MeanArrivalDelay1", caption="Mean Arr. Delay 1", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format=fmtNumDP, fmtFuncArgs=list(numDP=1)) pt$defineCalculation(calculationName="MeanArrivalDelay2", caption="Mean Arr. Delay 2", summariseExpression="mean(ArrivalDelay, na.rm=TRUE)", format=fmtNumDP, fmtFuncArgs=list(numDP=2)) pt$evaluatePivot() # pt$renderPivot() # sprintf("%.6f", sum(pt$cells$asMatrix())) # 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
 Total TrainsMean Arr. Delay 1Mean Arr. Delay 2
Arriva Trains Wales39092.32.30
CrossCountry229283.53.52
London Midland482792.32.27
Virgin Trains85943.02.98
All TOCs837102.72.71
" expect_equal(sum(pt$cells$asMatrix()), 167447.538966) expect_identical(as.character(pt$getHtml()), html) }) } } scenarios <- testScenarios("cell setStyling()") 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() cells <- pt$getCells(columnNumbers=2) colorText <- function(cell) { cell$setStyling(list(color="blue")) } invisible(lapply(cells, colorText)) # 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
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) }) }