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, varName="html") {
t <- escapeString(s)
u <- eval(parse(text=paste0("\"", t, "\"")))
if(s!=u) stop("Unable to escape string!")
if(is.null(varName)) varName <- "html"
t <- paste0("\t", varName, " <- \"", t, "\"")
utils::writeClipboard(t)
return(invisible())
}
context("GET VALUES TESTS")
test_that("retrieving cell value", {
# data for the table
saleIds <- c(5334, 5336, 5338)
items <- c("Apple", "Orange", "Banana")
quantities <- c(5, 8, 6)
prices <- c(0.34452354, 0.4732543, 1.3443243)
# construct the table
library(basictabler)
tbl <- BasicTable$new(compatibility=list(headerCellsAsTD=TRUE))
tbl$addData(data.frame(saleIds, items, quantities, prices, stringsAsFactors=FALSE),
firstColumnAsRowHeaders=TRUE,
explicitColumnHeaders=c("Sale ID", "Item", "Quantity", "Price"),
columnFormats=list(NULL, NULL, NULL, "%.2f"))
v1 <- tbl$cells$getValue(2, 4)
v2 <- tbl$cells$getValue(2, 4, formattedValue=TRUE)
rowValues <- tbl$cells$getRowValues(2, asList=TRUE)
rowValues <- paste0(class(rowValues), ": ", paste(rowValues, collapse=", "))
columnValues <- tbl$cells$getColumnValues(3)
columnValues <- paste0(class(columnValues), ": ", paste(columnValues, collapse=", "))
# tbl$renderTable()
# prepStr(tbl$print(asCharacter=TRUE), "str")
# prepStr(as.character(tbl$getHtml()))
str <- "Sale ID Item Quantity Price \n 5334 Apple 5 0.34 \n 5336 Orange 8 0.47 \n 5338 Banana 6 1.34 "
html <- "<table class=\"Table\">\n <tr>\n <td class=\"ColumnHeader\">Sale ID</td>\n <td class=\"ColumnHeader\">Item</td>\n <td class=\"ColumnHeader\">Quantity</td>\n <td class=\"ColumnHeader\">Price</td>\n </tr>\n <tr>\n <td class=\"RowHeader\">5334</td>\n <td class=\"Cell\">Apple</td>\n <td class=\"Cell\">5</td>\n <td class=\"Cell\">0.34</td>\n </tr>\n <tr>\n <td class=\"RowHeader\">5336</td>\n <td class=\"Cell\">Orange</td>\n <td class=\"Cell\">8</td>\n <td class=\"Cell\">0.47</td>\n </tr>\n <tr>\n <td class=\"RowHeader\">5338</td>\n <td class=\"Cell\">Banana</td>\n <td class=\"Cell\">6</td>\n <td class=\"Cell\">1.34</td>\n </tr>\n</table>"
expect_identical(tbl$print(asCharacter=TRUE), str)
expect_identical(as.character(tbl$getHtml()), html)
expect_equal(v1, 0.34452354)
expect_identical(v2, "0.34")
expect_identical(rowValues, "list: 5334, Apple, 5, 0.34452354")
expect_identical(columnValues, "numeric: 5, 8, 6")
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.