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

165 options #174

Merged
merged 6 commits into from
Apr 23, 2021
Merged

165 options #174

merged 6 commits into from
Apr 23, 2021

Conversation

jmbarbone
Copy link
Contributor

Resolves #165

A few notes:

  • openxlsx_getOp() is a safe way to retrieve an openxlsx option
    • checks first for the name in op.openxlsx first -- this should prevent misspellings (already has for me)
    • a default can be assigned for NULL values -- which will also make the function defaults visible
  • options that have not been set are set .onAttach()
  • Documentation added for the op.openxlsx object so make all options user facing (although more descriptions can probably be added)
  • Only one test has been edited but I don't believe I wrote it correctly in the first place.

* adds openxls_getOp()
* adds op.openxlsx
* adds testing
* updates docs
* makes corrections to resolve options
* sets most options to NULL
* adds some more todos to do
* formatting
@ycphs
Copy link
Owner

ycphs commented Apr 13, 2021

Thank you very much.

maybe we could also use package options

Comment on lines +59 to +164
#' \code{openxlsx_getOp()} retrieves the \code{"openxlsx"} options found in
#' \code{op.openxlsx}. If none are set (currently `NULL`) retrieves the
#' default option from \code{op.openxlsx}. This will also check that the
#' intended option is a standard option (listed in \code{op.openxlsx}) and
#' will provide a warning otherwise.
#'
#' \code{openxlsx_setOp()} is a safer way to set an option as it will first
#' check that the option is a standard option (as above) before setting.
#'
#' @examples
#' openxlsx_getOp("borders")
#' op.openxlsx[["openxlsx.borders"]]
#'
#' @export
#' @name openxlsx_options
op.openxlsx <- list(
openxlsx.bandedCols = FALSE,
openxlsx.bandedRows = TRUE,
openxlsx.borderColour = "black",
openxlsx.borders = NULL,
openxlsx.borderStyle = "thin",
# Where is compressionLevel called?
openxlsx.compressionLevel = 9,
openxlsx.creator = "",
openxlsx.dateFormat = "mm/dd/yyyy",
openxlsx.datetimeFormat = "yyyy-mm-dd hh:mm:ss",
openxlsx.hdpi = 300,
openxlsx.header = NULL,
openxlsx.headerStyle = NULL,
openxlsx.firstColumn = NULL,
openxlsx.firstFooter = NULL,
openxlsx.firstHeader = NULL,
openxlsx.footer = NULL,
openxlsx.evenFooter = NULL,
openxlsx.evenHeader = NULL,
openxlsx.gridLines = TRUE,
openxlsx.keepNA = FALSE,
openxlsx.lastColumn = NULL,
openxlsx.na.string = NULL,
openxlsx.numFmt = "GENERAL",
openxlsx.oddFooter = NULL,
openxlsx.oddHeader = NULL,
openxlsx.orientation = "portrait",
openxlsx.paperSize = 9,
openxlsx.showGridLines = NA,
openxlsx.tabColour = NULL,
openxlsx.tableStyle = "TableStyleLight9",
openxlsx.vdpi = 300,
openxlsx.withFilter = NULL
)


#' @param x An option name (\code{"openxlsx."} prefix optional)
#' @param default A default value if \code{NULL}
#' @rdname openxlsx_options
#' @export
openxlsx_getOp <- function(x, default = NULL) {
if (length(x) != 1L || length(default) > 1L) {
stop("x must be length 1 and default NULL or length 1", call. = FALSE)
}

x <- check_openxlsx_op(x)
getOption(x, op.openxlsx[[x]]) %||% default
}

#' @param value The new value for the option (optional if x is a named list)
#' @rdname openxlsx_options
#' @export
openxlsx_setOp <- function(x, value) {
if (is.list(x)) {
if (is.null(names(x))) {
stop("x cannot be an unnamed list", call. = FALSE)
}

mapply(openxlsx_setOp, x = names(x), value = x)
}

value <- as.list(value)
names(value) <- check_openxlsx_op(x)
options(value)
}

check_openxlsx_op <- function(x) {
if (length(x) != 1L || !is.character(x)) {
stop("option must be a character vector of length 1", call. = FALSE)
}

if (!grepl("^openxlsx[.]", x)) {
x <- paste0("openxlsx.", x)
}

if (!x %in% names(op.openxlsx)) {
warning(
x, " is not a standard openxlsx option\nCheck spelling",
call. = FALSE
)
}

x
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much.

maybe we could also use package options

@ycphs Not quite sure what you mean. These functions use the option() function for setting the global options. There is an additional step in which openxlsx_getOp() and openxlsx_setOp() also check that the option is present in the op.openxlsx object to validate the name is given appropriately (I had seen a few instances of trying to retrieve options with names like dateTimeFormat and datetimeFormat. Users are still free to use either approach:

# explicitly adding the openxlsx. prefix
op1 <- list(openxlsx.gridLines = FALSE, openxlsx.withFilter = FALSE)
op2 <- list(gridLines = FALSE, withFilter = FALSE)

options(op1) # valid and works fine
options(op2) # valid but won't set the openxlsx options

# appends the openxlsx. prefix is not provided, results are the same
openxlsx_setOps(op1)
openxlsx_setOps(op2)

openxlsx_setOps(withfilter = FALSE) # will throw a warning because the name isn't exactly right

Another example of this sort of implementation is in the tibble package.

@ycphs ycphs merged commit 8b9d5af into ycphs:master Apr 23, 2021
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

Successfully merging this pull request may close these issues.

Request: More global options
2 participants