-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathload_workbook.cpp
358 lines (280 loc) · 9.63 KB
/
load_workbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "openxlsx2.h"
#include <set>
// [[Rcpp::export]]
Rcpp::DataFrame col_to_df(XPtrXML doc) {
Rcpp::CharacterVector col_nams= {
"min",
"max",
"width",
"bestFit",
"customWidth",
"collapsed",
"hidden",
"outlineLevel",
"phonetic",
"style"
};
auto nn = std::distance(doc->begin(), doc->end());
auto kk = col_nams.length();
Rcpp::CharacterVector rvec(nn);
// 1. create the list
Rcpp::List df(kk);
for (auto i = 0; i < kk; ++i)
{
SET_VECTOR_ELT(df, i, Rcpp::CharacterVector(Rcpp::no_init(nn)));
}
// 2. fill the list
// <row ...>
auto itr = 0;
for (auto col : doc->children("col")) {
for (auto attrs : col.attributes()) {
Rcpp::CharacterVector attr_name = attrs.name();
std::string attr_value = attrs.value();
// mimic which
Rcpp::IntegerVector mtc = Rcpp::match(col_nams, attr_name);
Rcpp::IntegerVector idx = Rcpp::seq(0, mtc.length()-1);
// check if name is already known
if (all(Rcpp::is_na(mtc))) {
Rcpp::Rcout << attr_name << ": not found in col name table" << std::endl;
} else {
size_t ii = Rcpp::as<size_t>(idx[!Rcpp::is_na(mtc)]);
Rcpp::as<Rcpp::CharacterVector>(df[ii])[itr] = attr_value;
}
}
// rownames as character vectors matching to <c s= ...>
rvec[itr] = std::to_string(itr);
++itr;
}
// 3. Create a data.frame
df.attr("row.names") = rvec;
df.attr("names") = col_nams;
df.attr("class") = "data.frame";
return df;
}
// [[Rcpp::export]]
Rcpp::CharacterVector df_to_xml(std::string name, Rcpp::DataFrame df_col) {
auto n = df_col.nrow();
Rcpp::CharacterVector z(n);
for (auto i = 0; i < n; ++i) {
pugi::xml_document doc;
Rcpp::CharacterVector attrnams = df_col.names();
pugi::xml_node col = doc.append_child(name.c_str());
for (auto j = 0; j < df_col.ncol(); ++j) {
Rcpp::CharacterVector cv_s = "";
cv_s = Rcpp::as<Rcpp::CharacterVector>(df_col[j])[i];
// only write attributes where cv_s has a value
if (cv_s[0] != "") {
// Rf_PrintValue(cv_s);
const std::string val_strl = Rcpp::as<std::string>(cv_s);
col.append_attribute(attrnams[j]) = val_strl.c_str();
}
}
std::ostringstream oss;
doc.print(oss, " ", pugi::format_raw);
z[i] = oss.str();
}
return z;
}
Rcpp::DataFrame row_to_df(XPtrXML doc) {
auto ws = doc->child("worksheet").child("sheetData");
std::set<std::string> row_nams {
"r",
"spans",
"s",
"ht",
"hidden",
"collapsed",
"customFormat",
"customHeight",
"x14ac:dyDescent",
"outlineLevel",
"ph",
"thickBot",
"thickTop"
};
auto nn = std::distance(ws.children("row").begin(), ws.children("row").end());
auto kk = row_nams.size();
Rcpp::CharacterVector rvec(nn);
// 1. create the list
Rcpp::List df(kk);
for (auto i = 0; i < kk; ++i)
{
SET_VECTOR_ELT(df, i, Rcpp::CharacterVector(Rcpp::no_init(nn)));
}
// 2. fill the list
// <row ...>
auto itr = 0;
for (auto row : ws.children("row")) {
bool has_rowname = false;
for (auto attrs : row.attributes()) {
std::string attr_name = attrs.name();
std::string attr_value = attrs.value();
// mimic which
auto find_res = row_nams.find(attr_name);
// check if name is already known
if (row_nams.count(attr_name) == 0) {
Rcpp::Rcout << attr_name << ": not found in row name table" << std::endl;
} else {
auto mtc = std::distance(row_nams.begin(), find_res);
Rcpp::as<Rcpp::CharacterVector>(df[mtc])[itr] = attr_value;
if (attr_name == "r") has_rowname = true;
}
}
// some files have no row name in this case, we add one
if (!has_rowname) {
std::string attr_name = {"r"};
auto find_res = row_nams.find(attr_name);
auto mtc = std::distance(row_nams.begin(), find_res);
Rcpp::as<Rcpp::CharacterVector>(df[mtc])[itr] = std::to_string(itr + 1);
}
// rownames as character vectors matching to <c s= ...>
rvec[itr] = std::to_string(itr);
++itr;
}
// 3. Create a data.frame
df.attr("row.names") = rvec;
df.attr("names") = row_nams;
df.attr("class") = "data.frame";
return df;
}
// this function imports the data from the dataset and returns row_attr and cc
// [[Rcpp::export]]
void loadvals(Rcpp::Environment sheet_data, XPtrXML doc) {
auto ws = doc->child("worksheet").child("sheetData");
size_t n = std::distance(ws.begin(), ws.end());
// character
Rcpp::DataFrame row_attributes;
Rcpp::Shield<SEXP> rownames(Rf_allocVector(STRSXP, n));
std::vector<xml_col> xml_cols;
// we check against these
const std::string f_str = "f";
const std::string r_str = "r";
const std::string s_str = "s";
const std::string t_str = "t";
const std::string v_str = "v";
const std::string ca_str = "ca";
const std::string cm_str = "cm";
const std::string is_str = "is";
const std::string ph_str = "ph";
const std::string si_str = "si";
const std::string vm_str = "vm";
const std::string ref_str = "ref";
/*****************************************************************************
* Row information is returned as list of lists returning as much as possible.
*
* Col information is returned as dataframe returning only a fraction of known
* tags and attributes.
****************************************************************************/
row_attributes = row_to_df(doc);
auto itr_rows = 0;
for (auto worksheet: ws.children("row")) {
/* ---------------------------------------------------------------------- */
/* read cval, and ctyp -------------------------------------------------- */
/* ---------------------------------------------------------------------- */
// buffer is string buf is SEXP
std::string buffer, attr_name, val_name, cattr_name;
auto itr_cols = 0;
for (auto col : worksheet.children("c")) {
// contains all values of a col
xml_col single_xml_col {
openxlsxNA, // row_r
openxlsxNA, // c_r
openxlsxNA, // c_s
openxlsxNA, // c_t
openxlsxNA, // c_cm
openxlsxNA, // c_ph
openxlsxNA, // c_vm
openxlsxNA, // v
openxlsxNA, // f
openxlsxNA, // f_t
openxlsxNA, // f_ref
openxlsxNA, // f_ca
openxlsxNA, // f_si
openxlsxNA // is
};
// get number of children and attributes
auto nn = std::distance(col.children().begin(), col.children().end());
// typ: attribute ------------------------------------------------------
bool has_colname = false;
auto attr_itr = 0;
for (auto attr : col.attributes()) {
buffer = attr.value();
attr_name = attr.name();
if (attr_name == r_str) {
// get r attr e.g. "A1" and return colnames "A"
// get col name
std::string colrow = buffer;
colrow.erase(std::remove_if(colrow.begin(),
colrow.end(),
&isdigit),
colrow.end());
single_xml_col.c_r = colrow;
has_colname = true;
// get colnum
colrow = buffer;
// remove numeric from string
colrow.erase(std::remove_if(colrow.begin(),
colrow.end(),
&isalpha),
colrow.end());
single_xml_col.row_r = colrow;
}
if (attr_name == s_str) single_xml_col.c_s = buffer;
if (attr_name == t_str) single_xml_col.c_t = buffer;
if (attr_name == cm_str) single_xml_col.c_cm = buffer;
if (attr_name == ph_str) single_xml_col.c_ph = buffer;
if (attr_name == vm_str) single_xml_col.c_vm = buffer;
++attr_itr;
}
// some files have no colnames. in this case we need to add c_r and row_r
// if the file provides dimensions, they could be fixed later
if (!has_colname) {
std::string tmp_colname= int_to_col(itr_cols);
single_xml_col.c_r = tmp_colname;
single_xml_col.row_r = std::to_string(itr_rows + 1);
}
// val ------------------------------------------------------------------
if (nn > 0) {
auto val_itr = 0;
for (auto val: col.children()) {
val_name = val.name();
// <is>
if (val_name == is_str) {
std::ostringstream oss;
val.print(oss, " ", pugi::format_raw);
single_xml_col.is = oss.str();
} // </is>
// <f>
if (val_name == f_str) {
single_xml_col.f = val.child_value();
// additional attributes to <f>
// This currently handles
// * t=
// * ref=
// * ca=
// * si=
for (auto cattr : val.attributes())
{
buffer = cattr.value();
cattr_name = cattr.name();
if (cattr_name == t_str) single_xml_col.f_t = buffer;
if (cattr_name == ref_str) single_xml_col.f_ref = buffer;
if (cattr_name == ca_str) single_xml_col.f_ca = buffer;
if (cattr_name == si_str) single_xml_col.f_si = buffer;
}
} // </f>
// <v>
if (val_name == v_str) single_xml_col.v = val.child_value();
++val_itr;
}
/* row is done */
}
xml_cols.push_back(single_xml_col);
++itr_cols;
}
/* ---------------------------------------------------------------------- */
++itr_rows;
}
sheet_data["row_attr"] = row_attributes;
sheet_data["cc"] = Rcpp::wrap(xml_cols);
}