Skip to content

Commit

Permalink
More cleanup using @inheritParams for removing duplicated documenta…
Browse files Browse the repository at this point in the history
…tion for similar and deprecated functions
  • Loading branch information
olivroy committed Aug 22, 2023
1 parent ea69155 commit a701717
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 181 deletions.
33 changes: 11 additions & 22 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ wb_save <- function(wb, file = NULL, overwrite = TRUE, path = NULL) {
#' @param inline_strings write characters as inline strings
#' @param ... additional arguments
#' @export
#' @details Formulae written using write_formula to a Workbook object will not get picked up by `read_xlsx()`.
#' This is because only the formula is written and left to Excel to evaluate the formula when the file is opened in Excel.
#' @details Formulae written using [wb_add_formula()] to a Workbook object will
#' not get picked up by `read_xlsx()`. This is because only the formula is written
#' and left to Excel to evaluate the formula when the file is opened in Excel.
#' The string `"_openxlsx_NA"` is reserved for `openxlsx2`.
#'
#' If the data frame contains this string, the output will be broken.
#' Many base classes are covered, though not all and far from all third-party classes.
#' When data of an unknown class is written, it is handled with `as.character()`.
Expand Down Expand Up @@ -217,16 +219,9 @@ wb_add_data <- function(
#' The string `"_openxlsx_NA"` is reserved for `openxlsx2`. If `x` contains this
#' string, the output will be broken.
#'
#' @param wb A `wbWorkbook` object containing a worksheet.
#' @param sheet The worksheet to write to. Can be the worksheet index or name.
#' @param x A data frame.
#' @param dims A spreadsheet cell range that will determine `start_col` and `start_row`: "A1", "A1:B2", "A:B"
#' @param start_col A vector specifying the starting column to write `x`
#' @param start_row A vector specifying the starting row to write `x`
#' @param col_names If `TRUE`, column names of `x` are written.
#' @param row_names If `TRUE`, row names of `x` are written.
#' @param table_style Any excel table style name or "none" (see "formatting"
#' vignette).
#' @inheritParams wb_add_data
#' @param x A data frame
#' @param table_style Any excel table style name or "none" (see `vignette("openxlsx2_style_manual")`)
#' @param table_name name of table in workbook. The table name must be unique.
#' @param with_filter If `TRUE`, columns with have filters in the first row.
#' @param sep Only applies to list columns. The separator used to collapse list
Expand All @@ -237,16 +232,10 @@ wb_add_data <- function(
#' \cr
#' \if{html}{\figure{tableoptions.png}{options: width="40\%" alt="Figure: table_options.png"}}
#' \if{latex}{\figure{tableoptions.pdf}{options: width=7cm}}
#'
#' @param first_column logical. If `TRUE`, the first column is bold
#' @param last_column logical. If `TRUE`, the last column is bold
#' @param banded_rows logical. If `TRUE`, rows are color banded
#' @param banded_cols logical. If `TRUE`, the columns are color banded
#' @param apply_cell_style Should we write cell styles to the workbook
#' @param remove_cell_style keep the cell style?
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' [na_strings()] uses the special `#N/A` value within the workbook.
#' @param inline_strings write characters as inline strings
#' @param first_column logical. If `TRUE`, the first column is bold.
#' @param last_column logical. If `TRUE`, the last column is bold.
#' @param banded_rows logical. If `TRUE`, rows are color banded.
#' @param banded_cols logical. If `TRUE`, the columns are color banded.
#' @param ... additional arguments
#'
#' @family workbook wrappers
Expand Down
9 changes: 2 additions & 7 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#' Create a data frame from Workbook
#'
#' Simple function to create a `data.frame` from a workbook. Simple as in simply
#' written down. [read_xlsx()] and [wb_read()] are just internal wrappers for
#' [wb_to_df()] intended for people coming from other packages.
#' written down. `read_xlsx()` and `wb_read()` are just internal wrappers of
#' `wb_to_df()` intended for people coming from other packages.
#'
#' @details
#' Depending if the R package `hms` is loaded, `wb_to_df()` returns
Expand Down Expand Up @@ -599,11 +599,6 @@ wb_to_df <- function(
# `read_xlsx()` -----------------------------------------------------------------
# Ignored by roxygen2 when combining documentation
# #' Read from an Excel file or Workbook object
#'
#' @details
#' Formulae written using write_formula to a Workbook object will not get picked up by `read_xlsx()`.
#' This is because only the formula is written and left to be evaluated when the file is opened in Excel.
#' Opening, saving and closing the file with Excel will resolve this.
#' @rdname wb_to_df
#' @export
read_xlsx <- function(
Expand Down
70 changes: 5 additions & 65 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -946,83 +946,23 @@ write_data_table <- function(
# `write_data()` ---------------------------------------------------------------
#' Write an object to a worksheet
#'
#' Write an object to worksheet with optional styling. Use [wb_add_data()] in new code.
#' Write an object to worksheet with optional styling. Use [wb_add_data()] or [write_xlsx()] in new code.
#'
#' Formulae written using write_formula to a Workbook object will not get picked up by read_xlsx().
#' Formulae written using [wb_add_formula()] to a Workbook object will not get picked up by `read_xlsx()`.
#' This is because only the formula is written and left to Excel to evaluate the formula when the file is opened in Excel.
#' The string `"_openxlsx_NA"` is reserved for `openxlsx2`. If the data frame contains this string, the output will be broken.
#'
#' @param wb A Workbook object containing a worksheet.
#' @param sheet The worksheet to write to. Can be the worksheet index or name.
#' @param x Object to be written. For classes supported look at the examples.
#' @param start_col A vector specifying the starting column to write to.
#' @param start_row A vector specifying the starting row to write to.
#' @param dims Spreadsheet dimensions that will determine startCol and startRow: "A1", "A1:B2", "A:B"
#' @param array A bool if the function written is of type array
#' @param col_names If `TRUE`, column names of x are written.
#' @param row_names If `TRUE`, data.frame row names of x are written.
#' @param with_filter If `TRUE`, add filters to the column name row. NOTE can only have one filter per worksheet.
#' @param sep Only applies to list columns. The separator used to collapse list columns to a character vector e.g. sapply(x$list_column, paste, collapse = sep).
#' @param name If not NULL, a named region is defined.
#' @param apply_cell_style apply styles when writing on the sheet
#' @param remove_cell_style if writing into existing cells, should the cell style be removed?
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @param inline_strings write characters as inline strings
#' @param ... additional arguments
#' @seealso [wb_add_data()], [wb_add_data_table()]
#' @inheritParams wb_add_data
#' @seealso [wb_add_data()], [write_xlsx()]
#' @return invisible(0)
# TODO examples them from here, as docs of `wb_add_data()` and `write_data()` are now separated.
#' @examples
#' ## See formatting vignette for further examples.
#'
#' ## Options for default styling (These are the defaults)
#' options("openxlsx2.dateFormat" = "mm/dd/yyyy")
#' options("openxlsx2.datetimeFormat" = "yyyy-mm-dd hh:mm:ss")
#' options("openxlsx2.numFmt" = NULL)
#'
#' #############################################################################
#' ## Create Workbook object and add worksheets
#' wb <- wb_workbook()
#'
#' ## Add worksheets
#' wb$add_worksheet("Cars")
#' wb$add_worksheet("Formula")
#'
#' x <- mtcars[1:6, ]
#' wb$add_data("Cars", x, startCol = 2, startRow = 3, rowNames = TRUE)
#'
#' #############################################################################
#' ## Hyperlinks
#' ## - vectors/columns with class 'hyperlink' are written as hyperlinks'
#'
#' v <- rep("https://CRAN.R-project.org/", 4)
#' names(v) <- paste0("Hyperlink", 1:4) # Optional: names will be used as display text
#' class(v) <- "hyperlink"
#' wb$add_data("Cars", x = v, dims = c("B32"))
#'
#' #############################################################################
#' ## Formulas
#' ## - vectors/columns with class 'formula' are written as formulas'
#'
#' df <- data.frame(
#' x = 1:3, y = 1:3,
#' z = paste(paste0("A", 1:3 + 1L), paste0("B", 1:3 + 1L), sep = "+"),
#' stringsAsFactors = FALSE
#' )
#'
#' class(df$z) <- c(class(df$z), "formula")
#'
#' wb$add_data(sheet = "Formula", x = df)
#'
#' #############################################################################
#' # update cell range and add mtcars
#' xlsxFile <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2")
#' wb2 <- wb_load(xlsxFile)
#'
#' # read dataset with inlinestr
#' wb_to_df(wb2)
#' write_data(wb2, 1, mtcars, startCol = 4, startRow = 4)
#' write_data(wb2, 1, mtcars, start_col = 4, start_row = 4)
#' wb_to_df(wb2)
#' @export
#' @keywords internal
Expand Down
6 changes: 4 additions & 2 deletions man/wb_add_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions man/wb_add_data_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions man/wb_to_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 15 additions & 55 deletions man/write_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a701717

Please sign in to comment.