Title: | Quick Customized Excel Files |
---|---|
Description: | A wrapper to the openxlsx package optimized for writing flat data structures. |
Authors: | Patrick Barks [aut, cre] |
Maintainer: | Patrick Barks <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2024-10-24 16:07:37 UTC |
Source: | https://github.com/epicentre-msf/qxl |
Translate an R expression into an Excel conditional formatting formula
expr_to_excel(x, data, row_start = 2L)
expr_to_excel(x, data, row_start = 2L)
x |
An R-style expression |
data |
A data frame that expression |
row_start |
Integer reflecting the first row of data in the Excel sheet to be output. Defaults to 2. |
A character string reflecting an Excel conditional formatting formula
library(datasets) expr_to_excel(cyl > 4, mtcars) expr_to_excel(cyl > 4 & hp < 200, mtcars)
library(datasets) expr_to_excel(cyl > 4, mtcars) expr_to_excel(cyl > 4 & hp < 200, mtcars)
Wrapper to openxlsx::protectWorksheet
with additional argument cols
to
enable protection to be limited to specific columns.
In practice, protection is first applied to the entire worksheet, and then subsequently columns not selected for protection (if any) are unlocked one by one. This unlock step (when relevant) also requires a row specification, which by default we limit to the range of the current data. Thus, in 'unprotected' columns within protected worksheets, rows beyond the range of the data will remain protected. As a hack to work around this, the user can specify a 'buffer' of additional empty rows to unprotect within each non-protected column (e.g. to allow further data entry).
qprotect( password = NULL, cols = everything(), row_buffer = 0L, protect = TRUE, lockSelectingLockedCells = FALSE, lockSelectingUnlockedCells = FALSE, lockFormattingCells = FALSE, lockFormattingColumns = FALSE, lockFormattingRows = FALSE, lockInsertingColumns = TRUE, lockInsertingRows = TRUE, lockInsertingHyperlinks = FALSE, lockDeletingColumns = TRUE, lockDeletingRows = TRUE, lockSorting = FALSE, lockAutoFilter = FALSE, lockPivotTables = NULL, lockObjects = NULL, lockScenarios = NULL )
qprotect( password = NULL, cols = everything(), row_buffer = 0L, protect = TRUE, lockSelectingLockedCells = FALSE, lockSelectingUnlockedCells = FALSE, lockFormattingCells = FALSE, lockFormattingColumns = FALSE, lockFormattingRows = FALSE, lockInsertingColumns = TRUE, lockInsertingRows = TRUE, lockInsertingHyperlinks = FALSE, lockDeletingColumns = TRUE, lockDeletingRows = TRUE, lockSorting = FALSE, lockAutoFilter = FALSE, lockPivotTables = NULL, lockObjects = NULL, lockScenarios = NULL )
password |
(optional) password required to unprotect the worksheet |
cols |
Tidy-selection specifying the columns that protection should
apply to. Defaults to |
row_buffer |
The number of additional rows (beyond the range of the
current data) to unprotect within columns not specified in argument |
protect |
Whether to protect or unprotect the sheet (default=TRUE) |
lockSelectingLockedCells |
Whether selecting locked cells is locked |
lockSelectingUnlockedCells |
Whether selecting unlocked cells is locked |
lockFormattingCells |
Whether formatting cells is locked |
lockFormattingColumns |
Whether formatting columns is locked |
lockFormattingRows |
Whether formatting rows is locked |
lockInsertingColumns |
Whether inserting columns is locked |
lockInsertingRows |
Whether inserting rows is locked |
lockInsertingHyperlinks |
Whether inserting hyperlinks is locked |
lockDeletingColumns |
Whether deleting columns is locked |
lockDeletingRows |
Whether deleting rows is locked |
lockSorting |
Whether sorting is locked |
lockAutoFilter |
Whether auto-filter is locked |
lockPivotTables |
Whether pivot tables are locked |
lockObjects |
Whether objects are locked |
lockScenarios |
Whether scenarios are locked |
Wrapper to readxl::read_excel
with minor changes to default settings:
columns of dates with no time component have class "Date" rather than "POSIX"
empty columns are read in as class "character" rather than "logical"
the max number of rows used to guess column types is 10k rather than 1k
qread( path, sheet = NULL, range = NULL, col_names = TRUE, col_types = NULL, simplify_dates = TRUE, empty_cols_to_chr = TRUE, na = "", trim_ws = TRUE, skip = 0, n_max = Inf, guess_max = min(10000, n_max), progress = FALSE, .name_repair = "unique" )
qread( path, sheet = NULL, range = NULL, col_names = TRUE, col_types = NULL, simplify_dates = TRUE, empty_cols_to_chr = TRUE, na = "", trim_ws = TRUE, skip = 0, n_max = Inf, guess_max = min(10000, n_max), progress = FALSE, .name_repair = "unique" )
path |
Path to the xls/xlsx file. |
sheet |
Sheet to read. Either a string (the name of a sheet), or an
integer (the position of the sheet). Ignored if the sheet is specified via
|
range |
A cell range to read from, as described in cell-specification.
Includes typical Excel ranges like "B3:D87", possibly including the sheet
name like "Budget!B2:G14", and more. Interpreted strictly, even if the
range forces the inclusion of leading or trailing empty rows or columns.
Takes precedence over |
col_names |
|
col_types |
Either |
simplify_dates |
Logical indicating whether to convert date columns
lacking a time component to class "Date". By default readxl::read_excel
reads columns containing dates or datetimes as class POSIX, even if there
is no time component (i.e. in which case the times will all be "00:00:00").
If |
empty_cols_to_chr |
Logical indicating whether columns of class
"logical" containing all missing values should be converted to class
"character". If argument |
na |
Character vector of strings to interpret as missing values. By default, readxl treats blank cells as missing data. |
trim_ws |
Should leading and trailing whitespace be trimmed? |
skip |
Minimum number of rows to skip before reading anything, be it
column names or data. Leading empty rows are automatically skipped, so this
is a lower bound. Ignored if |
n_max |
Maximum number of data rows to read. Trailing empty rows are
automatically skipped, so this is an upper bound on the number of rows in
the returned tibble. Ignored if |
guess_max |
Maximum number of data rows to use for guessing column types. |
progress |
Display a progress spinner? By default, the spinner appears
only in an interactive session, outside the context of knitting a document,
and when the call is likely to run for several seconds or more. See
|
.name_repair |
Handling of column names. Passed along to
|
Wrapper to openxlsx::createStyle
to create cell styles, with additional
arguments rows
and cols
to specify the rows and/or columns that the style
should apply to.
qstyle( rows = "data", cols = everything(), fontName = NULL, fontSize = NULL, fontColour = NULL, border = NULL, borderColour = getOption("openxlsx.borderColour", "black"), borderStyle = getOption("openxlsx.borderStyle", "thin"), bgFill = NULL, fgFill = NULL, halign = NULL, valign = NULL, textDecoration = NULL, wrapText = FALSE, textRotation = NULL, indent = NULL, locked = NULL, hidden = NULL )
qstyle( rows = "data", cols = everything(), fontName = NULL, fontSize = NULL, fontColour = NULL, border = NULL, borderColour = getOption("openxlsx.borderColour", "black"), borderStyle = getOption("openxlsx.borderStyle", "thin"), bgFill = NULL, fgFill = NULL, halign = NULL, valign = NULL, textDecoration = NULL, wrapText = FALSE, textRotation = NULL, indent = NULL, locked = NULL, hidden = NULL )
rows |
Which rows the style should apply to. Can be set using
either: Integer rows indexes: (e.g. Expressions can optionally include a Note that conditional formatting can update in real time if relevant data is changed within the workbook. |
cols |
Tidy-selection specifying the columns that the style should apply
to. Defaults to |
fontName |
A name of a font. Note the font name is not validated. If fontName is NULL, the workbook base font is used. (Defaults to Calibri) |
fontSize |
Font size. A numeric greater than 0. If fontSize is NULL, the workbook base font size is used. (Defaults to 11) |
fontColour |
Colour of text in cell. A valid hex colour beginning with "#" or one of colours(). If fontColour is NULL, the workbook base font colours is used. (Defaults to black) |
border |
Cell border. A vector of "top", "bottom", "left", "right" or a single string).
|
borderColour |
Colour of cell border vector the same length as the number of sides specified in "border" A valid colour (belonging to colours()) or a valid hex colour beginning with "#" |
borderStyle |
Border line style vector the same length as the number of sides specified in "border"
|
bgFill |
Cell background fill colour. A valid colour (belonging to colours()) or a valid hex colour beginning with "#". – Use for conditional formatting styles only. |
fgFill |
Cell foreground fill colour. A valid colour (belonging to colours()) or a valid hex colour beginning with "#" |
halign |
Horizontal alignment of cell contents
|
valign |
A name Vertical alignment of cell contents
|
textDecoration |
Text styling.
|
wrapText |
Logical. If |
textRotation |
Rotation of text in degrees. 255 for vertical text. |
indent |
Horizontal indentation of cell contents. |
locked |
Whether cell contents are locked (if worksheet protection is turned on) |
Whether the formula of the cell contents will be hidden (if worksheet protection is turned on) |
An expression passed to the rows
argument can optionally incorporate a .x
selector to refer to multiple columns within the worksheet.
When a .x
selector is used, each column specified in arguments cols
is
independently swapped into the .x
position of the expression, which is then
translated to the Excel formula equivalent and applied as conditional
formatting to the worksheet.
For example, given the following qstyle
specification with respect to the
mtcars
dataset
qstyle( rows = .x == 1, cols = c(vs, am, carb), bgFill = "#FFC7CE" )
the style bgFill = "#FFC7CE"
would be independently applied to any cell in
columns vs
, am
, or carb
with a value of 1
.
# apply style halign = "center" to all data rows (by default rows = "data") qstyle(halign = "center") # apply style halign = "center" to all rows including header qstyle(rows = "all", halign = "center") # apply style halign = "center" to Excel rows 2:10 qstyle(rows = 2:10, halign = "center") # apply conditional formatting to rows where cyl == 8 & mpg > 16 qstyle(cyl == 8 & mpg > 16, fgFill = "#fddbc7", textDecoration = "bold")
# apply style halign = "center" to all data rows (by default rows = "data") qstyle(halign = "center") # apply style halign = "center" to all rows including header qstyle(rows = "all", halign = "center") # apply style halign = "center" to Excel rows 2:10 qstyle(rows = 2:10, halign = "center") # apply conditional formatting to rows where cyl == 8 & mpg > 16 qstyle(cyl == 8 & mpg > 16, fgFill = "#fddbc7", textDecoration = "bold")
Wrapper to openxlsx::saveWorkbook
to write an Excel workbook to file
qwrite(wb, file, overwrite = FALSE)
qwrite(wb, file, overwrite = FALSE)
wb |
A Workbook object to write to file |
file |
A character string naming an xlsx file |
overwrite |
If |
A wrapper to the openxlsx package optimized for writing tidy data frames. Includes arguments to quickly add customization like:
conditional formatting written as R expressions
data validation rules based on a tidy dictionary structure
column-specific worksheet protection
custom column names with original variable-names hidden in the row below
qxl( x, file = NULL, wb = openxlsx::createWorkbook(), sheet = NULL, header = NULL, style_head = qstyle(rows = 1, textDecoration = "bold"), hide_subhead = TRUE, style1 = NULL, style2 = NULL, style3 = NULL, style4 = NULL, style5 = NULL, group, group_style = qstyle(bgFill = "#ffcccb"), row_heights = NULL, col_widths = "auto", freeze_row = 1L, freeze_col = NULL, protect, validate = NULL, validate_cond = NULL, validate_cond_all = NULL, filter = FALSE, filter_cols = everything(), zoom = 120L, date_format = "yyyy-mm-dd", overwrite = TRUE )
qxl( x, file = NULL, wb = openxlsx::createWorkbook(), sheet = NULL, header = NULL, style_head = qstyle(rows = 1, textDecoration = "bold"), hide_subhead = TRUE, style1 = NULL, style2 = NULL, style3 = NULL, style4 = NULL, style5 = NULL, group, group_style = qstyle(bgFill = "#ffcccb"), row_heights = NULL, col_widths = "auto", freeze_row = 1L, freeze_col = NULL, protect, validate = NULL, validate_cond = NULL, validate_cond_all = NULL, filter = FALSE, filter_cols = everything(), zoom = 120L, date_format = "yyyy-mm-dd", overwrite = TRUE )
x |
A data frame, or list of data frames |
file |
Filename to write to. If |
wb |
An openxlsx workbook object to write to. Defaults to a fresh
workbook created with |
sheet |
Optional character vector of worksheet names. If |
header |
Optional column header. Defaults to header = c( mpg = "Miles per US gallon", cyl = "Number of cylinders", disp = "Engine displacement (cubic in.) ) |
style_head |
Style for the header row. Set with |
hide_subhead |
Logical indicating whether to hide the subheader (if present). Defaults to TRUE. |
style1 , style2 , style3 , style4 , style5
|
Optional style to set using |
group |
Optional vector of one or more column names used to create
alternating groupings of rows, with every other row grouping styled as per
argument |
group_style |
Optional style to apply to alternating groupings of rows,
as specified using argument |
row_heights |
Numeric vector of row heights (in Excel units). The vector
is recycled if shorter than the number of rows in |
col_widths |
Vector of column widths (in Excel units). Can be numeric or
character, and may include keyword "auto" for automatic column sizing. The
vector is recycled if shorter than the number of columns in Use named vector to give column widths for specific columns, where names
represent column names of # specify widths for cols mpg and cyl, all others default to "auto" col_widths <- c(mpg = 5, cyl = 10) # specify widths for cols mpg and cyl, and explicit default for all others col_widths <- c(mpg = 5, cyl = 10, .default = 7) |
freeze_row |
Integer specifying a row to freeze at. Defaults to |
freeze_col |
Integer specifying a column to freeze at. Defaults to
|
protect |
Optional function specifying how to protect worksheet
components from user modification. See function |
validate |
Optional specification of list-style data validation for one
or more columns. Can specify either as a list of vectors giving options for
one or more column in list( var_x = c("Yes", "No"), var_y = c("Small", "Medium", "Large") ) or as a data.frame where the first column gives column names and the second column gives corresponding options, e.g.: data.frame( col = c("var_x", "var_x", "var_y", "var_y", "var_y"), val = c("Yes", "No", "Small", "Medium", "Large") ) Validation options are written/appended to a hidden worksheet named "valid_options". |
validate_cond |
Optional specification of conditional list-style
validation, where the set of values to be allowed in a given column depends
on the corresponding value within one or more other columns (e.g. the
allowed values in column 'city' depend on the corresponding value in
columns 'country' and 'province'). Must be a data.frame with at least two
columns, where the first column(s) give the conditional entries (e.g.
'country', 'province') and the last column gives the corresponding allowed
entries (e.g. 'city') to be implemented as data validation. The column
names in Note that in the current implementation validation is based on values in
the conditional column(s) of |
validate_cond_all |
Optional vector of value(s) to always allow, independent of the value in the conditional column (e.g. "Unknown"). |
filter |
Logical indicating whether to add column filters. |
filter_cols |
Tidy-selection specifying which columns to filter. Only
used if |
zoom |
Integer specifying initial zoom percentage. Defaults to 130. |
date_format |
Excel format for date columns. Defaults to "yyyy-mm-dd". |
overwrite |
Logical indicating whether to overwrite existing file.
Defaults to |
If argument file
is not specified, returns an openxlsx workbook object.
Otherwise writes workbook to file with no return.
Given a dataset with multiple rows per group (e.g. repeated observations on a given individual), it can sometimes be useful to uniquely stylize alternating groups to allow for quick visual distinction of the rows belonging to any given group.
Given one or more grouping columns specified using argument groups
, the
qxl
function arranges the rows of the resulting worksheet by group and then
applies the style group_style
to the rows in every other group, to create
an alternating pattern. The alternating pattern is achieved by first creating
a group index variable called g
which is assigned a value of either 1
or
0
: 1
for the 1st group, 0
for the 2nd, 1
for the 3rd, 0
for the
4th, etc. The style specified by group_style
is then applied conditionally
to rows where g == 0
. The grouping variable is written in column A, which
is hidden.
library(datasets) qxl(mtcars, file = tempfile(fileext = ".xlsx"))
library(datasets) qxl(mtcars, file = tempfile(fileext = ".xlsx"))