Skip to content

Commit 9ad45f2

Browse files
committed
Merge pull request #26 from donarus/logical_column_types
Fix for logical column types
2 parents 6046092 + a8119c0 commit 9ad45f2

4 files changed

+59
-25
lines changed

R/apply-table-changes.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ applyChange <- function(table, change, trim=TRUE){
4242
# Extract and shift to 1-based-indexing
4343
row <- as.integer(change[1]) + 1
4444
col <- as.integer(change[2]) + 1
45-
old <- change[3]
45+
old <- setHtableClass(change[3], table[row, col])[1, 1]
4646
new <- change[4]
4747

4848
if (trim){

R/calc-htable-delta.R

+23-19
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
#' @param new The new data.frame
66
#' @return A matrix in which each row represents a change from the old to the
77
#' new matrix in the form of [row, col, newVal, oldVal].
8-
#' @author Jeff Allen \email{jeff@@trestletech.com}
8+
#' @author Jeff Allen \email{jeff@@trestletech.com}, Jonathan Owen \email{jonathanro@@gmail.com}, Tadeas Palusga \email{tadeas@@palusga.cz}
99
#' @export
1010
calcHtableDelta <- function (old, new, zeroIndex = TRUE){
11-
changes <- matrix(ncol=4, nrow=0)
12-
colnames(changes) <- c("row", "col", "new", "old")
11+
changes <- NULL
1312

1413
# Loop through each column, comparing the data
1514
for(i in 1:(max(ncol(new), ncol(old)))){
@@ -18,36 +17,41 @@ calcHtableDelta <- function (old, new, zeroIndex = TRUE){
1817

1918
if (i > ncol(new)){
2019
# the new data.frame doesn't have this column
21-
thisColChanges <- matrix(c(1:nrow(old),
22-
rep(i, nrow(old)),
23-
rep(NA, nrow(old)),
24-
old[,i])
25-
, ncol=4)
20+
thisColChanges <- data.frame(1:nrow(old),
21+
rep(i, nrow(old)),
22+
rep(NA, nrow(old)),
23+
old[,i])
2624
} else if (i > ncol(old)){
2725
# The old data.frame doesn't have this column
28-
thisColChanges <- matrix(c(1:nrow(new),
29-
rep(i, nrow(new)),
30-
new[,i],
31-
rep(NA, nrow(new)))
32-
, ncol=4)
26+
thisColChanges <- data.frame(1:nrow(new),
27+
rep(i, nrow(new)),
28+
new[,i],
29+
rep(NA, nrow(new)))
3330
} else {
3431
# They both have this column
3532
deltaInd <- which(suppressWarnings(old[,i] != new[,i]))
3633
lng <- length(deltaInd)
3734

38-
thisColChanges <- matrix(c(deltaInd,
39-
rep(i, lng),
40-
new[deltaInd, i],
41-
old[deltaInd, i])
42-
, ncol=4)
35+
thisColChanges <- data.frame(deltaInd,
36+
rep(i, lng),
37+
new[deltaInd, i],
38+
old[deltaInd, i])
4339
}
4440

41+
if (is.logical(thisColChanges[, 3]))
42+
thisColChanges[, 3] = ifelse(thisColChanges[, 3], "true", "false")
43+
if (is.logical(thisColChanges[, 4]))
44+
thisColChanges[, 4] = ifelse(thisColChanges[, 4], "true", "false")
45+
4546
if (zeroIndex && nrow(thisColChanges) > 0){
4647
thisColChanges[,1] <- as.integer(thisColChanges[,1]) - 1;
4748
thisColChanges[,2] <- as.integer(thisColChanges[,2]) - 1;
4849
}
49-
5050
changes <- rbind(changes, thisColChanges)
5151
}
52+
53+
if(!is.null(changes)) {
54+
colnames(changes) <- c("row", "col", "new", "old")
55+
}
5256
return (changes)
5357
}

R/get-htable-types.R

+35
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,39 @@ getHtableTypes <- function(data){
2626
})
2727

2828
as.character(types)
29+
}
30+
31+
# Convert to specified class
32+
#
33+
# @param x vector
34+
# @param cls character
35+
# @return converted vector
36+
# @seealso https://stackoverflow.com/questions/9214819/supply-a-vector-to-classes-of-dataframe
37+
# @author Jonathan Owen, jonathanro@@gmail.com
38+
toCls = function(x, cls) tryCatch(do.call(paste("as", cls, sep = "."), list(x)),
39+
warning = function(w) do.call(as.character, list(x)))
40+
41+
# Covert htable output matrix to data.frame using classes of model data.frame
42+
#
43+
# @param data htable matrix
44+
# @param old original data.frame
45+
# @return data.frame
46+
# @seealso https://stackoverflow.com/questions/9214819/supply-a-vector-to-classes-of-dataframe
47+
# @author Jonathan Owen, jonathanro@@gmail.com
48+
setHtableClass = function(data, old) {
49+
if (class(old) == "matrix") {
50+
toCls(data, class(old[1, 1]))
51+
} else {
52+
data = as.data.frame(data, stringsAsFactors = FALSE)
53+
54+
cls = sapply(old, class)
55+
56+
# assume all cols are numeric, will be down coverted to character in toCls
57+
# is there a better way to track which columns were added or removed?
58+
if (length(cls) != ncol(data))
59+
cls = rep("numeric", ncol(data))
60+
61+
data = replace(data, values = Map(toCls, data, cls))
62+
}
63+
data
2964
}

R/render-htable.R

-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ renderHtable <- function(expr, env = parent.frame(),
5858
}
5959

6060
delta <- calcHtableDelta(.oldTables[[shinysession$token]][[name]], data)
61-
62-
# Avoid the awkward serialization of a row-less matrix in RJSONIO
63-
if (nrow(delta) == 0){
64-
delta <- NULL
65-
}
6661

6762
.oldTables[[shinysession$token]][[name]] <- data
6863

0 commit comments

Comments
 (0)