Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active April 16, 2024 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/24e091b71f37e0aad6d3ce800835fd71 to your computer and use it in GitHub Desktop.
Save matt-dray/24e091b71f37e0aad6d3ce800835fd71 to your computer and use it in GitHub Desktop.
Testing 'general' cell number format with {openxlsx2}
x <- palmerpenguins::penguins |>
tidyr::drop_na() |>
dplyr::mutate(
sp = species,
bill = bill_length_mm,
sex = as.character(sex),
.keep = "none"
) |>
head() |>
as.data.frame()
x[c(3, 6), "bill"] <- "[c]"
x <- tibble::as_tibble(x)
x_wb <- openxlsx2::wb_workbook()$
add_worksheet("sheet")$
add_data_table(x = x)$
add_numfmt(
dims = openxlsx2::wb_dims(x = x, select = "data"),
numfmt = "General"
)
x_wb |> openxlsx2::wb_open() # works, unless you remove the sp or sex cols in x
y <- data.frame(
abb = state.abb,
area1 = state.area,
ame = state.name,
region = state.region,
area2 = state.area + 0.1,
div = state.division
) |> head()
y[y$abb %in% c("AZ", "CO"), "area1"] <- "[c]"
y[y$abb %in% c("AL", "AK"), "area2"] <- "[c]"
y <- tibble::tibble(y)
y_wb <- openxlsx2::wb_workbook()$
add_worksheet("sheet")$
add_data_table(x = y)$
add_numfmt(
dims = openxlsx2::wb_dims(x = y, cols = "area1"),
numfmt = "General"
)$
add_numfmt( # have to repeat for each named column
dims = openxlsx2::wb_dims(x = y, cols = "area2"),
numfmt = "General"
)
y_wb |> openxlsx2::wb_open() # area2 column works, area1 doesn't
z <- data.frame(
abb = state.abb,
area1 = state.area,
area2 = state.area,
area3 = state.area
) |> head()
z[y$abb %in% c("AL", "AR"), "area1"] <- "[c]"
z[y$abb %in% c("AL"), "area2"] <- "[c]"
z[y$abb %in% c("AK", "CO"), "area3"] <- "[c]"
z <- tibble::tibble(z)
z_wb <- openxlsx2::wb_workbook()$
add_worksheet("sheet")$
add_data_table(x = z)$
add_numfmt(
# dims = openxlsx2::wb_dims(x = z, cols = 2:4),
dims = openxlsx2::wb_dims(x = mtcars, select = "data"),
numfmt = "General"
)
z_wb |> openxlsx2::wb_open() # neither of the add_numft approaches works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment