Debian Codegraph @

R/misc.R

raw
1 # Map R classes to handsontable.js types
2 get_col_types = function(data) {
3 if (is.matrix(data)) {
4 types = rep(typeof(data), ncol(data))
5 } else if (is.data.frame(data)){
6 types = as.character(lapply(data, class))
7 } else{
8 stop("Unsupported object type: ", class(data), " Can't extract column types.")
9 }
10
11 types <- sapply(types, function(type) {
12 if (grepl("factor", type)) return("factor")
13
14 switch(type,
15 integer="integer",
16 double="numeric",
17 numeric="numeric",
18 character="text",
19 logical="checkbox",
20 Date="date",
21 "text")
22 })
23
24 as.character(types)
25 }
26
27 # Convert handsontable to R object
28 toR = function(data, changes, params, ...) {
29 rClass = params$rClass
30 colHeaders = unlist(params$rColHeaders)
31 rowHeaders = unlist(params$rRowHeaders)
32 rColClasses = unlist(params$rColClasses)[colHeaders]
33
34 out = data
35
36 # copy/paste may add rows without firing an afterCreateRow event (still needed?)
37 # if (length(out) != length(rowHeaders))
38 # changes$event = "afterCreateRow"
39
40 # remove spare empty rows; autofill fix (not working)
41 # if (!is.null(changes$source) && changes$source == "autofill") {
42 # rm_inds = sapply(out, function(x) all(unlist(x) == "NA"))
43 # rm_inds = suppressWarnings(min(which(diff(rm_inds) == -1)))
44 # if (rm_inds != Inf)
45 # out = out[-(length(out) - rm_inds + 1)]
46 # }
47
48 # pre-conversion updates; afterCreateCol moved to end of function
49 # if (changes$event == "afterCreateRow") {
50 # inds = seq(changes$ind + 1, length.out = changes$ct)
51 # # prevent duplicates
52 # nm = 1
53 # while (nm %in% rowHeaders) {
54 # nm = nm + 1
55 # }
56 # rowHeaders = c(head(rowHeaders, inds - 1), nm,
57 # tail(rowHeaders, length(rowHeaders) - inds + 1))
58 # } else if (changes$event == "afterRemoveRow") {
59 # inds = seq(changes$ind + 1, length.out = changes$ct)
60 # rowHeaders = rowHeaders[-inds]
61 if (changes$event == "afterRemoveCol") {
62 if (!("matrix" %in% rClass)) {
63 inds = seq(changes$ind + 1, 1, length.out = changes$ct)
64 rColClasses = rColClasses[-inds]
65 }
66 }
67
68 # convert
69 if ("matrix" %in% rClass) {
70 nr = length(out)
71 out = unlist(out, recursive = FALSE)
72 # replace NULL with NA
73 out = unlist(lapply(out, function(x) if (is.null(x)) NA else x))
74
75 # If there is no data create empty matrix
76 if (length(out) == 0) {
77 out = matrix(nrow = 0, ncol = length(colHeaders))
78 } else {
79 out = matrix(out, nrow = nr, byrow = TRUE)
80 }
81
82 class(out) = params$rColClasses
83
84 } else if ("data.frame" %in% rClass) {
85 nr = length(out)
86
87 out = unlist(out, recursive = FALSE)
88 # replace NULL with NA
89 out = unlist(lapply(out, function(x) if (is.null(x)) NA else x))
90
91 # If there is no data create empty matrix
92 if (length(out) == 0) {
93 out = matrix(nrow = 0, ncol = length(colHeaders))
94 } else {
95 out = matrix(out, nrow = nr, byrow = TRUE)
96 }
97
98 out = colClasses(as.data.frame(out, stringsAsFactors = FALSE),
99 rColClasses, params$columns, ...)
100 } else {
101 stop("Conversion not implemented: ", rClass)
102 }
103
104
105 # post-conversion updates
106 if (changes$event == "afterCreateRow") {
107 # default logical NA in data.frame to FALSE
108 if (!("matrix" %in% rClass)) {
109 inds_logical = which(rColClasses == "logical")
110 for (i in inds_logical)
111 # bypass empty columns
112 if(!all(is.na(out[[i]]))) {
113 out[[i]] = ifelse(is.na(out[[i]]), FALSE, out[[i]])
114 }
115 }
116 }
117
118 if (ncol(out) != length(colHeaders))
119 colHeaders = genColHeaders(changes, colHeaders)
120
121 if (nrow(out) != length(rowHeaders) && !is.null(rowHeaders))
122 rowHeaders = genRowHeaders(changes, rowHeaders)
123
124 colnames(out) = colHeaders
125 rownames(out) = rowHeaders
126
127 if ("data.table" %in% rClass)
128 out = as(out, "data.table")
129
130 out
131 }
132
133 # Coerces data.frame columns to the specified classes
134 # see http://stackoverflow.com/questions/9214819/supply-a-vector-to-classes-of-dataframe
135 colClasses <- function(d, colClasses, cols, date_fmt = "%m/%d/%Y", ...) {
136 colClasses <- rep(colClasses, len=length(d))
137 for(i in seq_along(d))
138 d[[i]] = switch(
139 colClasses[i],
140 numeric = as.numeric(d[[i]]),
141 Date = as.Date(d[[i]], origin='1970-01-01',
142 format = date_fmt),
143 POSIXct = as.POSIXct(d[[i]], origin='1970-01-01',
144 format = date_fmt),
145 factor = factor(d[[i]],
146 levels = c(unlist(cols[[i]]$source),
147 unique(d[[i]][!(d[[i]] %in% unlist(cols[[i]]$source))])),
148 ordered = TRUE),
149 json = jsonlite::toJSON(d[[i]]),
150 logical = type.convert(d[[i]], as.is = TRUE), # new columns - character/numeric
151 suppressWarnings(as(d[[i]], colClasses[i])))
152 d
153 }
154
155 genColHeaders <- function(changes, colHeaders) {
156 ind_ct = length(which(grepl("V[0-9]{1,}", colHeaders)))
157
158 if (changes$event == "afterRemoveCol") {
159 colHeaders[-(seq(changes$ind, length = changes$ct) + 1)]
160 } else if (changes$event == "afterCreateCol") {
161 # create new column names
162 new_cols = paste0("V", changes$ct + ind_ct)
163 # insert into vector
164 inds = seq(changes$ind + 1, 1, length.out = changes$ct)
165 c(colHeaders, new_cols)[order(c(seq_along(colHeaders), inds - 0.5))]
166 } else {
167 stop("Change no recognized:", changes$event)
168 }
169 }
170
171 genRowHeaders <- function(changes, rowHeaders) {
172 if(changes$event %in% c("afterCreateRow", "afterRemoveRow")) {
173 inds = seq(changes$ind + 1, length.out = changes$ct)
174 if (changes$event == "afterCreateRow") {
175 # prevent duplicates
176 nm = 1
177 while (nm %in% rowHeaders) {
178 nm = nm + 1
179 }
180 c(head(rowHeaders, inds - 1), nm,
181 tail(rowHeaders, length(rowHeaders) - inds + 1))
182 } else if (changes$event == "afterRemoveRow") {
183 rowHeaders[-inds]
184 }
185 }
186 }