Skip to content

Commit ec3bd2e

Browse files
committed
[save] avoid memory blow up
1 parent 70eb56b commit ec3bd2e

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

R/class-workbook.R

+17-14
Original file line numberDiff line numberDiff line change
@@ -9782,40 +9782,43 @@ wbWorkbook <- R6::R6Class(
97829782
}
97839783
} else {
97849784
## Write worksheets
9785-
ws <- self$worksheets[[i]]
9786-
hasHL <- length(ws$hyperlinks) > 0
9785+
# ws <- self$worksheets[[i]]
9786+
hasHL <- length(self$worksheets[[i]]$hyperlinks) > 0
97879787

9788-
prior <- ws$get_prior_sheet_data()
9789-
post <- ws$get_post_sheet_data()
9788+
prior <- self$worksheets[[i]]$get_prior_sheet_data()
9789+
post <- self$worksheets[[i]]$get_post_sheet_data()
97909790

9791-
if (!is.null(ws$sheet_data$cc)) {
9791+
if (!is.null(self$worksheets[[i]]$sheet_data$cc)) {
97929792

9793-
cc <- ws$sheet_data$cc
9794-
cc$r <- stringi::stri_join(cc$c_r, cc$row_r)
9793+
self$worksheets[[i]]$sheet_data$cc$r <- with(
9794+
self$worksheets[[i]]$sheet_data$cc,
9795+
stringi::stri_join(c_r, row_r)
9796+
)
9797+
cc <- self$worksheets[[i]]$sheet_data$cc
97959798
# prepare data for output
97969799

97979800
# there can be files, where row_attr is incomplete because a row
97989801
# is lacking any attributes (presumably was added before saving)
97999802
# still row_attr is what we want!
98009803

9801-
rows_attr <- ws$sheet_data$row_attr
9802-
ws$sheet_data$row_attr <- rows_attr[order(as.numeric(rows_attr[, "r"])), ]
9804+
rows_attr <- self$worksheets[[i]]$sheet_data$row_attr
9805+
self$worksheets[[i]]$sheet_data$row_attr <- rows_attr[order(as.numeric(rows_attr[, "r"])), ]
98039806

9804-
cc_rows <- ws$sheet_data$row_attr$r
9807+
cc_rows <- self$worksheets[[i]]$sheet_data$row_attr$r
98059808
# c("row_r", "c_r", "r", "v", "c_t", "c_s", "c_cm", "c_ph", "c_vm", "f", "f_attr", "is")
98069809
cc <- cc[cc$row_r %in% cc_rows, ]
98079810

9808-
ws$sheet_data$cc <- cc[order(as.integer(cc[, "row_r"]), col2int(cc[, "c_r"])), ]
9811+
self$worksheets[[i]]$sheet_data$cc <- cc[order(as.integer(cc[, "row_r"]), col2int(cc[, "c_r"])), ]
98099812
} else {
9810-
ws$sheet_data$row_attr <- NULL
9811-
ws$sheet_data$cc <- NULL
9813+
self$worksheets[[i]]$sheet_data$row_attr <- NULL
9814+
self$worksheets[[i]]$sheet_data$cc <- NULL
98129815
}
98139816

98149817
# create entire sheet prior to writing it
98159818
sheet_xml <- write_worksheet(
98169819
prior = prior,
98179820
post = post,
9818-
sheet_data = ws$sheet_data
9821+
sheet_data = self$worksheets[[i]]$sheet_data
98199822
)
98209823
ws_file <- file.path(xlworksheetsDir, sprintf("sheet%s.xml", i))
98219824
write_xmlPtr(doc = sheet_xml, fl = ws_file)

src/RcppExports.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -854,14 +854,14 @@ BEGIN_RCPP
854854
END_RCPP
855855
}
856856
// write_worksheet
857-
XPtrXML write_worksheet(std::string prior, std::string post, Rcpp::Environment sheet_data);
857+
XPtrXML write_worksheet(std::string prior, std::string post, Rcpp::Environment& sheet_data);
858858
RcppExport SEXP _openxlsx2_write_worksheet(SEXP priorSEXP, SEXP postSEXP, SEXP sheet_dataSEXP) {
859859
BEGIN_RCPP
860860
Rcpp::RObject rcpp_result_gen;
861861
Rcpp::RNGScope rcpp_rngScope_gen;
862862
Rcpp::traits::input_parameter< std::string >::type prior(priorSEXP);
863863
Rcpp::traits::input_parameter< std::string >::type post(postSEXP);
864-
Rcpp::traits::input_parameter< Rcpp::Environment >::type sheet_data(sheet_dataSEXP);
864+
Rcpp::traits::input_parameter< Rcpp::Environment& >::type sheet_data(sheet_dataSEXP);
865865
rcpp_result_gen = Rcpp::wrap(write_worksheet(prior, post, sheet_data));
866866
return rcpp_result_gen;
867867
END_RCPP

src/write_file.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void xml_sheet_data(pugi::xml_node &doc, Rcpp::DataFrame &row_attr, Rcpp::DataFr
202202
// create single xml rows of sheet_data.
203203
//
204204
// [[Rcpp::export]]
205-
XPtrXML write_worksheet(std::string prior, std::string post, Rcpp::Environment sheet_data) {
205+
XPtrXML write_worksheet(std::string prior, std::string post, Rcpp::Environment &sheet_data) {
206206
uint32_t pugi_parse_flags = pugi::parse_cdata | pugi::parse_wconv_attribute | pugi::parse_ws_pcdata | pugi::parse_eol;
207207

208208
// sheet_data will be in order, just need to check for row_heights

0 commit comments

Comments
 (0)