Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] add selection argument #1227

Open
JanMarvin opened this issue Dec 30, 2024 · 0 comments
Open

[FR] add selection argument #1227

JanMarvin opened this issue Dec 30, 2024 · 0 comments

Comments

@JanMarvin
Copy link
Owner

Basically I want to be able to pass something analog to which(..., arr.ind = TRUE). This should be possible with
rowcol_to_dims(enforce = TRUE) and/or with wb_dims(enforce = TRUE). The argument enforce is already used in wb_add_data() to indicate that specific dims are requested.

library(openxlsx2)

# create a subset of mtcars
df <- mtcars[14:19, ]

# create coordinates from the which condition df < 4
df_sel <- as.data.frame(which(df < 4, arr.ind = TRUE))

# helper function to create dims out of the condition
rowcol_to_dims2 <- function(row, col) {
  if (length(row) != length(col)) stop("lengths are unequal")
  cols <- int2col(col)
  rows <- as.integer(row)
  cells <- vapply(seq_along(row), function(x) paste0(cols[x], rows[x]), NA_character_)
  paste0(cells, collapse = ",")
}

# create the dims
sel_dims <- rowcol_to_dims2(row = df_sel$row, col = df_sel$col)

dims_to_dataframe(dims = sel_dims, fill = TRUE)
#>    E  F G  H  I  J  K
#> 1 E1 F1   H1 I1 J1 K1
#> 2 E2      H2 I2 J2   
#> 3 E3      H3 I3 J3   
#> 4 E4      H4 I4 J4   
#> 5    F5   H5 I5    K5
#> 6    F6   H6 I6    K6

# use this
wb <- wb_workbook()$add_worksheet()$add_data(x = df, col_names = FALSE)$
  add_fill(dims = sel_dims, color = wb_color("yellow"))

if(interactive()) wb$open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant