Skip to content

Commit ba4f49d

Browse files
billdenneyclaude
andcommitted
Tidy the API: shared coercion, accurate docs, most-probable-first arguments
Follow-up to the xl_sheet_view() regrouping, from an API review before the next phase widens things further. The three cell shorthands each repeated the same factor-to-character coercion and a stopifnot() whose failure printed an expression rather than a name. They now share .as_character_arg(), which reports the argument and the class it got. write_xlsx()'s format_headers documentation still described the header as "centered and bold". That stopped being true when the header format became configurable through xl_properties(header_format =), so it now says what the argument actually does and points at the setting. xl_workbook() inherits both that and col_names rather than carrying its own paraphrase. xl_hyperlink(name =), xl_hyperlink_cell(value =) and xl_cell_general(value =) all mean the display text shown instead of a URL; the docs now say so, since the differing names are only an accident of xl_hyperlink() mirroring Excel's HYPERLINK() argument. Two signatures now lead with their most probable argument: xl_font() with bold and italic rather than name and size, and xl_comment() with author before format. Every call site names its arguments past the first, so nothing positional depended on the old order. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 54ce307 commit ba4f49d

12 files changed

Lines changed: 61 additions & 32 deletions

R/excel_types.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Coerce a character-ish argument, accepting a factor, and fail with a message
2+
# that names the argument rather than stopifnot()'s expression dump. Shared by
3+
# the three cell shorthands below, which all take a character vector first.
4+
.as_character_arg <- function(x, arg) {
5+
if (is.factor(x)) x <- as.character(x)
6+
if (!is.character(x))
7+
stop(sprintf("`%s` must be a character vector (got %s)", arg,
8+
paste(class(x), collapse = "/")), call. = FALSE)
9+
x
10+
}
11+
112
#' Excel Types
213
#'
314
#' Create special column types to write to a spreadsheet.
@@ -50,9 +61,7 @@
5061
#' # cleanup
5162
#' unlink(c('universities.xlsx', 'universities2.xlsx'))
5263
xl_formula <- function(x, format = NULL){
53-
if(is.factor(x))
54-
x <- as.character(x)
55-
stopifnot(is.character(x))
64+
x <- .as_character_arg(x, "x")
5665
if(!all(grepl("^=",x) | is.na(x)))
5766
stop("Formulas must start with '='")
5867
xl_cell_general(formula = x, format = format)
@@ -62,12 +71,13 @@ xl_formula <- function(x, format = NULL){
6271
#' @export
6372
#' @param url character vector of URLs. Use `NA` to produce a blank cell.
6473
#' @param name character vector of friendly display names shown in the cell
74+
#' instead of the URL. This is the same idea as `value` in
75+
#' [xl_hyperlink_cell()] and in [xl_cell_general()]; the names differ only
76+
#' because `xl_hyperlink()` mirrors Excel's own `HYPERLINK()` argument
6577
#' instead of the raw URL. When `NULL`, the URL is shown. Ignored for
6678
#' `NA` URLs.
6779
xl_hyperlink <- function(url, name = NULL, format = NULL){
68-
if(is.factor(url))
69-
url <- as.character(url)
70-
stopifnot(is.character(url))
80+
url <- .as_character_arg(url, "url")
7181
fmlas <- if(!is.null(name)){
7282
paste0("=HYPERLINK(", dubquote(url), ",", dubquote(name), ")")
7383
} else {
@@ -80,13 +90,12 @@ xl_hyperlink <- function(url, name = NULL, format = NULL){
8090
#' @rdname xl_formula
8191
#' @export
8292
#' @param value character vector (or `NULL`) of display text shown in the
93+
#' cell instead of the URL --- the same idea as `name` in [xl_hyperlink()]
8394
#' cell. For `xl_hyperlink_cell()`, when `NULL` the raw URL is shown.
8495
#' Recycled to the length of `url`. Automatically set to `NA` for cells
8596
#' whose URL is `NA`.
8697
xl_hyperlink_cell <- function(url, value = NULL, format = NULL){
87-
if(is.factor(url))
88-
url <- as.character(url)
89-
stopifnot(is.character(url))
98+
url <- .as_character_arg(url, "url")
9099
if(is.null(value)){
91100
xl_cell_general(hyperlink = url, format = format)
92101
} else {

R/write_xlsx.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
#' @param x a data frame, an [xl_sheet], an [xl_workbook], or a (named) list of
2323
#' data frames / `xl_sheet`s that become the sheets in the xlsx
2424
#' @param path a file name to write to
25-
#' @param col_names write column names at the top of the file?
26-
#' @param format_headers make the \code{col_names} in the xlsx centered and bold
25+
#' @param col_names write column names as the header row at the top of the sheet?
26+
#' @param format_headers apply the workbook's header format to that header row?
27+
#' The default header format is bold and centered; change it with
28+
#' \code{\link{xl_properties}(header_format = )}.
2729
#' @param use_zip64 use \href{https://en.wikipedia.org/wiki/Zip_(file_format)#ZIP64}{zip64}
2830
#' to enable support for 4GB+ xlsx files. Not all platforms can read this.
2931
#' @examples # Roundtrip example with single excel sheet named 'mysheet'

R/xl_comment.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ is_xl_comment <- function(x) inherits(x, "xl_comment")
6060
#' xl_comment("Estimate", author = "Finance",
6161
#' format = xl_font(name = "Arial", size = 10) +
6262
#' xl_fill(background = "lightyellow"))
63-
xl_comment <- function(text, format = NULL, author = NA, visible = NA,
63+
xl_comment <- function(text, author = NA, format = NULL, visible = NA,
6464
width = NA, height = NA, x_scale = NA, y_scale = NA,
6565
start_row = NA, start_col = NA, x_offset = NA,
6666
y_offset = NA) {

R/xl_format.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ NULL
204204

205205
#' @rdname xl_format_groups
206206
#' @export
207-
xl_font <- function(name = NA, size = NA, color = NA, bold = NA, italic = NA,
207+
xl_font <- function(bold = NA, italic = NA, color = NA, size = NA, name = NA,
208208
underline = NA, strikeout = NA, script = NA, family = NA,
209209
charset = NA, outline = NA, shadow = NA, condense = NA,
210210
extend = NA, scheme = NA, theme = NA, color_indexed = NA,

R/xl_sheet.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ xl_row_spec <- function(rows, height = NA, hidden = NA, level = NA,
147147
#' @param page An [xl_page_setup()] describing how the sheet prints
148148
#' (orientation, paper size, margins, scaling, header and footer). Affects
149149
#' printing only, never the cell data.
150+
#' @param merge One [xl_merge()], or a list of them, merging rectangles of cells
151+
#' into single cells. Merges are applied after the sheet's rows are written,
152+
#' so a merge over cells the data frame filled keeps only the merged text ---
153+
#' as merging in Excel does. Any merge turns off the memory-efficient
154+
#' row-streaming mode, since it writes back over rows already emitted.
150155
#' @return An `xl_sheet` object.
151156
#' @family writexl
152157
#' @seealso [xl_col_spec], [xl_row_spec], [write_xlsx]

R/xl_workbook.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ print.xl_properties <- function(x, ...) {
135135
#'
136136
#' @param sheets A data frame, an [xl_sheet], or a (named) list of them.
137137
#' @param properties An [xl_properties] object.
138-
#' @param col_names Write column names as the header row?
139-
#' @param format_headers Apply the header format to the header row?
138+
#' @inheritParams write_xlsx
140139
#' @return An `xl_workbook` object.
141140
#' @family writexl
142141
#' @seealso [xl_properties], [xl_sheet], [write_xlsx]

man/write_xlsx.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xl_comment.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xl_format_groups.Rd

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/xl_formula.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)