From dabb26f2766f8156aec81c45994253fa0b1cbd66 Mon Sep 17 00:00:00 2001
From: JanMarvin
Date: Tue, 18 Jun 2024 18:20:07 +0000
Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20JanMarvi?=
=?UTF-8?q?n/openxlsx2@25e0dd4f030cfaf683583009c2b0bfb9cda731cc=20?=
=?UTF-8?q?=F0=9F=9A=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
dev/pkgdown.yml | 2 +-
dev/reference/properties-wb.html | 2 +-
dev/reference/wb_add_conditional_formatting.html | 6 +++---
dev/search.json | 2 +-
4 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/dev/pkgdown.yml b/dev/pkgdown.yml
index e4619521c..0aa4c3fda 100644
--- a/dev/pkgdown.yml
+++ b/dev/pkgdown.yml
@@ -9,7 +9,7 @@ articles:
openxlsx2_style_manual: openxlsx2_style_manual.html
openxlsx2: openxlsx2.html
Update-from-openxlsx: Update-from-openxlsx.html
-last_built: 2024-06-17T11:51Z
+last_built: 2024-06-18T18:19Z
urls:
reference: https://janmarvin.github.io/openxlsx2/reference
article: https://janmarvin.github.io/openxlsx2/articles
diff --git a/dev/reference/properties-wb.html b/dev/reference/properties-wb.html
index e347aafe6..5dddaf3f0 100644
--- a/dev/reference/properties-wb.html
+++ b/dev/reference/properties-wb.html
@@ -153,7 +153,7 @@
diff --git a/dev/search.json b/dev/search.json
index b1aeaad74..a1af3d992 100644
--- a/dev/search.json
+++ b/dev/search.json
@@ -1 +1 @@
-[{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"basic-read-and-write-functions","dir":"Articles","previous_headings":"","what":"Basic read and write functions","title":"Upgrade from openxlsx","text":"Welcome openxlsx2 update vignette. vignette take common code examples openxlsx show similar results can replicated openxlsx2. Thank taking look, let’s get started. previous openxlsx functions used . function calls, well camelCase, tried switch snake_case (still work progress, may still function arguments use camelCase).","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"read-xlsx-or-xlsm-files","dir":"Articles","previous_headings":"Basic read and write functions","what":"Read xlsx or xlsm files","title":"Upgrade from openxlsx","text":"basic read function changed read.xlsx read_xlsx. Using default xlsx file included package: old syntax looked like : changed : can see, return spreadsheet return codes (e.g., #NUM) openxlsx2. Another thing see , return cell row rowname data frame returned. openxlsx2 return data frame selected size, even empty. preferred openxlsx::readWorkbook() become wb_read(). wrappers newly introduced function wb_to_df() provides options. read_xlsx() wb_read() created backward comparability.","code":"file <- system.file(\"extdata\", \"openxlsx2_example.xlsx\", package = \"openxlsx2\") # read in openxlsx openxlsx::read.xlsx(xlsxFile = file) # read in openxlsx2 openxlsx2::read_xlsx(file = file) ## Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 ## 3 TRUE 1 NA 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 ## 4 TRUE NA NA #NUM! b 2023-05-23 0 14:02:57 ## 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 ## 6 FALSE 2 NA #NUM! 2 17:24:53 ## 7 FALSE 3 NA 1.56 e ## 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 ## 9 NA NA NA ## 10 FALSE 2 NA 23 h 2023-12-24 25 ## 11 FALSE 3 NA 67.3 i 2023-12-25 3 ## 12 NA 1 NA 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"write-xlsx-files","dir":"Articles","previous_headings":"","what":"Write xlsx files","title":"Upgrade from openxlsx","text":"Basic writing openxlsx2 behaves identical openxlsx. Though aware overwrite optional parameter openxlsx2 just like functions like base::write.csv() write onto existing file name, file replaced. Setting output temporary xlsx file previous write function looks like : new function looks quite similar:","code":"output <- temp_xlsx() # write in openxlsx openxlsx::write.xlsx(iris, file = output, colNames = TRUE) # write in openxlsx2 openxlsx2::write_xlsx(iris, file = output, col_names = TRUE)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"basic-workbook-functions","dir":"Articles","previous_headings":"","what":"Basic workbook functions","title":"Upgrade from openxlsx","text":"Workbook functions renamed begin wb_ plenty package, therefore looking man pages seems fastest way. Yet, begins loading workbook.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"loading-a-workbook","dir":"Articles","previous_headings":"Basic workbook functions","what":"Loading a workbook","title":"Upgrade from openxlsx","text":"major feature openxlsx workbooks. Obviously remain central piece openxlsx2. Previous load : openxlsx2 loading changed : plenty functions interact workbooks describe every single one . detailed list can found references","code":"wb <- openxlsx::loadWorkbook(file = file) wb <- wb_load(file = file)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"styles","dir":"Articles","previous_headings":"Basic workbook functions","what":"Styles","title":"Upgrade from openxlsx","text":"One biggest user facing change removal stylesObject. following section use code openxlsx::addStyle() openxlsx2 code looks something like : code uses chaining. prefer piping, provide chained functions prefix wb_ wb_add_worksheet(), wb_add_data(), wb_add_border() wb_set_col_widths() functions use pipes %>% |>. pipes code becomes aware chains modify object place pipes . can re-use styles wb_get_cell_style() wb_set_cell_style(). Abandoning stylesObject openxlsx2 huge benefit can import export spreadsheet without changing cell style. still possible modify cell style wb_add_border(), wb_add_fill(), wb_add_font() wb_add_numfmt(). Additional examples regarding styles can found styles vignette.","code":"# openxlsx ## Create a new workbook wb <- createWorkbook(creator = \"My name here\") addWorksheet(wb, \"Expenditure\", gridLines = FALSE) writeData(wb, sheet = 1, USPersonalExpenditure, rowNames = TRUE) ## style for body bodyStyle <- createStyle(border = \"TopBottom\", borderColor = \"#4F81BD\") addStyle(wb, sheet = 1, bodyStyle, rows = 2:6, cols = 1:6, gridExpand = TRUE) ## set column width for row names column setColWidths(wb, 1, cols = 1, widths = 21) # openxlsx2 chained border_color <- wb_color(hex = \"4F81BD\") wb <- wb_workbook(creator = \"My name here\")$ add_worksheet(\"Expenditure\", grid_lines = FALSE)$ add_data(x = USPersonalExpenditure, row_names = TRUE)$ add_border( # add the outer and inner border dims = \"A1:F6\", top_border = \"thin\", top_color = border_color, bottom_border = \"thin\", bottom_color = border_color, inner_hgrid = \"thin\", inner_hcolor = border_color, left_border = \"\", right_border = \"\" )$ set_col_widths( # set column width cols = 1:6, widths = c(20, rep(10, 5)) )$ # remove the value in A1 add_data(dims = \"A1\", x = \"\") # openxlsx2 with pipes border_color <- wb_color(hex = \"4F81BD\") wb <- wb_workbook(creator = \"My name here\") %>% wb_add_worksheet(sheet = \"Expenditure\", grid_lines = FALSE) %>% wb_add_data(x = USPersonalExpenditure, row_names = TRUE) %>% wb_add_border( # add the outer and inner border dims = \"A1:F6\", top_border = \"thin\", top_color = border_color, bottom_border = \"thin\", bottom_color = border_color, inner_hgrid = \"thin\", inner_hcolor = border_color, left_border = \"\", right_border = \"\" ) %>% wb_set_col_widths( # set column width cols = 1:6, widths = c(20, rep(10, 5)) ) %>% # remove the value in A1 wb_add_data(dims = \"A1\", x = \"\") # openxlsx2 wbp <- wb_workbook() %>% wb_add_worksheet() wbc <- wb_workbook()$add_worksheet() # need to assign wbp wbp <- wbp %>% wb_add_data(x = iris) wbc$add_data(x = iris)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"conditional-formatting","dir":"Articles","previous_headings":"Basic workbook functions","what":"Conditional formatting","title":"Upgrade from openxlsx","text":"See vignette(\"conditional-formatting\") extended examples formatting. minimal example:","code":"# openxlsx2 with chains wb <- wb_workbook()$ add_worksheet(\"a\")$ add_data(x = 1:4, col_names = FALSE)$ add_conditional_formatting(dims = \"A1:A4\", rule = \">2\") # openxlsx2 with pipes wb <- wb_workbook() %>% wb_add_worksheet(\"a\") %>% wb_add_data(x = 1:4, col_names = FALSE) %>% wb_add_conditional_formatting(dims = \"A1:A4\", rule = \">2\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"data-validation","dir":"Articles","previous_headings":"Basic workbook functions","what":"Data validation","title":"Upgrade from openxlsx","text":"Similarly, data validation updated improved. openxlsx code data validation looks openxlsx2 something like :","code":"# openxlsx wb <- createWorkbook() addWorksheet(wb, \"Sheet 1\") writeDataTable(wb, 1, x = iris[1:30, ]) dataValidation(wb, 1, col = 1:3, rows = 2:31, type = \"whole\", operator = \"between\", value = c(1, 9) ) # openxlsx2 with chains wb <- wb_workbook()$ add_worksheet(\"Sheet 1\")$ add_data_table(1, x = iris[1:30, ])$ add_data_validation(1, dims = wb_dims(rows = 2:31, cols = 1:3), # alternatively, dims can also be \"A2:C31\" if you know the span in your Excel workbook. type = \"whole\", operator = \"between\", value = c(1, 9) ) # openxlsx2 with pipes wb <- wb_workbook() %>% wb_add_worksheet(\"Sheet 1\") %>% wb_add_data_table(1, x = iris[1:30, ]) %>% wb_add_data_validation( sheet = 1, dims = \"A2:C31\", # alternatively, dims = wb_dims(rows = 2:31, cols = 1:3) type = \"whole\", operator = \"between\", value = c(1, 9) )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"saving","dir":"Articles","previous_headings":"Basic workbook functions","what":"Saving","title":"Upgrade from openxlsx","text":"Saving switched saveWorkbook() wb_save() opening workbook switched openXL() wb_open().","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"why-openxlsx2","dir":"Articles","previous_headings":"","what":"Why openxlsx2?","title":"Upgrade from openxlsx","text":"Originally, openxlsx2 started private branch openxlsx include pugixml library provide fully functional XML parser openxlsx. time, became clear home-written openxlsx XML parser limited ability reliably parse XML files, leading problems broken unreadable xlsx files. inclusion pugixml addressed, new internal structure created, structure required changes old openxlsx functions. accompanied change methods R6 possibility chaining piping functions. Working styles object openxlsx became clear great idea, work well enough needs files loaded modified openxlsx never look . always things look little style objects work perfectly. Likewise, lot edge cases openxlsx assume file structure xlsx objects simplified approximation actually going . example, openxlsx assumes sheet accompanied drawing. works many cases, match definition format openxml standard. may worksheets multiple drawings, drawing folder drawings included. Unfortunately, many things deeply embedded openxlsx code, development took place openxlsx2, things differed fork origin. point fork called independent project previously privately developed branch made public. say went hand hand modification actual project goal. , creating similar looking xlsx file able partially edit . Now writing identical xlsx file just able change everything. Since internal functions openxlsx cleaned , fixed mostly rewritten. package developed new ways handle styles styles manager, provides full range style options hard impossible include openxlsx. included support native graphs mschart feature creation pivot tables. support conditional formatting options, improved support data validation, sparklines form control objects. addition many quirks old package ironed . switched consistent stable API build dims provide multiple vignettes document code plenty functions interact openxml format. provide basic xlsb support msoc created package encrypt decrypt openxml files.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/Update-from-openxlsx.html","id":"invitation-to-contribute","dir":"Articles","previous_headings":"","what":"Invitation to contribute","title":"Upgrade from openxlsx","text":"put lot work openxls2 make useful needs, improving found useful openxlsx removing didn’t need. claim omniscient things can spreadsheet software, claim omniscient things can openxlsx2. package still active development, though reached semi stable API change next major release. quite fond little package invite others try comment like course think missing something doesn’t work. openxlsx2 complex piece software certainly work bug-free, even best. want contribute development openxlsx2, please guest Github. Join open discussion, post fix issues write us mail.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"rule-applies-to-all-each-cell-in-range","dir":"Articles","previous_headings":"","what":"Rule applies to all each cell in range","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"cellIs\") wb$add_data(\"cellIs\", -5:5) wb$add_data(\"cellIs\", LETTERS[1:11], start_col = 2) wb$add_conditional_formatting( \"cellIs\", dims = \"A1:A11\", rule = \"!=0\", style = \"negStyle\" ) wb$add_conditional_formatting( \"cellIs\", dims = \"A1:A11\", rule = \"==0\", style = \"posStyle\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"highlight-row-dependent-on-first-cell-in-row","dir":"Articles","previous_headings":"","what":"Highlight row dependent on first cell in row","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"Moving Row\") wb$add_data(\"Moving Row\", -5:5) wb$add_data(\"Moving Row\", LETTERS[1:11], start_col = 2) wb$add_conditional_formatting( \"Moving Row\", dims = \"A1:B11\", rule = \"$A1<0\", style = \"negStyle\" ) wb$add_conditional_formatting( \"Moving Row\", dims = \"A1:B11\", rule = \"$A1>0\", style = \"posStyle\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"highlight-column-dependent-on-first-cell-in-column","dir":"Articles","previous_headings":"","what":"Highlight column dependent on first cell in column","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"Moving Col\") wb$add_data(\"Moving Col\", -5:5) wb$add_data(\"Moving Col\", LETTERS[1:11], start_col = 2) wb$add_conditional_formatting( \"Moving Col\", dims = \"A1:B11\", rule = \"A$1<0\", style = \"negStyle\" ) wb$add_conditional_formatting( \"Moving Col\", dims = \"A1:B11\", rule = \"A$1>0\", style = \"posStyle\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"highlight-entire-range-cols-x-rows-dependent-only-on-cell-a1","dir":"Articles","previous_headings":"","what":"Highlight entire range cols X rows dependent only on cell A1","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"Dependent on\") wb$add_data(\"Dependent on\", -5:5) wb$add_data(\"Dependent on\", LETTERS[1:11], start_col = 2) wb$add_conditional_formatting( \"Dependent on\", dims = \"A1:B11\", rule = \"$A$1 < 0\", style = \"negStyle\" ) wb$add_conditional_formatting( \"Dependent on\", dims = \"A1:B11\", rule = \"$A$1>0\", style = \"posStyle\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"highlight-cells-in-column-1-based-on-value-in-column-2","dir":"Articles","previous_headings":"","what":"Highlight cells in column 1 based on value in column 2","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_data(\"Dependent on\", data.frame(x = 1:10, y = runif(10)), startRow = 15) wb$add_conditional_formatting( \"Dependent on\", dims = \"A16:A25\", rule = \"B16<0.5\", style = \"negStyle\" ) wb$add_conditional_formatting( \"Dependent on\", dims = \"A16:A25\", rule = \"B16>=0.5\", style = \"posStyle\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"highlight-duplicates-using-default-style","dir":"Articles","previous_headings":"","what":"Highlight duplicates using default style","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"Duplicates\") wb$add_data(\"Duplicates\", sample(LETTERS[1:15], size = 10, replace = TRUE)) wb$add_conditional_formatting( \"Duplicates\", dims = \"A1:A10\", type = \"duplicatedValues\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"cells-containing-text","dir":"Articles","previous_headings":"","what":"Cells containing text","title":"Add conditional formatting to a workbook","text":"","code":"fn <- function(x) paste(sample(LETTERS, 10), collapse = \"-\") wb$add_worksheet(\"containsText\") wb$add_data(\"containsText\", sapply(1:10, fn)) wb$add_conditional_formatting( \"containsText\", dims = \"A1:A10\", type = \"containsText\", rule = \"A\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"cells-not-containing-text","dir":"Articles","previous_headings":"","what":"Cells not containing text","title":"Add conditional formatting to a workbook","text":"","code":"fn <- function(x) paste(sample(LETTERS, 10), collapse = \"-\") my_dat <- sapply(1:10, fn) wb$add_worksheet(\"notcontainsText\") wb$add_data(\"notcontainsText\", x = my_dat) wb$add_conditional_formatting( \"notcontainsText\", dims = wb_dims(x = my_dat), type = \"notContainsText\", rule = \"A\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"cells-begins-with-text","dir":"Articles","previous_headings":"","what":"Cells begins with text","title":"Add conditional formatting to a workbook","text":"","code":"fn <- function(x) paste(sample(LETTERS, 10), collapse = \"-\") wb$add_worksheet(\"beginsWith\") wb$add_data(\"beginsWith\", sapply(1:100, fn)) wb$add_conditional_formatting( \"beginsWith\", dims = \"A1:A100\", type = \"beginsWith\", rule = \"A\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"cells-ends-with-text","dir":"Articles","previous_headings":"","what":"Cells ends with text","title":"Add conditional formatting to a workbook","text":"","code":"fn <- function(x) paste(sample(LETTERS, 10), collapse = \"-\") wb$add_worksheet(\"endsWith\") wb$add_data(\"endsWith\", x = sapply(1:100, fn)) wb$add_conditional_formatting( \"endsWith\", dims = \"A1:A100\", type = \"endsWith\", rule = \"A\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"colorscale-colors-cells-based-on-cell-value","dir":"Articles","previous_headings":"","what":"Colorscale colors cells based on cell value","title":"Add conditional formatting to a workbook","text":"Yep, color scale image. Rule vector colors length 2 3 (hex color colors()). rule NULL, min max cells used. Rule must length style L.","code":"df <- read_xlsx(\"https://github.com/JanMarvin/openxlsx-data/raw/main/readTest.xlsx\", sheet = 5) wb$add_worksheet(\"colorScale\", zoom = 30) wb$add_data(x = df, col_names = FALSE) ## write data.frame wb$add_conditional_formatting( sheet = \"colorScale\", dims = wb_dims( rows = seq_len(nrow(df)), cols = seq_len(ncol(df)) ), # dims = wb_dims(x = df, col_names = FALSE, select = \"data\") style = c(\"black\", \"white\"), rule = c(0, 255), type = \"colorScale\" ) wb$set_col_widths(\"colorScale\", cols = seq_along(df), widths = 1.07) wb$set_row_heights(\"colorScale\", rows = seq_len(nrow(df)), heights = 7.5)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"databars","dir":"Articles","previous_headings":"","what":"Databars","title":"Add conditional formatting to a workbook","text":"","code":"wb$add_worksheet(\"databar\") ## Databars wb$add_data(\"databar\", -5:5, start_col = 1) wb <- wb_add_conditional_formatting( wb, \"databar\", dims = \"A1:A11\", type = \"dataBar\" ) ## Default colors wb$add_data(\"databar\", -5:5, start_col = 3) wb <- wb_add_conditional_formatting( wb, \"databar\", dims = \"C1:C11\", type = \"dataBar\", params = list( showValue = FALSE, gradient = FALSE ) ) ## Default colors wb$add_data(\"databar\", -5:5, start_col = 5) wb <- wb_add_conditional_formatting( wb, sheet = \"databar\", dims = \"E1:E11\", type = \"dataBar\", style = c(\"#a6a6a6\"), params = list(showValue = FALSE) ) wb$add_data(\"databar\", -5:5, start_col = 7) wb <- wb_add_conditional_formatting( wb, \"databar\", dims = \"G1:G11\", type = \"dataBar\", style = c(\"red\"), params = list( showValue = TRUE, gradient = FALSE ) ) # custom color wb$add_data(\"databar\", -5:5, start_col = 9) wb <- wb_add_conditional_formatting( wb, \"databar\", dims = wb_dims(rows = 1:11, cols = 9), type = \"dataBar\", style = c(\"#a6a6a6\", \"#a6a6a6\"), params = list(showValue = TRUE, gradient = FALSE) ) # with rule wb$add_data(x = -5:5, start_col = 11) wb <- wb_add_conditional_formatting( wb, \"databar\", dims = wb_dims(rows = 1:11, cols = 11), type = \"dataBar\", rule = c(0, 5), style = c(\"#a6a6a6\", \"#a6a6a6\"), params = list(showValue = TRUE, gradient = FALSE) )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"between","dir":"Articles","previous_headings":"","what":"Between","title":"Add conditional formatting to a workbook","text":"Highlight cells interval [-2, 2]","code":"wb$add_worksheet(\"between\") wb$add_data(\"between\", -5:5) wb$add_conditional_formatting( \"between\", dims = wb_dims(rows = 2:11, cols = 1), type = \"between\", rule = c(-2, 2) ) wb$add_worksheet(\"topN\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"top-n","dir":"Articles","previous_headings":"","what":"Top N","title":"Add conditional formatting to a workbook","text":"Highlight top 5 values column x Highlight top 20 percentage column y","code":"wb$add_data(\"topN\", data.frame(x = 1:10, y = rnorm(10))) wb$add_conditional_formatting( \"topN\", dims = wb_dims(rows = 2:11, cols = 1), style = \"posStyle\", type = \"topN\", params = list(rank = 5) ) wb$add_conditional_formatting( \"topN\", dims = wb_dims(rows = 2:11, cols = 2), style = \"posStyle\", type = \"topN\", params = list(rank = 20, percent = TRUE) ) wb$add_worksheet(\"bottomN\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"bottom-n","dir":"Articles","previous_headings":"","what":"Bottom N","title":"Add conditional formatting to a workbook","text":"Highlight bottom 5 values column x Highlight bottom 20 percentage column y","code":"wb$add_data(\"bottomN\", data.frame(x = 1:10, y = rnorm(10))) wb$add_conditional_formatting( \"bottomN\", dims = wb_dims(rows = 2:11, cols = 1), style = \"negStyle\", type = \"bottomN\", params = list(rank = 5) ) wb$add_conditional_formatting( \"bottomN\", cols = 2, rows = 2:11, style = \"negStyle\", type = \"bottomN\", params = list(rank = 20, percent = TRUE) ) wb$add_worksheet(\"logical operators\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/conditional-formatting.html","id":"logical-operators","dir":"Articles","previous_headings":"","what":"Logical Operators","title":"Add conditional formatting to a workbook","text":"can use Excels logical Operators","code":"wb$add_data(\"logical operators\", 1:10) wb$add_conditional_formatting( \"logical operators\", dims = wb_dims(rows = 1:10, cols = 1), rule = \"OR($A1=1,$A1=3,$A1=5,$A1=7)\" )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"first-steps","dir":"Articles","previous_headings":"","what":"First steps","title":"openxlsx2 basic manual","text":"First let’s assume working installation openxlsx2 otherwise run lines install latest CRAN release: Now load library:","code":"install.packages(\"openxlsx2\") library(openxlsx2)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"handling-workbooks","dir":"Articles","previous_headings":"","what":"Handling workbooks","title":"openxlsx2 basic manual","text":"foundation openxlsx2 workbook object. can think object workbook loaded spreadsheet software. import entire thing. Every sheet, every chart, image, column, formula style, conditional formatting, pivot table whatever else spreadsheet file allowed carry. Therefore file want work , can load : usually name workbook objects wb documentation, obligation, can name workbook object whatever like call . workbook yet, possible create one. next line use three wrapper functions wb_workbook(), wb_add_worksheet(), wb_add_data(). wrapper functions piped together using classic magrittr pipe operator %>%, similarly can use R’s native pipe operator |>. 1 assume dataset your_data, either vector, matrix data frame want write worksheet: Okay, now workbook object, actually done? Let’s work along pipe syntax: (1) first created workbook object wb_worbkook(), (2) next assigned worksheet wb_add_worksheet(), (3) written data onto worksheet. Let’s try actual data. use mtcars dataset. code switch fictional your_data mtcars: Let’s see output looks like: output looks little cryptic, simply tells name worksheet: wb_add_worksheet() created default worksheet name \"Sheet 1\". code can see use sheet tell wb_add_data() write data. internally use waiver current_sheet() write sheet = \"Sheet 1\" whenever work worksheet. Basically current sheet updated whenever new worksheet added workbook. create two sheets \"Sheet 1\" \"Sheet 2\" data written second sheet. can access data sheet? Either wb_to_df() internal handler read workbooks (underlying function wb_read() read_xlsx() mere aliases wb_to_df()). lets look top output: Ah! output second sheet. need either sheet = 2 sheet = \"Sheet 2\". go second variant, sheet index position name might differ. looks like head mtcars dataset. successfully read workbook. Now want export workbook file: Alternatively can directly open spreadsheet software (one installed): , lets try USPersonalExpenditure dataset: Dang! ? ’ve added worksheet, wait, ? , assign wrapper functions object effect. Wrapper functions alter workbook objects executed . can check workbook worksheets: assign sheet, changes, data correctly written: Now ’re probably thinking, don’t want assign workbook object time wb_ functions little tedious type. alternative called chaining. Since workbook R6 object internally, can make use chains. Basically every function starts wb_ underlying function name without prefix. data writing example can written : Whether use wrapper functions chain functions personal preference. just one thing remember, documentation exclusively written wrapper function. want know arguments wb$add_data() part, lookup wrapper functions man page ?wb_add_data.","code":"wb <- wb_load(\"your_file.xlsx\") wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = your_data) wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars) wb #> A Workbook object. #> #> Worksheets: #> Sheets: Sheet 1 #> Write order: 1 wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars) wb #> A Workbook object. #> #> Worksheets: #> Sheets: Sheet 1, Sheet 2 #> Write order: 1, 2 wb %>% wb_to_df() %>% head() #> sheet found, but contains no data #> NULL wb %>% wb_to_df(sheet = \"Sheet 2\") %>% head() #> mpg cyl disp hp drat wt qsec vs am gear carb #> 2 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 #> 3 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 #> 4 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 #> 5 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> 6 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 #> 7 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 wb %>% wb_save(file = \"my_first_worksheet.xlsx\") wb %>% wb_open() wb <- wb_workbook() wb_add_worksheet(wb, sheet = \"USexp\") wb_add_data(wb, \"USexp\", USPersonalExpenditure) #> Error : Can't add data to a workbook with no worksheet. #> Did you forget to add a worksheet with `wb_add_worksheet()`? wb %>% wb_get_sheet_names() #> named character(0) wb <- wb_workbook() wb <- wb_add_worksheet(wb, sheet = \"USexp\") wb <- wb_add_data(wb, \"USexp\", USPersonalExpenditure) wb_get_sheet_names(wb) #> USexp #> \"USexp\" wb_to_df(wb) #> 1940 1945 1950 1955 1960 #> 2 22.200 44.500 59.60 73.2 86.80 #> 3 10.500 15.500 29.00 36.5 46.20 #> 4 3.530 5.760 9.71 14.0 21.10 #> 5 1.040 1.980 2.45 3.4 5.40 #> 6 0.341 0.974 1.80 2.6 3.64 wb <- wb_workbook()$add_worksheet(\"USexp\")$add_data(x = USPersonalExpenditure) wb$to_df() #> 1940 1945 1950 1955 1960 #> 2 22.200 44.500 59.60 73.2 86.80 #> 3 10.500 15.500 29.00 36.5 46.20 #> 4 3.530 5.760 9.71 14.0 21.10 #> 5 1.040 1.980 2.45 3.4 5.40 #> 6 0.341 0.974 1.80 2.6 3.64"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"importing-as-workbook","dir":"Articles","previous_headings":"","what":"Importing as workbook","title":"openxlsx2 basic manual","text":"addition importing directly xlsx xlsm files, openxlsx2 provides wbWorkbook class used importing modifying entire openxml files R. workbook class heart openxlsx2 probably reason reading manual first place. Importing file workbook looks like : additional options wb_load() provides internal use: sheet loads selected sheet workbook data_only reads data parts workbook ignores additional graphics pivot tables. functions create workbook objects can used read data, recommend end users use . Especially intend re-export workbook afterwards. workbook imported, provide several functions interact modify (wb_to_df() function mentioned works way imported workbook). possible add new sheets remove sheets, well add remove data. R-plots can inserted also style workbook can changed, new fonts, background colors number formats. wealth options explained man pages additional style vignette (vignettes follow).","code":"# the file we are going to load file <- system.file(\"extdata\", \"openxlsx2_example.xlsx\", package = \"openxlsx2\") # loading the file into the workbook wb <- wb_load(file = file)"},{"path":[]},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"exporting-data-frames-or-vectors","dir":"Articles","previous_headings":"Exporting data","what":"Exporting data frames or vectors","title":"openxlsx2 basic manual","text":"want export data frame R, can use write_xlsx() create xlsx file. file can tweaked . See ?write_xlsx see options. (explanation examples follow).","code":"write_xlsx(x = mtcars, file = \"mtcars.xlsx\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"exporting-a-wbworkbook","dir":"Articles","previous_headings":"Exporting data","what":"Exporting a wbWorkbook","title":"openxlsx2 basic manual","text":"Imported workbooks can saved xlsx xlsm files wrapper wb_save() wb$save(). functions take filename optional overwrite option. latter set, optional guard provided check file want write already exists. careful, optional. default save file replace existing file. course, Windows, files locked (example, opened another process) replaced.","code":"# replace the existing file wb$save(\"mtcars.xlsx\") # do not overwrite the existing file try(wb$save(\"mtcars.xlsx\", overwrite = FALSE))"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"dims-wb_dims","dir":"Articles","previous_headings":"","what":"dims/ wb_dims()","title":"openxlsx2 basic manual","text":"openxlsx2 functions interact worksheet cells using dims argument require users provide . dims cells cell ranges A1 notation. single argument dims hereby replaces col/row, cols/rows xy. Since A1 notation rather simple first columns might get confusing 26. Therefore provide wrapper construct :","code":"# various options wb_dims(from_row = 4) #> [1] \"A4\" wb_dims(rows = 4, cols = 4) #> [1] \"D4\" wb_dims(rows = 4, cols = \"D\") #> [1] \"D4\" wb_dims(rows = 4:10, cols = 5:9) #> [1] \"E4:I10\" wb_dims(rows = 4:10, cols = \"A:D\") # same as below #> [1] \"A4:D10\" wb_dims(rows = seq_len(7), cols = seq_len(4), from_row = 4) #> [1] \"A4:D10\" # 10 rows and 15 columns from indice B2. wb_dims(rows = 1:10, cols = 1:15, from_col = \"B\", from_row = 2) #> [1] \"B2:P11\" # data + col names wb_dims(x = mtcars) #> [1] \"A1:K33\" # only data wb_dims(x = mtcars, select = \"data\") #> [1] \"A2:K33\" # The dims of the values of a column in `x` wb_dims(x = mtcars, cols = \"cyl\") #> [1] \"B2:B33\" # a column in `x` with the column name wb_dims(x = mtcars, cols = \"cyl\", select = \"x\") #> [1] \"B1:B33\" # rows in `x` wb_dims(x = mtcars) #> [1] \"A1:K33\" # in a wb chain wb <- wb_workbook()$ add_worksheet()$ add_data(x = mtcars)$ add_fill( dims = wb_dims(x = mtcars, rows = 1:5), # only 1st 5 rows of x data color = wb_color(\"yellow\") )$ add_fill( dims = wb_dims(x = mtcars, select = \"col_names\"), # only column names color = wb_color(\"cyan2\") ) # or if the data's first coord needs to be located in B2. wb_dims_custom <- function(...) { wb_dims(x = mtcars, from_col = \"B\", from_row = 2, ...) } wb <- wb_workbook()$ add_worksheet()$ add_data(x = mtcars, dims = wb_dims_custom())$ add_fill( dims = wb_dims_custom(rows = 1:5), color = wb_color(\"yellow\") )$ add_fill( dims = wb_dims_custom(select = \"col_names\"), color = wb_color(\"cyan2\") )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2.html","id":"a-note-on-speed-and-memory-usage","dir":"Articles","previous_headings":"","what":"A note on speed and memory usage","title":"openxlsx2 basic manual","text":"current state openxlsx2 reasonably fast. , works well reasonably large input data reading writing. may work well data tests limits openxml specification. Things may slow R side things, performance usability depend speed size local operating system’s CPU memory. Note least two cases openxlsx2 constructs potentially large data frames () loading, openxlsx2 usually needs read entire input file pugixml convert long data frame(s), wb_to_df() converts one long data frame two data frames construct output object (ii) adding data workbook, openxlsx2 reshapes input data frame long data frame stores workbook, writes entire worksheet pugixml file written complete. Applying cell styles, date conversions etc. slow process finally sheets zipped provide xlsx output. Therefore, faced unreasonably large dataset, either give enough time, use another package write xlsx output (openxlsx2 written intention working maximum memory efficiency), means use ways store data (binary file formats database). However, always happy improve, found way improve currently , please let us know open issue pull request.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_charts_manual.html","id":"section","dir":"Articles","previous_headings":"","what":"Add charts to a workbook","title":"Add charts to a workbook","text":"","code":"library(openxlsx2) # openxlsx2 >= 0.4 for mschart and rvg support ## create a workbook wb <- wb_workbook()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_charts_manual.html","id":"add-plot-to-workbook","dir":"Articles","previous_headings":"","what":"Add plot to workbook","title":"Add charts to a workbook","text":"can include image PNG JPEG format. Simply open device save output pass worksheet wb_add_image().","code":"myplot <- tempfile(fileext = \".jpg\") jpeg(myplot) print(plot(AirPassengers)) #> NULL dev.off() #> agg_png #> 2 # Add basic plots to the workbook wb$add_worksheet(\"add_image\")$add_image(file = myplot)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_charts_manual.html","id":"add-ggplot2-plot-to-workbook","dir":"Articles","previous_headings":"","what":"Add {ggplot2} plot to workbook","title":"Add charts to a workbook","text":"can include ggplot2 plots similar include openxlsx. Call plot first afterwards use wb_add_plot().","code":"if (requireNamespace(\"ggplot2\")) { library(ggplot2) p <- ggplot(mtcars, aes(x = mpg, fill = as.factor(gear))) + ggtitle(\"Distribution of Gas Mileage\") + geom_density(alpha = 0.5) print(p) # Add ggplot to the workbook wb$add_worksheet(\"add_plot\")$ add_plot(width = 5, height = 3.5, file_type = \"png\", units = \"in\") } #> Loading required namespace: ggplot2"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_charts_manual.html","id":"add-plot-via-rvg","dir":"Articles","previous_headings":"","what":"Add plot via {rvg}","title":"Add charts to a workbook","text":"want vector graphics can modified spreadsheet software dml_xlsx() device comes handy. can pass output via wb_add_drawing().","code":"if (requireNamespace(\"ggplot2\") && requireNamespace(\"rvg\")) { library(rvg) ## create rvg example p <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point() + labs(title = \"With font Bradley Hand\") + theme_minimal(base_family = \"sans\", base_size = 18) tmp <- tempfile(fileext = \".xml\") rvg::dml_xlsx(file = tmp, fonts = list(sans = \"Bradley Hand\")) print(p) dev.off() # Add rvg to the workbook wb$add_worksheet(\"add_drawing\")$ add_drawing(xml = tmp)$ add_drawing(xml = tmp, dims = NULL) } #> Loading required namespace: rvg"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_charts_manual.html","id":"add-mschart-plots","dir":"Articles","previous_headings":"","what":"Add {mschart} plots","title":"Add charts to a workbook","text":"want native open xml charts, look mschart. Create one chart files pass workbook wb_add_mschart(). two options possible. 1. Either default mschart output identical one officer. Passing data object let mschart prepare data. case wb_add_mschart() add new data region. 2. Passing wb_data() object mschart. object contains references data worksheet allows using data “”.","code":"if (requireNamespace(\"mschart\")) { library(mschart) # mschart >= 0.4 for openxlsx2 support ## create chart from mschart object (this creates new input data) mylc <- ms_linechart( data = browser_ts, x = \"date\", y = \"freq\", group = \"browser\" ) wb$add_worksheet(\"add_mschart\")$add_mschart(dims = \"A10:G25\", graph = mylc) ## create chart referencing worksheet cells as input # write data starting at B2 wb$add_worksheet(\"add_mschart - wb_data\")$ add_data(x = mtcars, dims = \"B2\")$ add_data(x = data.frame(name = rownames(mtcars)), dims = \"A2\") # create wb_data object this will tell this mschart # from this PR to create a file corresponding to openxlsx2 dat <- wb_data(wb, dims = \"A2:G10\") # create a few mscharts scatter_plot <- ms_scatterchart( data = dat, x = \"mpg\", y = c(\"disp\", \"hp\") ) bar_plot <- ms_barchart( data = dat, x = \"name\", y = c(\"disp\", \"hp\") ) area_plot <- ms_areachart( data = dat, x = \"name\", y = c(\"disp\", \"hp\") ) line_plot <- ms_linechart( data = dat, x = \"name\", y = c(\"disp\", \"hp\"), labels = c(\"disp\", \"hp\") ) # add the charts to the data wb <- wb %>% wb_add_mschart(dims = \"F4:L20\", graph = scatter_plot) %>% wb_add_mschart(dims = \"F21:L37\", graph = bar_plot) %>% wb_add_mschart(dims = \"M4:S20\", graph = area_plot) %>% wb_add_mschart(dims = \"M21:S37\", graph = line_plot) # add chartsheet wb <- wb %>% wb_add_chartsheet() %>% wb_add_mschart(graph = scatter_plot) } #> Loading required namespace: mschart"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_formulas_manual.html","id":"simple-formulas","dir":"Articles","previous_headings":"","what":"Simple formulas","title":"Add formulas to a workbook","text":"","code":"wb <- wb_workbook()$add_worksheet()$ add_data(x = head(cars))$ add_formula(x = \"SUM(A2, B2)\", dims = \"D2\")$ add_formula(x = \"A2 + B2\", dims = \"D3\") # wb$open()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_formulas_manual.html","id":"array-formulas","dir":"Articles","previous_headings":"","what":"Array formulas","title":"Add formulas to a workbook","text":"","code":"wb <- wb_workbook()$add_worksheet()$ add_data(x = head(cars))$ add_formula(x = \"A2:A7 * B2:B7\", dims = \"C2:C7\", array = TRUE) # wb$open()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_formulas_manual.html","id":"array-formulas-creating-multiple-fields","dir":"Articles","previous_headings":"","what":"Array formulas creating multiple fields","title":"Add formulas to a workbook","text":"example want use MMULT() creates matrix multiplication. requires us write array formula specify region output written . Similar coefficients linear regression","code":"m1 <- matrix(1:6, ncol = 2) m2 <- matrix(7:12, nrow = 2) wb <- wb_workbook()$add_worksheet()$ add_data(x = m1)$ add_data(x = m2, dims = wb_dims(from_col = 4))$ add_formula(x = \"MMULT(A2:B4, D2:F3)\", dims = \"H2:J4\", array = TRUE) # wb$open() # we expect to find this in D1:E1 coef(lm(head(cars))) #> (Intercept) dist #> 5.2692308 0.1153846 wb <- wb_workbook()$add_worksheet()$ add_data(x = head(cars))$ add_formula(x = \"LINEST(A2:A7, B2:B7, TRUE)\", dims = \"D2:E2\", array = TRUE) # wb$open()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_formulas_manual.html","id":"cells-metadata-cm-formulas","dir":"Articles","previous_headings":"","what":"cells metadata (cm) formulas","title":"Add formulas to a workbook","text":"Similar array formulas, cell metadata (cm) formulas hide user array formulas. Using implemented openxlsx2 > 0.6.1:","code":"wb <- wb_workbook()$add_worksheet()$ add_data(x = head(cars))$ add_formula(x = \"SUM(ABS(A2:A7))\", dims = \"D2\", cm = TRUE) #> Warning in write_data2(wb = wb, sheet = sheet, data = x, name = name, colNames #> = colNames, : modifications with cm formulas are experimental. use at own risk # wb$open()"},{"path":[]},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_formulas_manual.html","id":"datatable-formula-differences","dir":"Articles","previous_headings":"dataTable formulas1","what":"dataTable formula differences","title":"Add formulas to a workbook","text":"Given basic table like , similarly basic formula total_sales “= A2 * C2” row value changing row. implementation formula using wb_add_formula() look (taken current documentation) lets say ’ve read data assigned table company_sales create workbook, worksheet, use wb_add_data_table(). One advantages open xml dataTable syntax don’t specify row numbers columns letters. table also grows dynamically, adding new rows new data appended extending formulas new rows. dataTable named columns can use instead letters. writing formulas within dataTable use following syntax [@[column_name]] reference current row. “total_sales” formula written open xml dataTable look like ; =[@[sales_price]] * [@[sales_quantity]] writing formula outside dataTable reference table name. case lets say table name ‘daily_sales’ =daily_sales[@[sales_price]] * daily_sales[@[sales_quantity]] However, pass text formula written cause error syntax open xml requires selecting current row different. open xml dataTable formula looks like : Now can see open xml replaces [@[sales_price]] daily_sales[[#Row],[sales_price]] must use syntax writing formulas dataTable open workbook view table created can see formula worked. can also see replaced [#Row] @. completion, formula wrote appears ;","code":"## creating example data company_sales <- data.frame( sales_price = c(20, 30, 40), COGS = c(5, 11, 13), sales_quantity = c(1, 2, 3) ) ## write in the formula company_sales$total_sales <- paste(paste0(\"A\", 1:3 + 1L), paste0(\"C\", 1:3 + 1L), sep = \" * \") ## add the formula class class(company_sales$total_sales) <- c(class(company_sales$total_sales), \"formula\") ## write a workbook wb <- wb_workbook()$ add_worksheet(\"Total Sales\")$ add_data_table(x = company_sales) daily_sales[[#This Row],[sales_price]]*daily_sales[[#ThisRow],[sales_quantity]] <\/calculatedColumnFormula> ## Because we want the `dataTable` formula to propagate down the entire column of the data ## we can assign the formula by itself to any column and allow that single string to be repeated for each row. ## creating example data example_data <- data.frame( sales_price = c(20, 30, 40), COGS = c(5, 11, 13), sales_quantity = c(1, 2, 3) ) ## base R method example_data$gross_profit <- \"daily_sales[[#This Row],[sales_price]] - daily_sales[[#This Row],[COGS]]\" example_data$total_COGS <- \"daily_sales[[#This Row],[COGS]] * daily_sales[[#This Row],[sales_quantity]]\" example_data$total_sales <- \"daily_sales[[#This Row],[sales_price]] * daily_sales[[#This Row],[sales_quantity]]\" example_data$total_gross_profit <- \"daily_sales[[#This Row],[total_sales]] - daily_sales[[#This Row],[total_COGS]]\" class(example_data$gross_profit) <- c(class(example_data$gross_profit), \"formula\") class(example_data$total_COGS) <- c(class(example_data$total_COGS), \"formula\") class(example_data$total_sales) <- c(class(example_data$total_sales), \"formula\") class(example_data$total_gross_profit) <- c(class(example_data$total_gross_profit), \"formula\") wb$ add_worksheet(\"Daily Sales\")$ add_data_table( x = example_data, table_style = \"TableStyleMedium2\", table_name = \"daily_sales\" ) #### sum dataTable examples wb$add_worksheet(\"sum_examples\") ### Note: dataTable formula do not need to be used inside of dataTables. dataTable formula are for referencing the data within the dataTable. ### Note: dataTable formula do not need to be used inside of dataTables. dataTable formula are for referencing the data within the dataTable. sum_examples <- data.frame( description = c(\"sum_sales_price\", \"sum_product_Price_Quantity\"), formula = c(\"\", \"\") ) wb$add_data(x = sum_examples) # add formulas wb$add_formula(x = \"sum(daily_sales[[#Data],[sales_price]])\", dims = \"B2\") wb$add_formula(x = \"sum(daily_sales[[#Data],[sales_price]] * daily_sales[[#Data],[sales_quantity]])\", dims = \"B3\", array = TRUE) #### dataTable referencing wb$add_worksheet(\"dt_references\") ### Adding the headers by themselves. wb$add_formula( x = \"daily_sales[[#Headers],[sales_price]:[total_gross_profit]]\", dims = \"A1:G1\", array = TRUE ) ### Adding the raw data by reference and selecting them directly. wb$add_formula( x = \"daily_sales[[#Data],[sales_price]:[total_gross_profit]]\", start_row = 2, dims = \"A2:G4\", array = TRUE ) # wb$open()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"importing-data","dir":"Articles","previous_headings":"","what":"Importing data","title":"openxlsx2 read to data frame manual","text":"Coming openxlsx might know read.xlsx() (two functions, one files one workbooks) readWorkbook(). Functions different things, mostly . openxlsx2 tried best reduce complexity hood user well. openxlsx2 replaced read_xlsx(), wb_read() share underlying function wb_to_df(). example use example data provided package. can locate “inst/extdata” folder. files included package source can open calculation software well.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"basic-import","dir":"Articles","previous_headings":"Importing data","what":"Basic import","title":"openxlsx2 read to data frame manual","text":"begin openxlsx2_example.xlsx file telling R find file system object contains path xlsx file pass file function read workbook R output created data frame contains data types date, logical, numeric character. function import file R, wb_to_df() provides similar options openxlsx functions read.xlsx() readWorkbook() new functions go options. might noticed, return column xlsx file row name data frame returned. Per default first sheet workbook imported. want switch , either provide sheet parameter correct index provide sheet name.","code":"file <- system.file(\"extdata\", \"openxlsx2_example.xlsx\", package = \"openxlsx2\") # import workbook library(openxlsx2) wb_to_df(file) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE NA NA #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 NA #NUM! 2 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 #> 9 NA NA NA #> 10 FALSE 2 NA 23 h 2023-12-24 25 #> 11 FALSE 3 NA 67.3 i 2023-12-25 3 #> 12 NA 1 NA 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"col_names---first-row-as-column-name","dir":"Articles","previous_headings":"Importing data","what":"col_names - first row as column name","title":"openxlsx2 read to data frame manual","text":"previous example first imported row used column name data frame. default behavior, always wanted expected. Therefore behavior can disabled user.","code":"# do not convert first row to column names wb_to_df(file, col_names = FALSE) #> B C D E F G H I J #> 2 Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE NA #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 NA #NUM! 2 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 #> 9 NA #> 10 FALSE 2 NA 23 h 2023-12-24 25 #> 11 FALSE 3 NA 67.3 i 2023-12-25 3 #> 12 1 NA 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"detect_dates---convert-cells-to-r-dates","dir":"Articles","previous_headings":"Importing data","what":"detect_dates - convert cells to R dates","title":"openxlsx2 read to data frame manual","text":"creators openxml standard well known mistakenly treating something date openxlsx2 built ways identify cell date try convert value , unfortunately always trivial task might fail. case provide option disable date conversion entirely. case underlying numerical value returned.","code":"# do not try to identify dates in the data wb_to_df(file, detect_dates = FALSE) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 45075 3209324 This #DIV/0! 0.06059028 #> 4 TRUE NA NA #NUM! b 45069 0 0.58538194 #> 5 TRUE 2 NA 1.34 c 44958 #VALUE! 0.95905093 #> 6 FALSE 2 NA #NUM! NA 2 0.72561343 #> 7 FALSE 3 NA 1.56 e NA NA #> 8 FALSE 1 NA 1.7 f 44987 2.7 0.36525463 #> 9 NA NA NA NA NA #> 10 FALSE 2 NA 23 h 45284 25 NA #> 11 FALSE 3 NA 67.3 i 45285 3 NA #> 12 NA 1 NA 123 45138 122 NA"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"show_formula---show-formulas-instead-of-results","dir":"Articles","previous_headings":"Importing data","what":"show_formula - show formulas instead of results","title":"openxlsx2 read to data frame manual","text":"Sometimes things might feel . can openxml files updating formula results sheets unless opened software provides functionality certain tabular calculation software. Therefore user might interested underlying functions see going sheet. Using show_formula possible","code":"# return the underlying Excel formula instead of their values wb_to_df(file, show_formula = TRUE) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 2023-05-29 3209324 This E3/0 01:27:15 #> 4 TRUE NA NA #NUM! b 2023-05-23 C4 14:02:57 #> 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 NA #NUM! C6+E6 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 C8+E8 08:45:58 #> 9 NA NA NA #> 10 FALSE 2 NA 23 h 2023-12-24 SUM(C10,E10) #> 11 FALSE 3 NA 67.3 i 2023-12-25 PRODUCT(C11,E3) #> 12 NA 1 NA 123 2023-07-31 E12-C12 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"dims---read-specific-dimension","dir":"Articles","previous_headings":"Importing data","what":"dims - read specific dimension","title":"openxlsx2 read to data frame manual","text":"Sometimes entire worksheet contains much data, case provide functions read selected dimension range. range consists either specific cell like “A1” cell range notion used openxml standard Alternatively, don’t know Excel sheet’s address, can use wb_dims() specify dimension. See ?wb_dims details.","code":"# read dimension without column names wb_to_df(file, dims = \"A2:C5\", col_names = FALSE) #> A B C #> 2 NA Var1 Var2 #> 3 NA TRUE 1 #> 4 NA TRUE #> 5 NA TRUE 2 # read dimension without column names with `wb_dims()` wb_to_df(file, dims = wb_dims(rows = 2:5, cols = 1:3), col_names = FALSE) #> A B C #> 2 NA Var1 Var2 #> 3 NA TRUE 1 #> 4 NA TRUE #> 5 NA TRUE 2"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"cols---read-selected-columns","dir":"Articles","previous_headings":"Importing data","what":"cols - read selected columns","title":"openxlsx2 read to data frame manual","text":"want read specific cell, cell range can use column attribute. attribute takes numeric vector argument","code":"# read selected cols wb_to_df(file, cols = c(\"A:B\", \"G\")) #> NA Var1 Var5 #> 3 NA TRUE 2023-05-29 #> 4 NA TRUE 2023-05-23 #> 5 NA TRUE 2023-02-01 #> 6 NA FALSE #> 7 NA FALSE #> 8 NA FALSE 2023-03-02 #> 9 NA NA #> 10 NA FALSE 2023-12-24 #> 11 NA FALSE 2023-12-25 #> 12 NA NA 2023-07-31"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"rows---read-selected-rows","dir":"Articles","previous_headings":"Importing data","what":"rows - read selected rows","title":"openxlsx2 read to data frame manual","text":"goes rows. can select using numeric vectors","code":"# read selected rows wb_to_df(file, rows = c(2, 4, 6)) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 4 TRUE NA NA #NUM! b 2023-05-23 NA 0 14:02:57 #> 6 FALSE 2 NA #NUM! NA 2 17:24:53"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"convert---convert-input-to-guessed-type","dir":"Articles","previous_headings":"Importing data","what":"convert - convert input to guessed type","title":"openxlsx2 read to data frame manual","text":"xml exists difference value types. values per default characters. provide numerics, logicals dates, openxlsx2 every software dealing xlsx files make assumptions cell type. especially tricky due notion worksheets. Unlike data frame, worksheet can wild mix types data. Even though conversion process character date numeric rather solid, sometimes user might want see data without conversion applied. might useful cases something unexpected happened import created warnings. case can look raw input data. want disable date detection well, please see entry .","code":"# convert characters to numerics and date (logical too?) wb_to_df(file, convert = FALSE) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 #NUM! 2 17:24:53 #> 7 FALSE 3 1.56 e #> 8 FALSE 1 1.7 f 2023-03-02 2.7 08:45:58 #> 9 #> 10 FALSE 2 23 h 2023-12-24 25 #> 11 FALSE 3 67.3 i 2023-12-25 3 #> 12 1 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"skip_empty_rows---remove-empty-rows","dir":"Articles","previous_headings":"Importing data","what":"skip_empty_rows - remove empty rows","title":"openxlsx2 read to data frame manual","text":"Even though openxlsx2 imports everything requested, sometimes might helpful remove empty lines data. might either left empty intentional empty formatted, cell value removed afterwards. added mostly backward comparability, default changed FALSE. behavior changed bit well. Previously empty cells removed prior conversion R data frames, now removed conversion removed completely empty","code":"# erase empty rows from dataset wb_to_df(file, sheet = 1, skip_empty_rows = TRUE) |> tail() #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 6 FALSE 2 NA #NUM! 2 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 #> 10 FALSE 2 NA 23 h 2023-12-24 25 #> 11 FALSE 3 NA 67.3 i 2023-12-25 3 #> 12 NA 1 NA 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"skip_empty_cols---remove-empty-columns","dir":"Articles","previous_headings":"Importing data","what":"skip_empty_cols - remove empty columns","title":"openxlsx2 read to data frame manual","text":"columns","code":"# erase empty cols from dataset wb_to_df(file, skip_empty_cols = TRUE) #> Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE NA #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 #NUM! 2 17:24:53 #> 7 FALSE 3 1.56 e #> 8 FALSE 1 1.7 f 2023-03-02 2.7 08:45:58 #> 9 NA NA #> 10 FALSE 2 23 h 2023-12-24 25 #> 11 FALSE 3 67.3 i 2023-12-25 3 #> 12 NA 1 123 2023-07-31 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"row_names---keep-rownames-from-input","dir":"Articles","previous_headings":"Importing data","what":"row_names - keep rownames from input","title":"openxlsx2 read to data frame manual","text":"Sometimes data source might provide rownames well. case can openxlsx2 treat first column rowname","code":"# convert first row to rownames wb_to_df(file, sheet = 2, dims = \"C6:G9\", row_names = TRUE) #> mpg cyl disp hp #> Mazda RX4 21.0 6 160 110 #> Mazda RX4 Wag 21.0 6 160 110 #> Datsun 710 22.8 4 108 93"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"types---convert-column-to-specific-type","dir":"Articles","previous_headings":"Importing data","what":"types - convert column to specific type","title":"openxlsx2 read to data frame manual","text":"user know better software type expect worksheet, can provided via types. parameter takes named numeric. 0 character, 1 numeric 2 date","code":"# define type of the data.frame wb_to_df(file, cols = c(2, 5), types = c(\"Var1\" = 0, \"Var3\" = 1)) #> Var1 Var3 #> 3 TRUE 1.00 #> 4 TRUE NaN #> 5 TRUE 1.34 #> 6 FALSE NA #> 7 FALSE 1.56 #> 8 FALSE 1.70 #> 9 NA #> 10 FALSE 23.00 #> 11 FALSE 67.30 #> 12 123.00"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"start_row---where-to-begin","dir":"Articles","previous_headings":"Importing data","what":"start_row - where to begin","title":"openxlsx2 read to data frame manual","text":"Often creator worksheet used lot creativity data begin first row, instead begins somewhere else. define row begin reading, define via start_row parameter","code":"# start in row 5 wb_to_df(file, start_row = 5, col_names = FALSE) #> B C D E F G H I J #> 5 TRUE 2 NA 1.34 c 2023-02-01 NA #VALUE! 23:01:02 #> 6 FALSE 2 NA NA #NUM! NA 2 17:24:53 #> 7 FALSE 3 NA 1.56 e NA #> 8 FALSE 1 NA 1.70 f 2023-03-02 NA 2.7 08:45:58 #> 9 NA NA NA NA NA #> 10 FALSE 2 NA 23.00 h 2023-12-24 NA 25 #> 11 FALSE 3 NA 67.30 i 2023-12-25 NA 3 #> 12 NA 1 NA 123.00 2023-07-31 NA 122 "},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_read_to_df.html","id":"na-strings---define-missing-values","dir":"Articles","previous_headings":"Importing data","what":"na.strings - define missing values","title":"openxlsx2 read to data frame manual","text":"“#N/” string, often user faced custom missing values values interested. strings can passed character vector via na.strings","code":"# na strings wb_to_df(file, na.strings = \"\") #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE NA NA #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 NA #NUM! 2 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 #> 9 NA NA NA #> 10 FALSE 2 NA 23 h 2023-12-24 25 #> 11 FALSE 3 NA 67.3 i 2023-12-25 3 #> 12 NA 1 NA 123 2023-07-31 122 "},{"path":[]},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"colors-text-rotation-and-number-formats","dir":"Articles","previous_headings":"Styling showcase","what":"Colors, text rotation and number formats","title":"Add styling to a workbook","text":"show two ways create styled tables openxlsx2 one using high level functions style worksheet areas one using bare metal approach creating identical table. show ways create styles openxlsx2 show build functions create functions. example , increased column width.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"the-quick-way-using-high-level-functions","dir":"Articles","previous_headings":"Styling showcase > Colors, text rotation and number formats","what":"the quick way: using high level functions","title":"Add styling to a workbook","text":"","code":"# add some dummy data set.seed(123) mat <- matrix(rnorm(28 * 28, mean = 44444, sd = 555), ncol = 28) colnames(mat) <- make.names(seq_len(ncol(mat))) border_col <- wb_color(theme = 1) border_sty <- \"thin\" # using ) to avoid counting manually which `dims` are spanned. dims_mat_header <- wb_dims(rows = 1, cols = seq_len(ncol(mat))) # returns \"A1:AB1\" # prepare workbook with data and formated first row wb <- wb_workbook() %>% wb_add_worksheet(\"test\") %>% wb_add_data(x = mat) %>% wb_add_border(dims = dims_mat_header, top_color = border_col, top_border = border_sty, bottom_color = border_col, bottom_border = border_sty, left_color = border_col, left_border = border_sty, right_color = border_col, right_border = border_sty, inner_hcolor = border_col, inner_hgrid = border_sty ) %>% wb_add_fill(dims = dims_mat_header, color = wb_color(hex = \"FF334E6F\")) %>% wb_add_font(dims = dims_mat_header, name = \"Arial\", bold = TRUE, color = wb_color(\"white\"), size = 20) %>% wb_add_cell_style(dims = dims_mat_header, horizontal = \"center\", textRotation = 45) # create various number formats x <- c( 0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 45, 46, 47, 48, 49 ) # apply the styles for (i in seq_along(x)) { cell <- sprintf(\"%s2:%s29\", int2col(i), int2col(i)) wb <- wb %>% wb_add_numfmt(dims = cell, numfmt = x[i]) } # wb$open()"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"the-long-way-using-bare-metal-functions","dir":"Articles","previous_headings":"Styling showcase > Colors, text rotation and number formats","what":"the long way: using bare metal functions","title":"Add styling to a workbook","text":"","code":"# create workbook wb <- wb_workbook() %>% wb_add_worksheet(\"test\") # add some dummy data to the worksheet set.seed(123) mat <- matrix(rnorm(28 * 28, mean = 44444, sd = 555), ncol = 28) colnames(mat) <- make.names(seq_len(ncol(mat))) wb$add_data(x = mat, col_names = TRUE) # create a border style and assign it to the workbook black <- wb_color(\"black\") # same as wb_color(\"black\") or wb_color(hex = \"FF000000\") or wb_color(\"FF000000\") new_border <- create_border( bottom = \"thin\", bottom_color = black, top = \"thin\", top_color = black, left = \"thin\", left_color = black, right = \"thin\", right_color = black ) wb$styles_mgr$add(new_border, \"new_border\") # create a fill style and assign it to the workbook new_fill <- create_fill(patternType = \"solid\", fgColor = wb_color(hex = \"FF334E6F\")) wb$styles_mgr$add(new_fill, \"new_fill\") # create a font style and assign it to the workbook new_font <- create_font(sz = 20, name = \"Arial\", b = TRUE, color = wb_color(\"white\")) wb$styles_mgr$add(new_font, \"new_font\") # create a new cell style, that uses the fill, the font and the border style new_cellxfs <- create_cell_style( num_fmt_id = 0, horizontal = \"center\", text_rotation = 45, fill_id = wb$styles_mgr$get_fill_id(\"new_fill\"), font_id = wb$styles_mgr$get_font_id(\"new_font\"), border_id = wb$styles_mgr$get_border_id(\"new_border\") ) # assign this style to the workbook wb$styles_mgr$add(new_cellxfs, \"new_styles\") # assign the new cell style to the header row of our data set # Achieve the same with `wb_dims()` and sprintf # cell <- sprintf(\"A1:%s1\", int2col(nrow(mat))) cell <- wb_dims(rows = 1, cols = seq_len(ncol(mat))) wb <- wb %>% wb_set_cell_style( dims = cell, style = wb$styles_mgr$get_xf_id(\"new_styles\") ) ## style the cells with some builtin format codes (no new numFmt entry is needed) # add builtin style ids x <- c( 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 45, 46, 47, 48, 49 ) # create styles new_cellxfs <- create_cell_style(num_fmt_id = x, horizontal = \"center\") # assign the styles to the workbook for (i in seq_along(x)) { wb$styles_mgr$add(new_cellxfs[i], paste0(\"new_style\", i)) } # new styles are 1:28 new_styles <- wb$styles_mgr$get_xf() for (i in as.integer(new_styles$id[new_styles$name %in% paste0(\"new_style\", seq_along(x))])) { cell <- wb_dims(rows = seq_len(nrow(mat)), cols = i, from_row = 2) wb <- wb %>% wb_set_cell_style(dims = cell, style = i) } # assign a custom tabColor wb$worksheets[[1]]$sheetPr <- xml_node_create( \"sheetPr\", xml_children = xml_node_create( \"tabColor\", xml_attributes = wb_color(hex = \"FF00FF00\") ) ) # # look at the beauty you've created # wb_open(wb)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"working-with-number-formats","dir":"Articles","previous_headings":"","what":"Working with number formats","title":"Add styling to a workbook","text":"default, openxlsx2 pick number formats selected R classes.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"numfmts","dir":"Articles","previous_headings":"Working with number formats","what":"numfmts","title":"Add styling to a workbook","text":"","code":"## Create Workbook object and add worksheets wb <- wb_workbook() wb$add_worksheet(\"S1\") wb$add_worksheet(\"S2\") df <- data.frame( \"Date\" = Sys.Date() - 0:19, \"T\" = TRUE, \"F\" = FALSE, \"Time\" = Sys.time() - 0:19 * 60 * 60, \"Cash\" = 1:20, \"Cash2\" = 31:50, \"hLink\" = \"https://CRAN.R-project.org/\", \"Percentage\" = seq(0, 1, length.out = 20), \"TinyNumbers\" = runif(20) / 1E9, stringsAsFactors = FALSE ) ## openxlsx will apply default Excel styling for these classes class(df$Cash) <- c(class(df$Cash), \"currency\") class(df$Cash2) <- c(class(df$Cash2), \"accounting\") class(df$hLink) <- \"hyperlink\" class(df$Percentage) <- c(class(df$Percentage), \"percentage\") class(df$TinyNumbers) <- c(class(df$TinyNumbers), \"scientific\") wb$add_data(\"S1\", x = df, start_row = 4, row_names = FALSE) wb$add_data_table(\"S2\", x = df, start_row = 4, row_names = FALSE)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"numfmts2","dir":"Articles","previous_headings":"Working with number formats","what":"numfmts2","title":"Add styling to a workbook","text":"addition, can set style picked using openxlsx2 options.","code":"wb <- wb_workbook() wb <- wb_add_worksheet(wb, \"test\") options(\"openxlsx2.dateFormat\" = \"yyyy\") options(\"openxlsx2.datetimeFormat\" = \"yyyy-mm-dd\") options(\"openxlsx2.numFmt\" = \"€ #.0\") df <- data.frame( \"Date\" = Sys.Date() - 0:19, \"T\" = TRUE, \"F\" = FALSE, \"Time\" = Sys.time() - 0:19 * 60 * 60, \"Cash\" = 1:20, \"Cash2\" = 31:50, \"hLink\" = \"https://CRAN.R-project.org/\", \"Percentage\" = seq(0, 1, length.out = 20), \"TinyNumbers\" = runif(20) / 1E9, stringsAsFactors = FALSE, \"numeric\" = 1 ) ## openxlsx will apply default Excel styling for these classes class(df$Cash) <- c(class(df$Cash), \"currency\") class(df$Cash2) <- c(class(df$Cash2), \"accounting\") class(df$hLink) <- \"hyperlink\" class(df$Percentage) <- c(class(df$Percentage), \"percentage\") class(df$TinyNumbers) <- c(class(df$TinyNumbers), \"scientific\") wb$add_data(\"test\", df)"},{"path":[]},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"wb_set_col_widths","dir":"Articles","previous_headings":"Modifying the column widths","what":"wb_set_col_widths","title":"Add styling to a workbook","text":"","code":"wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars, row_names = TRUE) cols <- 1:12 wb <- wb %>% wb_set_col_widths(cols = cols, widths = \"auto\")"},{"path":[]},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"add-borders","dir":"Articles","previous_headings":"Adding borders","what":"add borders","title":"Add styling to a workbook","text":"","code":"wb <- wb_workbook() # full inner grid wb$add_worksheet(sheet = \"S1\", grid_lines = FALSE)$ add_data(x = mtcars) wb$add_border( dims = \"A2:K33\", # can use wb_dims(rows = 2:33, cols = ncol(mtcars)) inner_hgrid = \"thin\", inner_hcolor = wb_color(hex = \"FF808080\"), inner_vgrid = \"thin\", inner_vcolor = wb_color(hex = \"FF808080\") ) # only horizontal grid wb$add_worksheet(\"S2\", grid_lines = FALSE)$ add_data(x = mtcars) wb$add_border( dims = \"A2:K33\", inner_hgrid = \"thin\", inner_hcolor = wb_color(hex = \"FF808080\") ) # only vertical grid wb$add_worksheet(\"S3\", grid_lines = FALSE)$ add_data(x = mtcars) wb$add_border( dims = \"A2:K33\", inner_vgrid = \"thin\", inner_vcolor = wb_color(hex = \"FF808080\") ) # no inner grid wb$add_worksheet(\"S4\", grid_lines = FALSE)$add_data(x = mtcars) wb$add_border(\"S4\", dims = \"A2:K33\") # wb_open(wb)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"styled-table","dir":"Articles","previous_headings":"Adding borders","what":"styled table","title":"Add styling to a workbook","text":"show two ways create styled tables openxlsx2 one using high level functions style worksheet areas one using bare metal approach creating identical table.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"the-quick-way-using-high-level-functions-1","dir":"Articles","previous_headings":"Adding borders > styled table","what":"the quick way: using high level functions","title":"Add styling to a workbook","text":"","code":"# add some dummy data to the worksheet mat <- matrix(1:4, ncol = 2, nrow = 2) colnames(mat) <- make.names(seq_len(ncol(mat))) wb <- wb_workbook() %>% wb_add_worksheet(\"test\") %>% wb_add_data(x = mat, dims = wb_dims(from_dims = \"B2\")) %>% # center first row wb_add_cell_style(dims = \"B2:C2\", horizontal = \"center\") %>% # add border for first row wb_add_border( dims = \"B2:C2\", bottom_color = wb_color(theme = 1), bottom_border = \"thin\", top_color = wb_color(theme = 1), top_border = \"double\", left_border = NULL, right_border = NULL ) %>% # add border for last row wb_add_border( dims = \"B4:C4\", bottom_color = wb_color(theme = 1), bottom_border = \"double\", top_border = NULL, left_border = NULL, right_border = NULL )"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"the-long-way-creating-everything-from-the-bone","dir":"Articles","previous_headings":"Adding borders > styled table","what":"the long way: creating everything from the bone","title":"Add styling to a workbook","text":"","code":"# add some dummy data to the worksheet mat <- matrix(1:4, ncol = 2, nrow = 2) colnames(mat) <- make.names(seq_len(ncol(mat))) wb <- wb_workbook() %>% wb_add_worksheet(\"test\") %>% wb_add_data( x = mat, dims = wb_dims(2, 2) ) # create a border style and assign it to the workbook black <- wb_color(\"black\") top_border <- create_border( top = \"double\", top_color = black, bottom = \"thin\", bottom_color = black ) bottom_border <- create_border(bottom = \"double\", bottom_color = black) wb$styles_mgr$add(top_border, \"top_border\") wb$styles_mgr$add(bottom_border, \"bottom_border\") # create a new cell style, that uses the fill, the font and the border style top_cellxfs <- create_cell_style( num_fmt_id = 0, horizontal = \"center\", border_id = wb$styles_mgr$get_border_id(\"top_border\") ) bottom_cellxfs <- create_cell_style( num_fmt_id = 0, border_id = wb$styles_mgr$get_border_id(\"bottom_border\") ) # assign this style to the workbook wb$styles_mgr$add(top_cellxfs, style_name = \"top_styles\") wb$styles_mgr$add(bottom_cellxfs, \"bottom_styles\") # assign the new cell style to the header row of our data set cell <- \"B2:C2\" wb <- wb %>% wb_set_cell_style(dims = cell, style = wb$styles_mgr$get_xf_id(\"top_styles\")) cell <- \"B4:C4\" wb <- wb %>% wb_set_cell_style(dims = cell, style = wb$styles_mgr$get_xf_id(\"bottom_styles\"))"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"use-workbook-colors-and-modify-them","dir":"Articles","previous_headings":"","what":"Use workbook colors and modify them","title":"Add styling to a workbook","text":"loop apply tint attribute fill color Tint variations theme colors.","code":"wb <- wb_workbook() %>% wb_add_worksheet(\"S1\") tints <- seq(-0.9, 0.9, by = 0.1) for (i in 0:9) { for (tnt in tints) { col <- paste0(int2col(i + 1), which(tints %in% tnt)) if (tnt == 0) { wb <- wb %>% wb_add_fill(dims = col, color = wb_color(theme = i)) } else { wb <- wb %>% wb_add_fill(dims = col, color = wb_color(theme = i, tint = tnt)) } } }"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"copy-cell-styles","dir":"Articles","previous_headings":"","what":"Copy cell styles","title":"Add styling to a workbook","text":"possible copy styles several cells . following example, styles cells formatted workbook applied previously empty cell range. careful though, wb_get_cell_style() returns styles, make sure copy-copy-dimensions match meaningful way.","code":"wb <- wb_load(system.file(\"extdata\", \"oxlsx2_sheet.xlsx\", package = \"openxlsx2\")) %>% wb_set_cell_style(1, \"A30:G35\", wb_get_cell_style(., 1, \"A10:G15\")) # wb_open(wb)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"style-strings","dir":"Articles","previous_headings":"","what":"Style strings","title":"Add styling to a workbook","text":"Using fmt_txt() possible style strings independently cell containing string. shown possible combine multiple styles together longer string. even possible use fmt_txt() na.strings:","code":"txt <- paste( fmt_txt(\"Embracing the full potential of \"), fmt_txt(\"openxlsx2\", bold = TRUE, size = 16), fmt_txt(\" with \"), fmt_txt(\"fmt_txt()\", font = \"Courier\"), fmt_txt(\" !\") ) wb <- wb_workbook()$add_worksheet()$add_data(x = txt) df <- mtcars df[df < 4] <- NA na_red <- fmt_txt(\"N/A\", color = wb_color(\"red\"), italic = TRUE, bold = TRUE) wb <- wb_workbook()$add_worksheet()$add_data(x = df, na.strings = na_red)"},{"path":"https://janmarvin.github.io/openxlsx2/dev/articles/openxlsx2_style_manual.html","id":"create-custom-table-styles","dir":"Articles","previous_headings":"","what":"Create custom table styles","title":"Add styling to a workbook","text":"create_tablestyle() possible create table styles. function uses create_dxfs_style() (just like spreadsheet software ). Therefore, quite user-friendly. following example shows function creates red table style. various dxfs styles must created assigned workbook (similar styles used conditional formatting). create_tablestyle() styles assigned table style elements. table style created, must also assigned workbook. can use workbook like table style.","code":"# a red table style dx0 <- create_dxfs_style( border = TRUE, left_color = wb_color(\"red\"), right_color = NULL, right_style = NULL, top_color = NULL, top_style = NULL, bottom_color = NULL, bottom_style = NULL ) dx1 <- create_dxfs_style( border = TRUE, left_color = wb_color(\"red\"), right_color = NULL, right_style = NULL, top_color = NULL, top_style = NULL, bottom_color = NULL, bottom_style = NULL ) dx2 <- create_dxfs_style( border = TRUE, top_color = wb_color(\"red\"), left_color = NULL, left_style = NULL, right_color = NULL, right_style = NULL, bottom_color = NULL, bottom_style = NULL ) dx3 <- create_dxfs_style( border = TRUE, top_color = wb_color(\"red\"), left_color = NULL, left_style = NULL, right_color = NULL, right_style = NULL, bottom_color = NULL, bottom_style = NULL ) dx4 <- create_dxfs_style( text_bold = TRUE ) dx5 <- create_dxfs_style( text_bold = TRUE ) dx6 <- create_dxfs_style( font_color = wb_color(\"red\"), text_bold = TRUE, border = TRUE, top_style = \"double\", left_color = NULL, left_style = NULL, right_color = NULL, right_style = NULL, bottom_color = NULL, bottom_style = NULL ) dx7 <- create_dxfs_style( font_color = wb_color(\"white\"), text_bold = TRUE, bgFill = wb_color(\"red\"), fgColor = wb_color(\"red\") ) dx8 <- create_dxfs_style( border = TRUE, left_color = wb_color(\"red\"), top_color = wb_color(\"red\"), right_color = wb_color(\"red\"), bottom_color = wb_color(\"red\") ) wb <- wb_workbook() %>% wb_add_worksheet(grid_lines = FALSE) wb$add_style(dx0) wb$add_style(dx1) wb$add_style(dx2) wb$add_style(dx3) wb$add_style(dx4) wb$add_style(dx5) wb$add_style(dx6) wb$add_style(dx7) wb$add_style(dx8) # finally create the table xml <- create_tablestyle( name = \"red_table\", whole_table = wb$styles_mgr$get_dxf_id(\"dx8\"), header_row = wb$styles_mgr$get_dxf_id(\"dx7\"), total_row = wb$styles_mgr$get_dxf_id(\"dx6\"), first_column = wb$styles_mgr$get_dxf_id(\"dx5\"), last_column = wb$styles_mgr$get_dxf_id(\"dx4\"), first_row_stripe = wb$styles_mgr$get_dxf_id(\"dx3\"), second_row_stripe = wb$styles_mgr$get_dxf_id(\"dx2\"), first_column_stripe = wb$styles_mgr$get_dxf_id(\"dx1\"), second_column_stripe = wb$styles_mgr$get_dxf_id(\"dx0\") ) wb$add_style(xml) # create a table and apply the custom style wb <- wb %>% wb_add_data_table(x = mtcars, table_style = \"red_table\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Jordan Mark Barbone. Author. Jan Marvin Garbuszus. Author, maintainer. Oliver Roy. Contributor. openxlsx authors. Copyright holder. openxlsx package Arseny Kapoulkine. Contributor, copyright holder. Author included pugixml code","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"(2024). openxlsx2: Read, Write Edit 'xlsx' Files. R package version 1.7.0.9000. URL https://github.com/JanMarvin/openxlsx2.","code":"@Manual{, title = {openxlsx2: Read, Write and Edit 'xlsx' Files}, year = {2024}, note = {R package version 1.7.0.9000}, url = {https://janmarvin.github.io/openxlsx2/}, }"},{"path":"https://janmarvin.github.io/openxlsx2/dev/index.html","id":"openxlsx2","dir":"","previous_headings":"","what":"Read, Write and Edit xlsx Files","title":"Read, Write and Edit xlsx Files","text":"R package modern reinterpretation widely used popular openxlsx package. Similar predecessor, simplifies creation xlsx files providing clean interface writing, designing editing worksheets. Based powerful XML library focusing modern programming flows pipes chains, openxlsx2 allows break many new ground.","code":""},{"path":"https://janmarvin.github.io/openxlsx2/dev/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Read, Write and Edit xlsx Files","text":"can install stable version openxlsx2 : can install development version openxlsx2 GitHub : r-universe :","code":"install.packages('openxlsx2') # install.packages(\"remotes\") remotes::install_github(\"JanMarvin/openxlsx2\") # Enable repository from janmarvin options(repos = c( janmarvin = 'https://janmarvin.r-universe.dev', CRAN = 'https://cloud.r-project.org')) # Download and install openxlsx2 in R install.packages('openxlsx2')"},{"path":"https://janmarvin.github.io/openxlsx2/dev/index.html","id":"introduction","dir":"","previous_headings":"","what":"Introduction","title":"Read, Write and Edit xlsx Files","text":"openxlsx2 aims swiss knife working openxml spreadsheet formats xlsx, xlsm (limited) xlsb (formats spreadsheet software supported). offer two different variants work openxlsx2. first one simply work R objects. possible read (read_xlsx()) write (write_xlsx()) data files. offer number options commands support various features openxml format, including reading writing named ranges tables. Furthermore, several ways read certain information openxml spreadsheet without opened spreadsheet software , e.g. get contained sheet names tables. second variant openxlsx2 offers work called wbWorkbook objects. openxml file read corresponding wbWorkbook object (wb_load()) new one created (wb_workbook()). Afterwards object can modified using various functions. example, worksheets can added removed, layout cells entire worksheets can changed, cells can modified (overwritten rewritten). Afterwards wbWorkbook objects can written openxml files processed suitable spreadsheet software. Many examples work openxlsx2 manual pages vignettes. can find :","code":"vignette(package = \"openxlsx2\")"},{"path":"https://janmarvin.github.io/openxlsx2/dev/index.html","id":"example","dir":"","previous_headings":"","what":"Example","title":"Read, Write and Edit xlsx Files","text":"basic example shows solve common problem:","code":"library(openxlsx2) # read xlsx or xlsm files path <- system.file(\"extdata/openxlsx2_example.xlsx\", package = \"openxlsx2\") read_xlsx(path) #> Var1 Var2 NA Var3 Var4 Var5 Var6 Var7 Var8 #> 3 TRUE 1 NA 1 a 2023-05-29 3209324 This #DIV/0! 01:27:15 #> 4 TRUE NA NA #NUM! b 2023-05-23 0 14:02:57 #> 5 TRUE 2 NA 1.34 c 2023-02-01 #VALUE! 23:01:02 #> 6 FALSE 2 NA #NUM! 2 17:24:53 #> 7 FALSE 3 NA 1.56 e #> 8 FALSE 1 NA 1.7 f 2023-03-02 2.7 08:45:58 #> 9 NA NA NA #> 10 FALSE 2 NA 23 h 2023-12-24 25 #> 11 FALSE 3 NA 67.3 i 2023-12-25 3 #> 12 NA 1 NA 123 2023-07-31 122