From 14f6ba65b6e3dc42ceb11e31046ac767a5affaa2 Mon Sep 17 00:00:00 2001 From: Eli Pousson Date: Wed, 18 Dec 2024 13:08:52 -0500 Subject: [PATCH] Add examples to wb_set_col_widths_ext --- R/wb_set_col_widths_ext.R | 14 +++++++++++--- man/wb_set_col_widths_ext.Rd | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/R/wb_set_col_widths_ext.R b/R/wb_set_col_widths_ext.R index 4e7e931..8febe58 100644 --- a/R/wb_set_col_widths_ext.R +++ b/R/wb_set_col_widths_ext.R @@ -2,11 +2,19 @@ #' #' [wb_set_col_widths_ext()] extends [openxlsx2::wb_set_col_widths()] by making #' `cols` optional (defaults to all columns) and allows setting columns based on -#' column names. +#' column names (rather than index position alone). #' #' @inheritParams openxlsx2::wb_set_col_widths #' @seealso #' [openxlsx2::wb_data()], [openxlsx2::col_widths-wb()] +#' @examples +#' wb <- as_wb(mtcars) +#' +#' wb_set_col_widths_ext(wb, cols = "drat") +#' +#' # `cols` is optional +#' wb_set_col_widths_ext(wb) +#' #' @keywords internal #' @export #' @importFrom openxlsx2 wb_data wb_set_col_widths @@ -14,9 +22,9 @@ wb_set_col_widths_ext <- function(wb, cols = NULL, widths = "auto", sheet = current_sheet()) { - cols <- cols %||% seq(ncol(openxlsx2::wb_data(wb, sheet))) + cols <- cols %||% seq(ncol(openxlsx2::wb_data(wb, sheet = sheet))) - if (any(is.na(as.integer(cols)))) { + if (any(is.na(suppressWarnings(as.integer(cols))))) { cols <- wb_cols_to_index( wb, cols = cols, diff --git a/man/wb_set_col_widths_ext.Rd b/man/wb_set_col_widths_ext.Rd index 742e409..351b91e 100644 --- a/man/wb_set_col_widths_ext.Rd +++ b/man/wb_set_col_widths_ext.Rd @@ -27,6 +27,15 @@ software. See \strong{Details} for general information on column widths.} \code{\link[=wb_set_col_widths_ext]{wb_set_col_widths_ext()}} extends \code{\link[openxlsx2:col_widths-wb]{openxlsx2::wb_set_col_widths()}} by making \code{cols} optional (defaults to all columns) and allows setting columns based on column names. +} +\examples{ +wb <- as_wb(mtcars) + +wb_set_col_widths_ext(wb, cols = "drat") + +# `cols` is optional +wb_set_col_widths_ext(wb) + } \seealso{ \code{\link[openxlsx2:wb_data]{openxlsx2::wb_data()}}, \code{\link[openxlsx2:col_widths-wb]{openxlsx2::col_widths-wb()}}