Skip to content

Commit

Permalink
[misc] revive support for R versions < 4.0
Browse files Browse the repository at this point in the history
Many `stringsAsFactors = FALSE` changes
  • Loading branch information
JanMarvin authored Dec 17, 2024
1 parent 964d911 commit df5abcb
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 94 deletions.
34 changes: 22 additions & 12 deletions R/class-style_mgr.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#' # style <- style_mgr$new(wb)
#' # style$initialize(wb)
#'
#' # wb$styles_mgr$get_numfmt() |> print()
#' # wb$styles_mgr$next_numfmt_id() |> print()
#' # wb$styles_mgr$get_numfmt() %>% print()
#' # wb$styles_mgr$next_numfmt_id() %>% print()
#' # wb$styles_mgr$get_numfmt_id("numFmt-166")
#'
#' # create new number format
Expand Down Expand Up @@ -129,7 +129,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$numfmt <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -142,7 +143,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$font <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -155,7 +157,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$fill <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -168,7 +171,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$border <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -181,7 +185,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$xf <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -195,7 +200,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$cellStyle <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -209,7 +215,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$cellStyleXf <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -222,7 +229,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$dxf <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}

Expand All @@ -249,7 +257,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
self$tableStyle <- data.frame(
typ,
id,
name
name,
stringsAsFactors = FALSE
)
}
}
Expand Down Expand Up @@ -533,7 +542,8 @@ style_mgr <- R6::R6Class("wbStylesMgr", {
new_entry <- data.frame(
typ = typ,
id = id[length(id)],
name = style_name[sty]
name = style_name[sty],
stringsAsFactors = FALSE
)

if (is_numfmt) self$numfmt <- rbind(self$numfmt, new_entry)
Expand Down
6 changes: 4 additions & 2 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ wb_add_data <- function(
#' add_data_table(
#' x = as.data.frame(USPersonalExpenditure),
#' row_names = TRUE,
#' total_row = c(text = "Total", "none", "sum", "sum", "sum", "SUM")
#' total_row = c(text = "Total", "none", "sum", "sum", "sum", "SUM"),
#' stringsAsFactors = FALSE
#' )
#' @export
wb_add_data_table <- function(
Expand Down Expand Up @@ -528,7 +529,8 @@ wb_add_pivot_table <- function(
#' df <- data.frame(
#' AirPassengers = c(AirPassengers),
#' time = seq(from = as.Date("1949-01-01"), to = as.Date("1960-12-01"), by = "month"),
#' letters = letters[1:4]
#' letters = letters[1:4],
#' stringsAsFactors = FALSE
#' )
#'
#' # create workbook
Expand Down
37 changes: 21 additions & 16 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -2586,14 +2586,14 @@ wbWorkbook <- R6::R6Class(

if (!is.null(target) && is.null(names(target))) {
if (nrow(x) > ncol(x)) {
target <- as.data.frame(as.matrix(target, nrow = nrow(x), ncol = ncol(x)))
target <- as.data.frame(as.matrix(target, nrow = nrow(x), ncol = ncol(x)), stringsAsFactors = FALSE)
}
names(target) <- nams
}

if (!is.null(tooltip) && is.null(names(tooltip))) {
if (nrow(x) > ncol(x)) {
tooltip <- as.data.frame(as.matrix(tooltip, nrow = nrow(x), ncol = ncol(x)))
tooltip <- as.data.frame(as.matrix(tooltip, nrow = nrow(x), ncol = ncol(x)), stringsAsFactors = FALSE)
}
names(tooltip) <- nams
}
Expand Down Expand Up @@ -4000,7 +4000,7 @@ wbWorkbook <- R6::R6Class(
if (transpose) {
to_cols <- seq.int(start_col, start_col + to_nrow)
to_rows <- seq.int(start_row, start_row + to_ncol)
from_dims_df <- as.data.frame(t(from_dims_df))
from_dims_df <- as.data.frame(t(from_dims_df), stringsAsFactors = FALSE)
}

to_dims <- rowcol_to_dims(to_rows, to_cols)
Expand All @@ -4015,7 +4015,7 @@ wbWorkbook <- R6::R6Class(

# TODO improve this. It should use v or inlineStr from cc
if (as_value) {
data <- as.data.frame(unclass(data))
data <- as.data.frame(unclass(data), stringsAsFactors = FALSE)

if (transpose) {
data <- t(data)
Expand All @@ -4031,10 +4031,12 @@ wbWorkbook <- R6::R6Class(

to_cc <- cc[match(from_dims, cc$r), ]
from_cells <- to_cc$r
to_cc[c("r", "row_r", "c_r")] <- cbind(
to_dims_f,
gsub("\\D+", "", to_dims_f),
int2col(col2int(to_dims_f))

to_cc[c("r", "row_r", "c_r")] <- data.frame(
r = to_dims_f,
row_r = gsub("\\D+", "", to_dims_f),
c_r = int2col(col2int(to_dims_f)),
stringsAsFactors = FALSE
)

if (as_ref) {
Expand Down Expand Up @@ -5433,7 +5435,7 @@ wbWorkbook <- R6::R6Class(
sprintf(
'<pane %s topLeftCell="%s" activePane="%s" state="frozen"/><selection pane="%s"/>',
stringi::stri_join(attrs, collapse = " ", sep = " "),
get_cell_refs(data.frame(first_active_row, first_active_col)),
get_cell_refs(data.frame(first_active_row, first_active_col, stringsAsFactors = FALSE)),
activePane,
activePane
)
Expand Down Expand Up @@ -5502,7 +5504,7 @@ wbWorkbook <- R6::R6Class(

cmts <- list()
if (length(cmmt) && length(self$comments) <= cmmt) {
cmts <- as.data.frame(do.call("rbind", self$comments[[cmmt]]))
cmts <- as.data.frame(do.call("rbind", self$comments[[cmmt]]), stringsAsFactors = FALSE)
if (!is.null(dims)) cmts <- cmts[cmts$ref %in% dims, ]
# print(cmts)
cmts <- cmts[c("ref", "author", "comment")]
Expand Down Expand Up @@ -5666,7 +5668,8 @@ wbWorkbook <- R6::R6Class(

tc <- cbind(
rbindlist(xml_attr(self$threadComments[[thread_id]], "threadedComment")),
text = xml_value(self$threadComments[[thread_id]], "threadedComment", "text")
text = xml_value(self$threadComments[[thread_id]], "threadedComment", "text"),
stringsAsFactors = FALSE
)

# probably correclty ordered, but we could order these by date?
Expand Down Expand Up @@ -5703,7 +5706,8 @@ wbWorkbook <- R6::R6Class(

tc <- cbind(
rbindlist(xml_attr(self$threadComments[[thrd]], "threadedComment")),
text = xml_value(self$threadComments[[thrd]], "threadedComment", "text")
text = xml_value(self$threadComments[[thrd]], "threadedComment", "text"),
stringsAsFactors = FALSE
)

if (!is.null(dims) && any(grepl(":", dims)))
Expand Down Expand Up @@ -5828,7 +5832,7 @@ wbWorkbook <- R6::R6Class(
if (!grepl("[A-Z]", substr(rule, 1, 2))) {
## formula looks like "operatorX" , attach top left cell to rule
rule <- paste0(
get_cell_refs(data.frame(min(rows), min(cols))),
get_cell_refs(data.frame(min(rows), min(cols), stringsAsFactors = FALSE)),
rule
)
} ## else, there is a letter in the formula and apply as is
Expand Down Expand Up @@ -6550,7 +6554,8 @@ wbWorkbook <- R6::R6Class(
style = styleplot_xml,
rels = chart1_rels_xml(next_chart),
chartEx = "",
relsEx = ""
relsEx = "",
stringsAsFactors = FALSE
)

self$charts <- rbind(self$charts, chart)
Expand Down Expand Up @@ -7804,7 +7809,7 @@ wbWorkbook <- R6::R6Class(

self$worksheets[[sheet]]$autoFilter <- sprintf(
'<autoFilter ref="%s"/>',
paste(get_cell_refs(data.frame("x" = c(rows, rows), "y" = c(min(cols), max(cols)))), collapse = ":")
paste(get_cell_refs(data.frame("x" = c(rows, rows), "y" = c(min(cols), max(cols)), stringsAsFactors = FALSE)), collapse = ":")
)

invisible(self)
Expand Down Expand Up @@ -9940,7 +9945,7 @@ wbWorkbook <- R6::R6Class(
# TODO can this be moved to the sheet data?
sheet <- private$get_sheet_index(sheet)
sqref <- stringi::stri_join(
get_cell_refs(data.frame(x = c(startRow, endRow), y = c(startCol, endCol))),
get_cell_refs(data.frame(x = c(startRow, endRow), y = c(startCol, endCol), stringsAsFactors = FALSE)),
collapse = ":"
)

Expand Down
4 changes: 3 additions & 1 deletion R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ wbWorksheet <- R6::R6Class(
data.frame(
string = values,
min = cumsum(lengths) - lengths + 1,
max = cumsum(lengths))
max = cumsum(lengths),
stringsAsFactors = FALSE
)
)

# remove duplicates pre merge
Expand Down
3 changes: 2 additions & 1 deletion R/get-named-regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ get_nr_from_definedName <- function(wb) {

dn <- cbind(
rbindlist(xml_attr(dn, "definedName")),
value = xml_value(dn, "definedName")
value = xml_value(dn, "definedName"),
stringsAsFactors = FALSE
)

if (!is.null(dn$value)) {
Expand Down
6 changes: 3 additions & 3 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ split_dims <- function(dims, direction = "row") {
if (direction == 2) direction <- "col"
}
direction <- match.arg(direction, choices = c("row", "col"))
if (direction == "row") df <- as.data.frame(t(df))
if (direction == "row") df <- as.data.frame(t(df), stringsAsFactors = FALSE)
vapply(df, FUN = function(x) {
fst <- x[1]
snd <- x[length(x)]
Expand Down Expand Up @@ -1165,7 +1165,7 @@ known_subtotal_funs <- function(x, total, table, row_names = FALSE) {
}

# prepare output
fml <- as.data.frame(t(fml))
fml <- as.data.frame(t(fml), stringsAsFactors = FALSE)
names(fml) <- nms_x
names(atr) <- nms_x
names(lbl) <- nms_x
Expand Down Expand Up @@ -1298,7 +1298,7 @@ fits_in_dims <- function(x, dims, startCol, startRow) {
transpose_df <- function(x) {
attribs <- attr(x, "c_cm")
classes <- class(x[[1]])
x <- as.data.frame(t(x))
x <- as.data.frame(t(x), stringsAsFactors = FALSE)
for (i in seq_along(x)) {
class(x[[i]]) <- classes
}
Expand Down
2 changes: 1 addition & 1 deletion R/pivot_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ get_items <- function(data, x, item_order, slicer = FALSE, choose = NULL, has_de

if (!is.null(choose)) {
# change order
choose <- eval(parse(text = choose), data.frame(x = dat))[item_order]
choose <- eval(parse(text = choose), data.frame(x = dat, stringsAsFactors = FALSE))[item_order]
hide <- as_xml_attr(!choose)
sele <- as_xml_attr(choose)
} else {
Expand Down
3 changes: 2 additions & 1 deletion R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ wb_to_df <- function(
lapply(hls, function(hl) {
c(hl$ref, ifelse(is.null(hl$target), hl$location, hl$target))
})
)
),
stringsAsFactors = FALSE
)
cc$val[match(hyprlnks$V1, cc$r)] <- hyprlnks$V2
}
Expand Down
10 changes: 8 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ con_rng <- function(x) {
ranges <- tapply(x, group, function(y) c(beg = y[1], end = y[length(y)]))
ranges_df <- do.call(rbind, ranges)

as.data.frame(ranges_df)
as.data.frame(ranges_df, stringsAsFactors = FALSE)
}

#' create consecutive dims from column and row vector
Expand Down Expand Up @@ -841,7 +841,7 @@ wb_dims <- function(..., select = NULL) {
x_has_named_dims <- inherits(x, "data.frame") || inherits(x, "matrix")

if (!is.null(x)) {
x <- as.data.frame(x)
x <- as.data.frame(x, stringsAsFactors = FALSE)
}

cnam <- isTRUE(args$col_names)
Expand Down Expand Up @@ -1363,3 +1363,9 @@ ave2 <- function(x, y, FUN) {
split(x, g) <- lapply(split(x, g), FUN)
x
}

if (getRversion() < "4.0.0") {
deparse1 <- function(expr, collapse = " ") {
paste(deparse(expr), collapse = collapse)
}
}
3 changes: 2 additions & 1 deletion R/wb_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ wb_load <- function(
colors = empty_chr,
style = empty_chr,
rels = empty_chr,
relsEx = empty_chr
relsEx = empty_chr,
stringsAsFactors = FALSE
)

chartsXML_id <- filename_id(chartsXML)
Expand Down
1 change: 1 addition & 0 deletions R/wb_styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@ create_colors_xml <- function(
), pointer = FALSE
)
}

#' @export
#' @rdname create_colors_xml
#' @usage NULL
Expand Down
Loading

0 comments on commit df5abcb

Please sign in to comment.