Skip to content

Commit

Permalink
[load] switch to read_xml2df() and write_df2xml()
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jan 11, 2025
1 parent 6f24253 commit e0527fa
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 141 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export(xml_attr_mod)
export(xml_node)
export(xml_node_create)
export(xml_node_name)
export(xml_order_children)
export(xml_rm_child)
export(xml_value)
import(R6)
Expand Down
4 changes: 0 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ xml_remove_child3 <- function(node, child, level1, level2, which, pointer) {
.Call(`_openxlsx2_xml_remove_child3`, node, child, level1, level2, which, pointer)
}

xml_order_children1 <- function(node, child, order, pointer) {
.Call(`_openxlsx2_xml_order_children1`, node, child, order, pointer)
}

xml_si_to_txt <- function(doc) {
.Call(`_openxlsx2_xml_si_to_txt`, doc)
}
Expand Down
27 changes: 0 additions & 27 deletions R/pugixml.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,30 +374,3 @@ xml_rm_child <- function(xml_node, xml_child, level, which = 0, pointer = FALSE,

return(z)
}

#' order xml children in node
#' @param xml_node an xml structure
#' @param level the xml root
#' @param order the wanted order as numeric
#' @param pointer pointer
#' @param ... additional arguments passed to `read_xml()`
#' @examples
#' xml <- "<a><b/><c/><d/></a>"
#' xml_order_children(xml, "a", c(3, 2, 1))
#' @export
xml_order_children <- function(xml_node, level, order, pointer = FALSE, ...) {

if (missing(xml_node) || missing(level) || missing(order))
stop("need xml_node, level, and order")

if (!inherits(xml_node, "pugi_xml")) xml_node <- read_xml(xml_node, ...)
assert_class(level, "character")

if (!xml_node_name(xml_node) %in% level)
stop("level not found")

order <- order - 1L

xml_order_children1(node = xml_node, child = level, order = order, pointer = pointer)

}
29 changes: 18 additions & 11 deletions R/wb_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -1542,21 +1542,28 @@ wb_load <- function(
# causes spreadsheet software to be unable to load and recover the file.
for (sheet in seq_along(wb$worksheets)) {
if (length(wb$worksheets[[sheet]]$customSheetViews) == 0) next
cvs <- xml_node(wb$worksheets[[sheet]]$customSheetViews, "customSheetViews", "customSheetView")

for (i in seq_along(cvs)) {
exp_nams <- c("pane", "selection", "rowBreaks", "colBreaks", "pageMargins", "printOptions", "pageSetup", "headerFooter", "autoFilter", "extLst")
cv_nms <- xml_node_name(cvs[i], "customSheetView")
cvs <- xml_node(wb$worksheets[[sheet]]$customSheetViews, "customSheetViews", "customSheetView")

ordr <- match(exp_nams, cv_nms)
ordr <- ordr[!is.na(ordr)]
# chart sheets have a reduced custom view
exp_attr <- c(
"guid", "scale", "colorId", "showPageBreaks", "showFormulas",
"showGridLines", "showRowCol", "outlineSymbols", "zeroValues",
"fitToPage", "printArea", "filter", "showAutoFilter", "hiddenRows",
"hiddenColumns", "state", "filterUnique", "view", "showRuler",
"topLeftCell", "zoomToFit"
)
exp_nams <- c(
"pane", "selection", "rowBreaks", "colBreaks", "pageMargins",
"printOptions", "pageSetup", "headerFooter", "autoFilter", "extLst"
)
cv <- read_xml2df(read_xml(cvs), "customSheetView", vec_attrs = exp_attr, vec_chlds = exp_nams)

cvs[i] <- xml_order_children(xml_node = cvs[i], level = "customSheetView", order = ordr, pointer = FALSE)
# headerFooter cause issues. they are (a) not added to the correct node
# and (b) brick the entire XML structure
cv$headerFooter <- ""

# headerFooter cause issues. they are (a) not added to the correct node
# and (b) brick the entire XML structure
if ("headerFooter" %in% cv_nms) cvs[i] <- xml_rm_child(cvs[i], "headerFooter")
}
cvs <- write_df2xml(cv[c(exp_attr, exp_nams)], "customSheetView", vec_attrs = exp_attr, vec_chlds = exp_nams)

wb$worksheets[[sheet]]$customSheetViews <- xml_node_create("customSheetViews", xml_children = cvs)
}
Expand Down
26 changes: 0 additions & 26 deletions man/xml_order_children.Rd

This file was deleted.

15 changes: 0 additions & 15 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,20 +583,6 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// xml_order_children1
SEXP xml_order_children1(XPtrXML node, std::string child, const std::vector<int>& order, bool pointer);
RcppExport SEXP _openxlsx2_xml_order_children1(SEXP nodeSEXP, SEXP childSEXP, SEXP orderSEXP, SEXP pointerSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< XPtrXML >::type node(nodeSEXP);
Rcpp::traits::input_parameter< std::string >::type child(childSEXP);
Rcpp::traits::input_parameter< const std::vector<int>& >::type order(orderSEXP);
Rcpp::traits::input_parameter< bool >::type pointer(pointerSEXP);
rcpp_result_gen = Rcpp::wrap(xml_order_children1(node, child, order, pointer));
return rcpp_result_gen;
END_RCPP
}
// xml_si_to_txt
SEXP xml_si_to_txt(XPtrXML doc);
RcppExport SEXP _openxlsx2_xml_si_to_txt(SEXP docSEXP) {
Expand Down Expand Up @@ -1029,7 +1015,6 @@ static const R_CallMethodDef CallEntries[] = {
{"_openxlsx2_xml_remove_child1", (DL_FUNC) &_openxlsx2_xml_remove_child1, 4},
{"_openxlsx2_xml_remove_child2", (DL_FUNC) &_openxlsx2_xml_remove_child2, 5},
{"_openxlsx2_xml_remove_child3", (DL_FUNC) &_openxlsx2_xml_remove_child3, 6},
{"_openxlsx2_xml_order_children1", (DL_FUNC) &_openxlsx2_xml_order_children1, 4},
{"_openxlsx2_xml_si_to_txt", (DL_FUNC) &_openxlsx2_xml_si_to_txt, 1},
{"_openxlsx2_is_to_txt", (DL_FUNC) &_openxlsx2_is_to_txt, 1},
{"_openxlsx2_si_to_txt", (DL_FUNC) &_openxlsx2_si_to_txt, 1},
Expand Down
42 changes: 0 additions & 42 deletions src/pugi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,45 +750,3 @@ SEXP xml_remove_child3(XPtrXML node, std::string child, std::string level1, std:
return Rcpp::wrap(Rcpp::String(oss.str()));
}
}

// [[Rcpp::export]]
SEXP xml_order_children1(XPtrXML node, std::string child, const std::vector<int>& order, bool pointer) {

uint32_t pugi_format_flags = pugi_format(node);

pugi::xml_node root = node->child(child.c_str());

std::vector<pugi::xml_node> children;
for (pugi::xml_node child : root.children()) {
if (child.type() == pugi::node_element) {
children.push_back(child);
}
}

if (order.size() != children.size()) {
Rcpp::stop("Order size (%d) does not match the number of children (%d).",
order.size(), children.size());
}

std::vector<pugi::xml_node> reordered_children(order.size());
for (size_t i = 0; i < order.size(); ++i) {
if (order[i] < 0 || static_cast<size_t>(order[i]) >= children.size()) {
Rcpp::stop("Invalid order index: %d", order[i]);
}
reordered_children[i] = children[order[i]];
}

root.remove_children();

for (const auto& child : reordered_children) {
root.append_copy(child);
}

if (pointer) {
return node;
} else {
std::ostringstream oss;
root.print(oss, " ", pugi_format_flags);
return Rcpp::wrap(Rcpp::String(oss.str()));
}
}
15 changes: 0 additions & 15 deletions tests/testthat/test-pugixml.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,3 @@ test_that("works with x namespace", {
expect_equal(exp, got)

})

test_that("xml_order_children works", {
xml <- "<a><b/><c/><d/></a>"
exp <- "<a><d/><c/><b/></a>"
got <- xml_order_children(xml, "a", c(3, 2, 1))
expect_equal(exp, got)

got <- xml_order_children(xml, "a", c(3, 2, 1), pointer = TRUE)
expect_equal(TRUE, inherits(got, "pugi_xml"))

expect_error(xml_order_children(xml, "b", c(3, 2, 1)), "level not found")
expect_error(xml_order_children(xml, "a", c(2, 1)), "does not match the number of children")
expect_error(xml_order_children(xml, "a", c(2, 1, 0)), "Invalid order index")
expect_error(xml_order_children(xml, c(2, 1, 0)), "need xml_node, level, and order")
})

0 comments on commit e0527fa

Please sign in to comment.