# Table of Contents - [Spreadsheet Munging Strategies](#spreadsheet-munging-strategies) - [1 Setup | Spreadsheet Munging Strategies](#1-setup-spreadsheet-munging-strategies) - [1.1 Packages | Spreadsheet Munging Strategies](#1-1-packages-spreadsheet-munging-strategies) - [2 Tidy-ish tables | Spreadsheet Munging Strategies](#2-tidy-ish-tables-spreadsheet-munging-strategies) - [1.2 Data | Spreadsheet Munging Strategies](#1-2-data-spreadsheet-munging-strategies) - [2.1 Clean & tidy tables | Spreadsheet Munging Strategies](#2-1-clean-tidy-tables-spreadsheet-munging-strategies) - [4 Small multiples | Spreadsheet Munging Strategies](#4-small-multiples-spreadsheet-munging-strategies) - [5 Formatting | Spreadsheet Munging Strategies](#5-formatting-spreadsheet-munging-strategies) - [8.1 Non-text headers e.g. dates | Spreadsheet Munging Strategies](#8-1-non-text-headers-e-g-dates-spreadsheet-munging-strategies) - [5.5 Superscript symbols | Spreadsheet Munging Strategies](#5-5-superscript-symbols-spreadsheet-munging-strategies) - [8 Other gotchas | Spreadsheet Munging Strategies](#8-other-gotchas-spreadsheet-munging-strategies) - [8.2 Data embedded in comments | Spreadsheet Munging Strategies](#8-2-data-embedded-in-comments-spreadsheet-munging-strategies) - [8.3 Named ranges | Spreadsheet Munging Strategies](#8-3-named-ranges-spreadsheet-munging-strategies) - [9.5 Ground water | Spreadsheet Munging Strategies](#9-5-ground-water-spreadsheet-munging-strategies) - [2.5 Layered meaningful formatting | Spreadsheet Munging Strategies](#2-5-layered-meaningful-formatting-spreadsheet-munging-strategies) - [2.2 Almost-tidy tables | Spreadsheet Munging Strategies](#2-2-almost-tidy-tables-spreadsheet-munging-strategies) - [4.4 Implied multiples | Spreadsheet Munging Strategies](#4-4-implied-multiples-spreadsheet-munging-strategies) - [5.2 Common formats | Spreadsheet Munging Strategies](#5-2-common-formats-spreadsheet-munging-strategies) - [2.7 Sentinel values in non-text columns | Spreadsheet Munging Strategies](#2-7-sentinel-values-in-non-text-columns-spreadsheet-munging-strategies) - [9 Case studies | Spreadsheet Munging Strategies](#9-case-studies-spreadsheet-munging-strategies) - [4.3 Same table in several worksheets/files but in different positions | Spreadsheet Munging Strategies](#4-3-same-table-in-several-worksheets-files-but-in-different-positions-spreadsheet-munging-strategies) - [4.2 Same table in several worksheets/files (using the sheet/file name) | Spreadsheet Munging Strategies](#4-2-same-table-in-several-worksheets-files-using-the-sheet-file-name-spreadsheet-munging-strategies) - [6 Data validation | Spreadsheet Munging Strategies](#6-data-validation-spreadsheet-munging-strategies) - [7 Formulas | Spreadsheet Munging Strategies](#7-formulas-spreadsheet-munging-strategies) - [2.3 Meaningfully formatted rows | Spreadsheet Munging Strategies](#2-3-meaningfully-formatted-rows-spreadsheet-munging-strategies) - [Unpivot Complex and Irregular Data Layouts • unpivotr](#unpivot-complex-and-irregular-data-layouts-unpivotr) - [9.2 Vaccinations | Spreadsheet Munging Strategies](#9-2-vaccinations-spreadsheet-munging-strategies) - [9.6 Cashflows | Spreadsheet Munging Strategies](#9-6-cashflows-spreadsheet-munging-strategies) - [9.1 Australian Marriage Survey | Spreadsheet Munging Strategies](#9-1-australian-marriage-survey-spreadsheet-munging-strategies) - [2.6 Hierarchies in formatting | Spreadsheet Munging Strategies](#2-6-hierarchies-in-formatting-spreadsheet-munging-strategies) - [5.3 In-cell formatting | Spreadsheet Munging Strategies](#5-3-in-cell-formatting-spreadsheet-munging-strategies) - [4.1 Small multiples with all headers present for each multiple | Spreadsheet Munging Strategies](#4-1-small-multiples-with-all-headers-present-for-each-multiple-spreadsheet-munging-strategies) - [5.1 An example formatting lookup | Spreadsheet Munging Strategies](#5-1-an-example-formatting-lookup-spreadsheet-munging-strategies) - [3 Pivot tables | Spreadsheet Munging Strategies](#3-pivot-tables-spreadsheet-munging-strategies) - [5.4 Multiple pieces of information in a single cell, with meaningful formatting | Spreadsheet Munging Strategies](#5-4-multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting-spreadsheet-munging-strategies) - [9.3 US Crime | Spreadsheet Munging Strategies](#9-3-us-crime-spreadsheet-munging-strategies) - [9.4 Toronto Transit Commission | Spreadsheet Munging Strategies](#9-4-toronto-transit-commission-spreadsheet-munging-strategies) - [9.7 School performance | Spreadsheet Munging Strategies](#9-7-school-performance-spreadsheet-munging-strategies) - [2.4 Meaningfully formatted cells | Spreadsheet Munging Strategies](#2-4-meaningfully-formatted-cells-spreadsheet-munging-strategies) - [3.1 Simple unpivoting | Spreadsheet Munging Strategies](#3-1-simple-unpivoting-spreadsheet-munging-strategies) - [3.2 Complex unpivoting | Spreadsheet Munging Strategies](#3-2-complex-unpivoting-spreadsheet-munging-strategies) - [Read Untidy Excel Files • tidyxl](#read-untidy-excel-files-tidyxl) - [2.4 Meaningfully formatted cells | Spreadsheet Munging Strategies](#2-4-meaningfully-formatted-cells-spreadsheet-munging-strategies) - [Detecting Spreadsheet Smells with xlex() • tidyxl](#detecting-spreadsheet-smells-with-xlex-tidyxl) - [Data Validation Rules • tidyxl](#data-validation-rules-tidyxl) - [2.3 Meaningfully formatted rows | Spreadsheet Munging Strategies](#2-3-meaningfully-formatted-rows-spreadsheet-munging-strategies) - [Tokenise 'Excel' Formulas • lexl](#tokenise-excel-formulas-lexl) - [Tidyxl • tidyxl](#tidyxl-tidyxl) - [Changelog • tidyxl](#changelog-tidyxl) - [Function reference • tidyxl](#function-reference-tidyxl) - [Parse xlsx (Excel) formulas into tokens — xlex • tidyxl](#parse-xlsx-excel-formulas-into-tokens-xlex-tidyxl) - [Import data validation rules of cells in xlsx (Excel) files — xlsx_validation • tidyxl](#import-data-validation-rules-of-cells-in-xlsx-excel-files-xlsx-validation-tidyxl) - [Import xlsx (Excel) cell contents into a tidy structure. — tidy_xlsx • tidyxl](#import-xlsx-excel-cell-contents-into-a-tidy-structure-tidy-xlsx-tidyxl) - [Import xlsx (Excel) cell contents into a tidy structure. — xlsx_cells • tidyxl](#import-xlsx-excel-cell-contents-into-a-tidy-structure-xlsx-cells-tidyxl) - [Function reference • lexl](#function-reference-lexl) - [Developer Notes • lexl](#developer-notes-lexl) - [All news • lexl](#all-news-lexl) - [License • lexl](#license-lexl) - [Run a shiny app to demonstrate lexl() — demo_lexl • lexl](#run-a-shiny-app-to-demonstrate-lexl-demo-lexl-lexl) - [Tokenise Excel Formulas • lexl](#tokenise-excel-formulas-lexl) - [Parse Excel formulas into tokens — lex_xl • lexl](#parse-excel-formulas-into-tokens-lex-xl-lexl) - [List sheets in an xlsx (Excel) file — xlsx_sheet_names • tidyxl](#list-sheets-in-an-xlsx-excel-file-xlsx-sheet-names-tidyxl) - [Determine file format — maybe_xlsx • tidyxl](#determine-file-format-maybe-xlsx-tidyxl) - [Names of all Excel functions — excel_functions • tidyxl](#names-of-all-excel-functions-excel-functions-tidyxl) - [Import xlsx (Excel) formatting definitions. — xlsx_formats • tidyxl](#import-xlsx-excel-formatting-definitions-xlsx-formats-tidyxl) - [Test that Excel number formats are date formats — is_date_format • tidyxl](#test-that-excel-number-formats-are-date-formats-is-date-format-tidyxl) - [Import theme color definitions from xlsx (Excel) files — xlsx_color_theme • tidyxl](#import-theme-color-definitions-from-xlsx-excel-files-xlsx-color-theme-tidyxl) - [Test that Excel formulas are ranges — is_range • tidyxl](#test-that-excel-formulas-are-ranges-is-range-tidyxl) - [Import named formulas from xlsx (Excel) files — xlsx_names • tidyxl](#import-named-formulas-from-xlsx-excel-files-xlsx-names-tidyxl) - [tidyxl: Import xlsx (Excel) spreadsheet data and formatting into tidy structures. — tidyxl-package • tidyxl](#tidyxl-import-xlsx-excel-spreadsheet-data-and-formatting-into-tidy-structures-tidyxl-package-tidyxl) - [Names and RGB values of Excel standard colours — xlsx_color_standard • tidyxl](#names-and-rgb-values-of-excel-standard-colours-xlsx-color-standard-tidyxl) - [Names of all Excel functions — excel_functions • lexl](#names-of-all-excel-functions-excel-functions-lexl) - [Test that Excel formulas are ranges — is_range • lexl](#test-that-excel-formulas-are-ranges-is-range-lexl) - [Plot the parse tree of a formula — lexl_igraph • lexl](#plot-the-parse-tree-of-a-formula-lexl-igraph-lexl) - [lexl: Parse Excel formulas into tokens. — lexl • lexl](#lexl-parse-excel-formulas-into-tokens-lexl-lexl) --- # Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/index.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= Welcome ======= This is a work-in-progress book about getting data out of spreadsheets, no matter how peculiar. The book is designed primarily for R users who have to extract data from spreadsheets and who are already familiar with the [tidyverse](https://www.tidyverse.org/) . It has a cookbook structure, and can be used as a reference, but readers who begin in the middle might have to work backwards from time to time. R packages that feature heavily are * [unpivotr](https://nacnudus.github.io/unpivotr) : deals with non-tabular data, especially from spreadsheets. * [tidyxl](https://nacnudus.github.io/tidyxl) : imports non-tabular data from Excel files Tidyxl and unpivotr are much more complicated than readxl, and that’s the point. Tidyxl and unpivotr give you more power and complexity when you need it. Please help me to improve this book by [opening a GitHub issue](https://github.com/nacnudus/spreadsheet-munging-strategies/issues) or [tweeting](https://twitter.com/nacnudus) . #### 0.0.0.1 Other ways to learn Some resources give directions in the old way, using compass directions. See `?unpivotr::direction` for their modern counterparts. * [YouTube videos](https://www.youtube.com/channel/UCrw0ScBCFSbk_lgkjyg4ucw) * [Worked example code](https://github.com/nacnudus/ukfarm) * [Blog post on `readr::melt_csv()`](https://nacnudus.github.io/duncangarmonsway/posts/2018-12-29-meltcsv/) * * * ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/by-nc-sa.png) Creative Commons License The online version of this book is licensed under the [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/) . --- # 1 Setup | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/setup.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 1 Setup ======= This section describes how the code in the book is set up. --- # 1.1 Packages | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/packages.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 1.1 Packages ------------ Here are the packages used by the code in this book. The last three are my own: [tidyxl](https://nacnudus.github.io/tidyxl) , [unpivotr](https://nacnudus.github.io/unpivotr) and [smungs](https://github.com/nacnudus/smungs) . You will need to install the latest versions from CRAN or GitHub. library(tidyverse) library(readxl) library(tidyxl) library(unpivotr) library(smungs) # GitHub only https://github.com/nacnudus/smungs --- # 2 Tidy-ish tables | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidyish.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2 Tidy-ish tables ================= This chapter is a gentle introduction, taking what you already know about importing tidy tabular data (with `read.csv()` or the [readr](https://readr.tidyverse.org/) package), and showing you how to how to do the same things with [tidyxl](https://nacnudus.github.io/tidyxl) and [unpivotr](https://nacnudus.github.io/unpivotr) . It works up to tables that are mostly tidy, but have subtle problems. --- # 1.2 Data | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/data.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 1.2 Data -------- The examples draw from a spreadsheet of toy data, included in the [unpivotr](https://nacnudus.github.io/unpivotr) package. It is recommended to [download](https://github.com/nacnudus/unpivotr/raw/master/inst/extdata/worked-examples.xlsx) the spreadsheet and have open it in a spreadsheet application while you read the book. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") --- # 2.1 Clean & tidy tables | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-clean.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.1 Clean & tidy tables ----------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/clean.png) If the tables in the spreadsheet are clean and tidy, then you should use a package like [readxl](https://github.com/tidyverse/readxl) . But it’s worth knowing how to emulate readxl with tidyxl and unpivotr, because some _almost_ clean tables can be handled using these techniques. Clean and tidy means * One table per sheet * A single row of column headers, or no headers * A single data type in each column * Only one kind of sentinel value (to be interpreted as `NA`) * No meaningful formatting * No data buried in formulas * No need to refer to named ranges Here’s the full process. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "clean") %>% behead("up", header) %>% select(row, data_type, header, character, numeric) %>% spatter(header) %>% select(-row) ## # A tibble: 3 x 2 ## Age Name ## ## 1 1 Matilda ## 2 3 Nicholas ## 3 5 Olivia `tidyxl::xlsx_cells()` imports the spreadsheet into a data frame, where each row of the data frame describes one cell of the spreadsheet. The columns `row` and `col` (and `address`) describe the position of the cell, and the value of the cell is in one of the columns `error`, `logical`, `numeric`, `date`, `character`, depending on the type of data in the cell. The column `data_type` says which column the value is in. Other columns describe formatting and formulas. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "clean") %>% select(row, col, data_type, character, numeric) ## # A tibble: 8 x 5 ## row col data_type character numeric ## ## 1 1 1 character Name NA ## 2 1 2 character Age NA ## 3 2 1 character Matilda NA ## 4 2 2 numeric 1 ## 5 3 1 character Nicholas NA ## 6 3 2 numeric 3 ## 7 4 1 character Olivia NA ## 8 4 2 numeric 5 `unpivotr::behead()` takes one level of headers from a pivot table and makes it part of the data. Think of it like `tidyr::gather()`, except that it works when there is more than one row of headers (or more than one column of row-headers), and it only works on tables that have first come through `unpivotr::as_cells()` or `tidyxl::xlsx_cells()`. xlsx_cells(path, sheet = "clean") %>% select(row, col, data_type, character, numeric) %>% behead("up", header) ## # A tibble: 6 x 6 ## row col data_type character numeric header ## ## 1 2 1 character Matilda NA Name ## 2 2 2 numeric 1 Age ## 3 3 1 character Nicholas NA Name ## 4 3 2 numeric 3 Age ## 5 4 1 character Olivia NA Name ## 6 4 2 numeric 5 Age `unpivotr::spatter()` spreads key-value pairs across multiple columns, like `tidyxl::spread()`, except that it handles mixed data types. It knows which column contains the cell value (i.e. the `character` column or the `numeric` column), by checking the `data_type` column. Just like `tidyr::spread()`, it can be confused by extraneous data, so it’s usually a good idea to drop the `col` column first, and to keep the `row` column. xlsx_cells(path, sheet = "clean") %>% select(row, col, data_type, character, numeric) %>% behead("up", header) %>% select(-col) %>% spatter(header) %>% select(-row) ## # A tibble: 3 x 2 ## Age Name ## ## 1 1 Matilda ## 2 3 Nicholas ## 3 5 Olivia In case the table has no column headers, you can spatter the `col` column instead of a nonexistent `header` column. xlsx_cells(path, sheet = "clean") %>% dplyr::filter(row >= 2) %>% select(row, col, data_type, character, numeric) %>% spatter(col) %>% select(-row) ## # A tibble: 3 x 2 ## `1` `2` ## ## 1 Matilda 1 ## 2 Nicholas 3 ## 3 Olivia 5 Tidyxl and unpivotr are much more complicated than readxl, and that’s the point: tidyxl and unpivotr give you more power and complexity when you need it. read_excel(path, sheet = "clean") ## # A tibble: 3 x 2 ## Name Age ## ## 1 Matilda 1 ## 2 Nicholas 3 ## 3 Olivia 5 read_excel(path, sheet = "clean", col_names = FALSE, skip = 1) ## New names: ## * `` -> ...1 ## * `` -> ...2 ## # A tibble: 3 x 2 ## ...1 ...2 ## ## 1 Matilda 1 ## 2 Nicholas 3 ## 3 Olivia 5 --- # 4 Small multiples | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 4 Small multiples ================= You might have heard the term ‘small multiples’ in the context of graphs, but it also occurs in spreadsheets, when an array of small tables could be combined into a single table. To import an array of small tables, start by writing the code to import one, and then apply that to each in turn. 1. Write the code to import one table. 2. Wrap that code in a function. 3. Partition the whole spreadsheet so that each table is in one partition. 4. Map the function over the partitions. --- # 5 Formatting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/formatting.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5 Formatting ============ This part explains in detail how to extract and interpret cell and in-cell formatting. Earlier sections have used formatting, but haven’t explained exactly how it works. The motivating example is a particularly pernicious gotcha: [superscript symbols](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html#superscript-symbols) . The formatting of a cell is available via a lookup table. Well, not a lookup table – a lookup list-of-lists-(of-lists-)of-vectors. It seems complicated, but in fact it is straightforward to find out a cell’s formatting. 1. Obtain the vector of formats that you need. 2. Look up the cell’s `style_format` or `local_format_id` in that vector. --- # 8.1 Non-text headers e.g. dates | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/non-text-headers-e-g-dates.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 8.1 Non-text headers e.g. dates ------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/non-text-headers.png) At the time of writing, readxl doesn’t convert Excel dates to R dates when they are in the header row. Using tidyxl and unpivotr, you can choose to make a cell of any data type into a tidy ‘header’, and you can reformat it as text before `spatter()` turns it into the header of a data frame. Another way to format headers as part of the `behead()` will be shown later. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "non-text headers") %>% behead("left", name) %>% behead("up", `academic-year`) %>% mutate(`academic-year` = strftime(`academic-year`, "%Y")) %>% select(row, data_type, `academic-year`, name, numeric) %>% spatter(`academic-year`) %>% select(-row) ## # A tibble: 2 x 3 ## name `2017` `2018` ## ## 1 Matilda 4 2 ## 2 Nicholas 3 1 When a single set of headers is of mixed data types, e.g. some character and some date, `behead()` chooses the correct ones using the `data_type` column, before converting them all to text via `format()`. xlsx_cells(path, sheet = "non-text headers") %>% select(row, col, data_type, character, numeric, date) %>% behead("up", header) ## # A tibble: 6 x 7 ## row col data_type character numeric date header ## ## 1 2 1 character Matilda NA NA Name ## 2 2 2 numeric 2 NA 2018-01-01 ## 3 2 3 numeric 4 NA 2017-01-01 ## 4 3 1 character Nicholas NA NA Name ## 5 3 2 numeric 1 NA 2018-01-01 ## 6 3 3 numeric 3 NA 2017-01-01 To format a header when a single set of headers are of mixed data types, you can specify a function for each data type in the call to `behead()`. xlsx_cells(path, sheet = "non-text headers") %>% select(row, col, data_type, character, numeric, date) %>% behead("up", header, formatters = list(date = ~ strftime(.x, "%Y"), character = toupper)) ## # A tibble: 6 x 7 ## row col data_type character numeric date header ## ## 1 2 1 character Matilda NA NA NAME ## 2 2 2 numeric 2 NA 2018 ## 3 2 3 numeric 4 NA 2017 ## 4 3 1 character Nicholas NA NA NAME ## 5 3 2 numeric 1 NA 2018 ## 6 3 3 numeric 3 NA 2017 --- # 5.5 Superscript symbols | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/superscript-symbols.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5.5 Superscript symbols ----------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/superscript-symbols.png) This is pernicious. What was Paula’s score, in the table below? path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") read_excel(path, sheet = "superscript symbols") ## # A tibble: 2 x 2 ## Name Score ## ## 1 Paula 91 ## 2 Matilda 10 The answer is, it’s not Paula, it’s Paul (superscript ‘a’), who scored 9 (superscript ‘1’). This sort of thing is difficult to spot. There’s a clue in the ‘Score’ column, which has been coerced to character so that the author could enter the superscript ‘1’ (Excel doesn’t allow superscripts in numeric cells), But it would be easy to interpret that as an accident of translation, and simply coerce back to numeric with `as.integer()`. With tidyxl, you can count the rows of each element of the `character_formatted` column to identify cells that have in-cell formatting. xlsx_cells(path, sheet = "superscript symbols") %>% dplyr::filter(data_type == "character") %>% dplyr::filter(map_int(character_formatted, nrow) != 1) %>% select(row, col, character) ## # A tibble: 2 x 3 ## row col character ## ## 1 2 1 Paula ## 2 2 2 91 The values and symbols can then be separated by assuming the value is the first string, and the symbol is the second. xlsx_cells(path, sheet = "superscript symbols") %>% mutate(character = map_chr(character_formatted, ~ ifelse(is.null(.x), character, .x$character[1])), symbol = map_chr(character_formatted, ~ ifelse(is.null(.x), NA, .x$character[2])), numeric = if_else(row > 1 & col == 2 & data_type == "character", as.numeric(character), numeric), character = if_else(is.na(numeric), character, NA_character_)) %>% select(row, col, numeric, character, symbol) ## Warning in if_else(row > 1 & col == 2 & data_type == "character", as.numeric(character), : NAs ## introduced by coercion ## # A tibble: 6 x 5 ## row col numeric character symbol ## ## 1 1 1 NA Name ## 2 1 2 NA Score ## 3 2 1 NA Paul a ## 4 2 2 9 1 ## 5 3 1 NA Matilda ## 6 3 2 10 --- # 8 Other gotchas | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/other-gotchas.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 8 Other gotchas =============== This part is a collection of gotchas that don’t fit anywhere else. --- # 8.2 Data embedded in comments | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-embedded-in-comments.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 8.2 Data embedded in comments ----------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/comments.png) Comment strings are availabe in the `comment` column, just like `character`. Comments can have formatting, but tidyxl doesn’t yet import the formatting. If you need this, please open an [issue](https://github.com/nacnudus/tidyxl/issues) . It would probably be imported into a `comment_formatted` column, similarly to `character_formatted`. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "comments") %>% select(row, col, data_type, character, numeric, comment) %>% behead("up", "header") ## # A tibble: 4 x 7 ## row col data_type character numeric comment header ## ## 1 2 1 character Paul NA Absent Term 1 Name ## 2 2 2 numeric 9 Predicted Score ## 3 3 1 character Matilda NA Name ## 4 3 2 numeric 10 Score Comments apply to single cells, so follow the same procedure as “Already a tidy table but with meaningful formatting of single cells”. cells <- xlsx_cells(path, sheet = "comments") %>% select(row, col, data_type, character, numeric, comment) cells ## # A tibble: 6 x 6 ## row col data_type character numeric comment ## ## 1 1 1 character Name NA ## 2 1 2 character Score NA ## 3 2 1 character Paul NA Absent Term 1 ## 4 2 2 numeric 9 Predicted ## 5 3 1 character Matilda NA ## 6 3 2 numeric 10 values <- cells %>% select(-comment) %>% behead("up", header) %>% select(-col) %>% spatter(header) values ## # A tibble: 2 x 3 ## row Name Score ## ## 1 2 Paul 9 ## 2 3 Matilda 10 comments <- cells %>% behead("up", header) %>% mutate(header = paste0(header, "_comment")) %>% select(row, header, comment) %>% spread(header, comment) comments ## # A tibble: 2 x 3 ## row Name_comment Score_comment ## ## 1 2 Absent Term 1 Predicted ## 2 3 left_join(values, comments, by = "row") %>% select(-row) ## # A tibble: 2 x 4 ## Name Score Name_comment Score_comment ## ## 1 Paul 9 Absent Term 1 Predicted ## 2 Matilda 10 --- # 8.3 Named ranges | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/named-ranges.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 8.3 Named ranges ---------------- TODO --- # 9.5 Ground water | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/ground-water.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.5 Ground water ---------------- [![Tweet by Beck Frydenborg about tidying data with R](https://nacnudus.github.io/spreadsheet-munging-strategies/images/groundwater-tweet.png)](https://twitter.com/beckfrydenborg/status/974787652573646849) If the cells containing `U` didn’t exist, then this spreadsheet would be a textbook example of unpivoting a pivot table. There are two rows of column headers, as well as two columns of row headers, so you would use `behead()` for each header. [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/groundwater.xlsx?raw=true) . Synthesised from the [original tweet](https://twitter.com/beckfrydenborg/status/974787652573646849) . x <- xlsx_cells(smungs::groundwater) %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% behead("up-left", "sample-type") %>% behead("up-left", "site") %>% behead("left", "parameter") %>% behead("left", "unit") x ## # A tibble: 17 x 9 ## row col data_type character numeric `sample-type` site parameter unit ## ## 1 4 3 numeric 3.2 ground water A Nitrogen, Kjeldahl mn/L ## 2 6 3 numeric 0.025 ground water A Nitrate Nitrite as N mg/L ## 3 6 4 character U NA ground water A Nitrate Nitrite as N mg/L ## 4 8 3 numeric 0.04 ground water A Phosphorus as P mg/L ## 5 4 5 numeric 1.2 ground water B Nitrogen, Kjeldahl mn/L ## 6 6 5 numeric 0.025 ground water B Nitrate Nitrite as N mg/L ## 7 6 6 character U NA ground water B Nitrate Nitrite as N mg/L ## 8 8 5 numeric 0.17 ground water B Phosphorus as P mg/L ## 9 4 7 numeric 0.5 ground water C Nitrogen, Kjeldahl mn/L ## 10 6 7 numeric 0.025 ground water C Nitrate Nitrite as N mg/L ## 11 6 8 character U NA ground water C Nitrate Nitrite as N mg/L ## 12 8 7 numeric 0.062 ground water C Phosphorus as P mg/L ## 13 4 9 numeric 0.4 ground water D Nitrogen, Kjeldahl mn/L ## 14 6 9 numeric 0.025 ground water D Nitrate Nitrite as N mg/L ## 15 6 10 character U NA ground water D Nitrate Nitrite as N mg/L ## 16 8 9 numeric 0.04 ground water D Phosphorus as P mg/L ## 17 8 10 character J3 NA ground water D Phosphorus as P mg/L So what to do about the `U` cells? We don’t know what they mean, but perhaps they are some kind of flag, to inform the interpretation of the numbers. If that’s the case, then they should appear in the same row of the final data frame as the numbers. Something like `tidyr::spread()` would work, except that instead of spreading the values in just one column, we need to spread the values in both the `character` and `numeric` columns, depending on the value in the `data_type` column. This is what `spatter()` is for. x %>% select(-col) %>% spatter(data_type) %>% select(-row) ## # A tibble: 12 x 6 ## `sample-type` site parameter unit character numeric ## ## 1 ground water A Nitrogen, Kjeldahl mn/L 3.2 ## 2 ground water B Nitrogen, Kjeldahl mn/L 1.2 ## 3 ground water C Nitrogen, Kjeldahl mn/L 0.5 ## 4 ground water D Nitrogen, Kjeldahl mn/L 0.4 ## 5 ground water A Nitrate Nitrite as N mg/L U 0.025 ## 6 ground water B Nitrate Nitrite as N mg/L U 0.025 ## 7 ground water C Nitrate Nitrite as N mg/L U 0.025 ## 8 ground water D Nitrate Nitrite as N mg/L U 0.025 ## 9 ground water A Phosphorus as P mg/L 0.04 ## 10 ground water B Phosphorus as P mg/L 0.17 ## 11 ground water C Phosphorus as P mg/L 0.062 ## 12 ground water D Phosphorus as P mg/L J3 0.04 Compare that with the results of `spread()`, which can only spread one value column at a time. x %>% select(-col) %>% spread(data_type, character) ## # A tibble: 17 x 7 ## row `sample-type` site parameter unit character numeric ## ## 1 4 ground water D Nitrogen, Kjeldahl mn/L ## 2 4 ground water C Nitrogen, Kjeldahl mn/L ## 3 4 ground water B Nitrogen, Kjeldahl mn/L ## 4 4 ground water A Nitrogen, Kjeldahl mn/L ## 5 6 ground water A Nitrate Nitrite as N mg/L ## 6 6 ground water B Nitrate Nitrite as N mg/L ## 7 6 ground water C Nitrate Nitrite as N mg/L ## 8 6 ground water D Nitrate Nitrite as N mg/L ## 9 6 ground water A Nitrate Nitrite as N mg/L U ## 10 6 ground water B Nitrate Nitrite as N mg/L U ## 11 6 ground water C Nitrate Nitrite as N mg/L U ## 12 6 ground water D Nitrate Nitrite as N mg/L U ## 13 8 ground water A Phosphorus as P mg/L ## 14 8 ground water D Phosphorus as P mg/L ## 15 8 ground water C Phosphorus as P mg/L ## 16 8 ground water B Phosphorus as P mg/L ## 17 8 ground water D Phosphorus as P mg/L J3 x %>% select(-col) %>% spread(data_type, numeric) ## # A tibble: 17 x 7 ## row `sample-type` site parameter unit character numeric ## ## 1 4 ground water A Nitrogen, Kjeldahl mn/L NA 3.2 ## 2 4 ground water B Nitrogen, Kjeldahl mn/L NA 1.2 ## 3 4 ground water C Nitrogen, Kjeldahl mn/L NA 0.5 ## 4 4 ground water D Nitrogen, Kjeldahl mn/L NA 0.4 ## 5 6 ground water A Nitrate Nitrite as N mg/L NA NA ## 6 6 ground water B Nitrate Nitrite as N mg/L NA NA ## 7 6 ground water C Nitrate Nitrite as N mg/L NA NA ## 8 6 ground water D Nitrate Nitrite as N mg/L NA NA ## 9 6 ground water A Nitrate Nitrite as N mg/L NA 0.025 ## 10 6 ground water B Nitrate Nitrite as N mg/L NA 0.025 ## 11 6 ground water C Nitrate Nitrite as N mg/L NA 0.025 ## 12 6 ground water D Nitrate Nitrite as N mg/L NA 0.025 ## 13 8 ground water D Phosphorus as P mg/L NA NA ## 14 8 ground water A Phosphorus as P mg/L NA 0.04 ## 15 8 ground water B Phosphorus as P mg/L NA 0.17 ## 16 8 ground water C Phosphorus as P mg/L NA 0.062 ## 17 8 ground water D Phosphorus as P mg/L NA 0.04 --- # 2.5 Layered meaningful formatting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/layered-formatting.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.5 Layered meaningful formatting --------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/combined-highlights.png) Sometimes different kinds of formatting relate to clearly different aspects of an observation, e.g. yellow highlight for “uncertain data” and red text for “product no longer available”. Both yellow highlighting and red text in the same row would indicate uncertain data and unavailability of the product at the same time. Deal with it by reading each kind of formatting into a separate column, e.g. fill colour into one column, font colour into another, bold/not-bold into a another, etc. # Step 1: import the table taking only cell values and ignoring the formatting path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "combined-highlights") # Step 2: import one kind of formatting of one column of the table # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb font_colours <- xlsx_formats(path)$local$font$color$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `fill` of the fill colours, by looking up the # local_format_id of each cell in the `formats` pallette. formats <- xlsx_cells(path, sheet = "combined-highlights") %>% dplyr::filter(row >= 2, col == 1) %>% # Omit the header row mutate(fill_colour = fill_colours[local_format_id], font_colour = font_colours[local_format_id]) %>% select(fill_colour, font_colour) # Step 3: append the `fill` column to the rest of the data bind_cols(x, formats) ## # A tibble: 4 x 5 ## Name Weight Price fill_colour font_colour ## ## 1 Knife 7 8 FF000000 ## 2 Fork 5 6 FFFFFF00 FF000000 ## 3 Spoon 3 4 FFFF0000 ## 4 Teaspoon 1 2 FFFFFF00 FFFF0000 Here’s the same thing, but using only tidyxl and unpivotr. fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb font_colours <- xlsx_formats(path)$local$font$color$rgb cells <- xlsx_cells(path, sheet = "combined-highlights") %>% mutate(fill_colour = fill_colours[local_format_id], font_colour = font_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour, font_colour) %>% behead("up", header) %>% behead("left", Name) %>% select(-col, -character) values <- cells %>% select(-fill_colour, -font_colour) %>% spread(header, numeric) formats <- distinct(cells, row, fill_colour, font_colour) left_join(values, formats, by = "row") %>% select(-row) ## # A tibble: 4 x 6 ## data_type Name Price Weight fill_colour font_colour ## ## 1 numeric Knife 8 7 FF000000 ## 2 numeric Fork 6 5 FFFFFF00 ## 3 numeric Spoon 4 3 FFFF0000 ## 4 numeric Teaspoon 2 1 FFFFFF00 FFFF0000 --- # 2.2 Almost-tidy tables | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/almost-tidy-tables.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.2 Almost-tidy tables ---------------------- For tables that are already ‘tidy’ (a single row of column headers), use packages like [readxl](http://readxl.tidyverse.org/) that specialise in importing tidy data. For everything else, read on. ### 2.2.1 Transposed (headers in the first row, data extends to the right) ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/transposed.png) Most packages for importing data assume that the headers are in the first row, and each row of data is an observation. They usually don’t support the alternative: headers in the first column, and each column of data is an observation. You can hack a way around this by importing without recognising any headers, transposing with `t()` (which outputs a matrix), placing the headers as names, and converting back to a data frame, but this almost always results in all the data types being converted. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") read_excel(path, sheet = "transposed", col_names = FALSE) %>% t() %>% `colnames<-`(.[1, ]) %>% .[-1, ] %>% as_tibble() ## New names: ## * `` -> ...1 ## * `` -> ...2 ## * `` -> ...3 ## * `` -> ...4 ## # A tibble: 3 x 2 ## Name Age ## ## 1 Matilda 1 ## 2 Nicholas 3 ## 3 Olivia 5 Tidyxl and unpivotr are agnostic to the layout of tables. Importing the transpose is the same is importing the usual layout, merely using the `"left"` direction instead of `"up"` when beheading the headers. xlsx_cells(path, sheet = "transposed") %>% behead("left", header) %>% select(col, data_type, header, character, numeric) %>% spatter(header) %>% select(Name, Age) ## # A tibble: 3 x 2 ## Name Age ## ## 1 Matilda 1 ## 2 Nicholas 3 ## 3 Olivia 5 ### 2.2.2 Other stuff on the same sheet ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/notes.png) It will be more complicated when the table doesn’t begin in cell A1, or if there are non-blank cells above, below or either side of the table. If you know at coding time which rows and columns the table occupies, then you can do the following. * Blank or non-blank cells above the table: use the `skip` argument of `readxl::read_excel()`. * Blank or non-blank cells either side of the table: use the `col_types` argument of `readxl::read_excel()` to ignore those columns. * Blank or non-blank cells below the table: use `n_max` argument of `readxl::read_excel()` to ignore those rows. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") readxl::read_excel(path, sheet = "notes", skip = 2, n_max = 33, col_types = c("guess", "guess", "skip")) %>% drop_na() ## # A tibble: 2 x 2 ## Name Age ## ## 1 Matilda 1 ## 2 Nicholas 3 If you don’t know at coding time which rows and columns the table occupies (e.g. when the latest version of the spreadsheet is published and the table has moved), then one strategy is to read the spreadsheet with `tidyxl::xlsx_cells()` first, and inspect the results to determine the boundaries of the table. Then use those boundaries as the `skip`, `n_max` and `col_types` arguments to `readxl::read_excel()` 1. Read the spreadsheet with `tidyxl::xlsx_cells()`. Filter the result for sentinel values, e.g. the cells containing the first and final column headers, and a cell in the final row of data. 2. Construct the arguments `skip`, `n_max` and `col_types` so that `readxl::read_excel()` gets the exact dimensions of the table. # Step 1: read the spreadsheet and filter for sentinel values to detect the # top-left and bottom-right cells cells <- xlsx_cells(path, sheet = "notes") rectify(cells) ## # A tibble: 7 x 5 ## `row/col` `1(A)` `2(B)` `3(C)` `4(D)` ## ## 1 1 Title text ## 2 2 ## 3 3 Name Age ## 4 4 Matilda 1 ## 5 5 Nicholas 3 ## 6 6 ## 7 7 Footnote top_left <- dplyr::filter(cells, character == "Name") %>% select(row, col) top_left ## # A tibble: 1 x 2 ## row col ## ## 1 3 2 # It can be tricky to find the bottom-right cell because you have to make some # assumptions. Here we assume that only cells within the table are numeric. bottom_right <- dplyr::filter(cells, data_type == "numeric") %>% summarise(row = max(row), col = max(col)) bottom_right ## # A tibble: 1 x 2 ## row col ## ## 1 5 3 # Step 2: construct the arguments `skip` and `n_max` for read_excel() skip <- top_left$row - 1L n_rows <- bottom_right$row - skip read_excel(path, sheet = "notes", skip = skip, n_max = n_rows) ## # A tibble: 2 x 2 ## Name Age ## ## 1 Matilda 1 ## 2 Nicholas 3 Here’s another way using only tidyxl and unpivotr. # Step 2: filter for cells between the top-left and bottom-right, and spatter # into a table cells %>% dplyr::filter(between(row, top_left$row, bottom_right$row), between(col, top_left$col, bottom_right$col)) %>% select(row, col, data_type, character, numeric) %>% behead("up", header) %>% select(-col) %>% spatter(header) %>% select(-row) ## # A tibble: 2 x 2 ## Age Name ## ## 1 1 Matilda ## 2 3 Nicholas --- # 4.4 Implied multiples | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 4.4 Implied multiples --------------------- Implied multiples look like a single table, but many of the headers appear more than once. There is a dominant set of headers that are on the same ‘level’ (e.g. in the same row) as the other headers. See a real-life [case study](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html#vaccinations) ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/implied-multiples.png) In the example, the header “Grade” is repeated, but it really belongs in each case to the header “Classics”, “History”, “Music” or “Drama”. Those subject headers serve two purposes: as title of each small multiple, and as the unstated “Score” header of their columns. The difficulty is in associating a grade with its corresponding score. 1. Filter for the “Classics”, “History”, “Music” and “Drama” headers, and assign them to a variable to be `enhead()`ed later. You could think of this as faking a set of headers that doesn’t exist, but is implied. 2. Meanwhile, `behead()` the original “Classics”, “History” (etc.) cells and then overwrite them with “Score”. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "implied-multiples") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) Filter for the “Classics”, “History”, “Music” and “Drama” headers, and assign them to a variable to be `enhead()`ed later. subjects <- all_cells %>% dplyr::filter(col >= 2, row == 2, character != "Grade") %>% select(row, col, subject = character) subjects ## # A tibble: 4 x 3 ## row col subject ## ## 1 2 2 Classics ## 2 2 4 History ## 3 2 6 Music ## 4 2 8 Drama Meanwhile, `behead()` the original “Classics”, “History” (etc.) cells and then overwrite them with “Score”. all_cells %>% behead("up-left", "field") %>% behead("up", "header") %>% behead("left", "name") %>% enhead(subjects, "up-left") %>% # Reattach the filtered subject headers mutate(header = if_else(header == "Grade", header, "Score")) %>% select(-col) %>% spatter(header) %>% select(-row) ## # A tibble: 8 x 5 ## field name subject Grade Score ## ## 1 Humanities Matilda Classics F 1 ## 2 Humanities Matilda History D 3 ## 3 Performance Matilda Drama A 7 ## 4 Performance Matilda Music B 5 ## 5 Humanities Olivia Classics D 2 ## 6 Humanities Olivia History C 4 ## 7 Performance Olivia Drama A 8 ## 8 Performance Olivia Music B 6 --- # 5.2 Common formats | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/common-formats.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5.2 Common formats ------------------ ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/formatting.png) This example shows how to look up the most common formats. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") cells <- xlsx_cells(path, sheet = "formatting") %>% select(row, col, character, style_format, local_format_id, height, width) formats <- xlsx_formats(path) bold <- formats$local$font$bold italic <- formats$local$font$italic underline <- formats$local$font$underline strikethrough <- formats$local$font$strike font_colour <- formats$local$font$color$rgb fill_colour <- formats$local$fill$patternFill$fgColor$rgb font_size <- formats$local$font$size font_name <- formats$local$font$name border_colour <- formats$local$border$right$color$rgb border_linetype <- formats$local$border$right$style mutate(cells, bold = bold[local_format_id], italic = italic[local_format_id], underline = underline[local_format_id], strikethrough = strikethrough[local_format_id], font_colour = font_colour[local_format_id], font_size = font_size[local_format_id], font_name = font_name[local_format_id], fill_colour = fill_colour[local_format_id], border_colour = border_colour[local_format_id], border_linetype = border_linetype[local_format_id]) ## # A tibble: 14 x 17 ## row col character style_format local_format_id height width bold italic underline ## ## 1 1 1 bold Normal 6 15 8.71 TRUE FALSE ## 2 2 1 italic Normal 8 15 8.71 FALSE TRUE ## 3 3 1 underline Normal 51 15 8.71 FALSE FALSE single ## 4 4 1 striketh… Normal 52 15 8.71 FALSE FALSE ## 5 5 1 red text Normal 12 15 8.71 FALSE FALSE ## 6 6 1 font siz… Normal 53 18.8 8.71 FALSE FALSE ## 7 7 1 font ari… Normal 54 15 8.71 FALSE FALSE ## 8 8 1 yellow f… Normal 11 15 8.71 FALSE FALSE ## 9 9 1 black bo… Normal 43 15 8.71 FALSE FALSE ## 10 10 1 thick bo… Normal 55 15 8.71 FALSE FALSE ## 11 11 1 dashed b… Normal 56 15 8.71 FALSE FALSE ## 12 12 1 row heig… Normal 1 30 8.71 FALSE FALSE ## 13 13 2 column w… Normal 1 15 17.4 FALSE FALSE ## 14 14 1 Bad' sty… Explanatory… 57 15 8.71 FALSE FALSE ## # … with 7 more variables: strikethrough , font_colour , font_size , ## # font_name , fill_colour , border_colour , border_linetype --- # 2.7 Sentinel values in non-text columns | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-sentinel.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.7 Sentinel values in non-text columns --------------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/sentinels.png) R packages like [readr](http://readr.tidyverse.org/) recognise `NA` as a sentinel value that means “Not Applicable”, or “Not Available”, or anything you want. It doesn’t affect the data type of a column when `NA` is one of the values. Some datasets use other symbols as a sentinel value, e.g. `N/A` or `.`, or a combination, in which case you can instruct `readr` to interpret those values as sentinels, and it will import them all as `NA`. But what if the data uses more than one _kind_ of sentinel value. For example, Statistics New Zealand uses `…` to mean “Not applicable”, and `..C` to mean “Confidentialised”. Most tools will either regard both values as `NA`, or coerce the whole column to characters. read_csv("a, b, c 1, 2, 3 4, …, ..C", na = c("…", "..C")) # Regard both values as NA ## # A tibble: 2 x 3 ## a b c ## ## 1 1 2 3 ## 2 4 NA NA read_csv("a, b, c 1, 2, 3 4, …, ..C", na = "") # Coerce the whole column to characters ## # A tibble: 2 x 3 ## a b c ## ## 1 1 2 3 ## 2 4 … ..C A better procedure is to import the sentinel values into their own column, or even into separate `TRUE`/`FALSE` columns for each kind of sentinel. Note that sentinel values relate the the value in the cell, rather than to the whole row, so the first step is to make the dataset _extra-tidy_ as in the section “Already a tidy table but with meaningful formatting of single cells”. # Tidy path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "sentinels") x ## # A tibble: 4 x 3 ## Name Subject Score ## ## 1 Matilda Music 7 ## 2 Nicholas Classics NA ## 3 Olivia … 3 ## 4 Paul NA ..C # Extra-tidy extra_tidy <- gather(x, variable, value, -Name) %>% arrange(Name, variable) extra_tidy ## # A tibble: 8 x 3 ## Name variable value ## ## 1 Matilda Score 7 ## 2 Matilda Subject Music ## 3 Nicholas Score NA ## 4 Nicholas Subject Classics ## 5 Olivia Score 3 ## 6 Olivia Subject … ## 7 Paul Score ..C ## 8 Paul Subject NA With an extra-tidy dataset, the sentinels can now be appended to the values of individual variables, rather than to whole observations. # Extra-tidy, with row and column numbers of the original variables, and the # sentinels omitted extra_tidy <- read_excel(path, sheet = "sentinels", na = c("NA", "…", "..C")) %>% mutate(row = row_number() + 1L) %>% gather(variable, value, -row, -Name) %>% group_by(row) %>% mutate(col = row_number() + 1L) %>% ungroup() %>% select(row, col, Name, variable, value) %>% arrange(row, col) extra_tidy ## # A tibble: 8 x 5 ## row col Name variable value ## ## 1 2 2 Matilda Subject Music ## 2 2 3 Matilda Score 7 ## 3 3 2 Nicholas Subject Classics ## 4 3 3 Nicholas Score ## 5 4 2 Olivia Subject ## 6 4 3 Olivia Score 3 ## 7 5 2 Paul Subject ## 8 5 3 Paul Score # Import all the cells, and filter for sentinel values sentinels <- xlsx_cells(path, sheet = "sentinels") %>% dplyr::filter(character %in% c("NA", "…", "..C")) %>% mutate(sentinel = character) %>% select(row, col, sentinel) sentinels ## # A tibble: 4 x 3 ## row col sentinel ## ## 1 3 3 NA ## 2 4 2 … ## 3 5 2 NA ## 4 5 3 ..C # Join the `sentinel` column to the rest of the data left_join(extra_tidy, sentinels, by = c("row", "col")) ## # A tibble: 8 x 6 ## row col Name variable value sentinel ## ## 1 2 2 Matilda Subject Music ## 2 2 3 Matilda Score 7 ## 3 3 2 Nicholas Subject Classics ## 4 3 3 Nicholas Score NA ## 5 4 2 Olivia Subject … ## 6 4 3 Olivia Score 3 ## 7 5 2 Paul Subject NA ## 8 5 3 Paul Score ..C Here’s another version using only tidyxl and unpivotr, which provides `isolate_sentinels()` to make this much more straightforward. xlsx_cells(path, sheet = "sentinels") %>% select(row, col, data_type, character, numeric) %>% isolate_sentinels(character, c("NA", "…", "..C")) %>% behead("left", Name) %>% behead("up", variable) %>% select(Name, variable, character, numeric, sentinel) ## # A tibble: 8 x 5 ## Name variable character numeric sentinel ## ## 1 Matilda Subject Music NA ## 2 Matilda Score 7 ## 3 Nicholas Subject Classics NA ## 4 Nicholas Score NA NA ## 5 Olivia Subject NA … ## 6 Olivia Score 3 ## 7 Paul Subject NA NA ## 8 Paul Score NA ..C --- # 9 Case studies | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/case-studies.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9 Case studies ============== This is a collection of spreadsheets found in the wild. Some are as easy to mung as the examples; others are harder because their structure is less consistent. Seeing and reading the code will help you guage how much work is still involved in munging a spreadsheet. Attempting them for yourself and checking the model answer will help you to hone your instincts. The spreadsheet files are provided in the `smungs` package on GitHub. Install as follows. # install.packages("devtools") # If you don't already have it devtools::install_github("nacnudus/smungs") #### 9.0.0.1 Other case studies elsewhere * [YouTube videos](https://www.youtube.com/channel/UCrw0ScBCFSbk_lgkjyg4ucw) * [Worked example code](https://github.com/nacnudus/ukfarm) * [Blog post on `readr::melt_csv()`](https://nacnudus.github.io/duncangarmonsway/posts/2018-12-29-meltcsv/) --- # 4.3 Same table in several worksheets/files but in different positions | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-but-in-different-positions.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 4.3 Same table in several worksheets/files but in different positions --------------------------------------------------------------------- ![sheet: female](https://nacnudus.github.io/spreadsheet-munging-strategies/images/female.png) ![sheet: male](https://nacnudus.github.io/spreadsheet-munging-strategies/images/male.png) This is almost the same as the section “Same table in several worksheets/files (using the sheet/file name)”. The only difference is that the function you write to unpivot the table must also _find_ the table in the first place, and be robust to differences in the placement and context of the table on each sheet. In this example, both tables begin in the same column, but there is an extra row of notes above one of the tables. There are a few ways to tackle this problem. Here, we filter for the `Subject` cell, which is either `A3` or `A4`, and then extend the selection to include the whole table. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = c("female", "male")) %>% dplyr::filter(!is_blank) %>% select(sheet, row, col, data_type, character, numeric) all_cells ## # A tibble: 21 x 6 ## sheet row col data_type character numeric ## ## 1 female 1 1 character Table of scores NA ## 2 female 3 1 character Subject NA ## 3 female 3 2 character Matilda NA ## 4 female 3 3 character Olivia NA ## 5 female 4 1 character Classics NA ## 6 female 4 2 numeric 1 ## 7 female 4 3 numeric 2 ## 8 female 5 1 character History NA ## 9 female 5 2 numeric 3 ## 10 female 5 3 numeric 4 ## # … with 11 more rows unpivot <- function(cells) { cells %>% dplyr::filter(character == "Subject") %>% pull(row) %>% {dplyr::filter(cells, row >= .)} %>% behead("up", name) %>% behead("left", subject) } all_cells %>% nest(-sheet) %>% mutate(data = map(data, unpivot)) %>% unnest() %>% select(sex = sheet, name, subject, score = numeric) ## Warning: All elements of `...` must be named. ## Did you want `data = c(row, col, data_type, character, numeric)`? ## Warning: `cols` is now required. ## Please use `cols = c(data)` ## # A tibble: 8 x 4 ## sex name subject score ## ## 1 female Matilda Classics 1 ## 2 female Olivia Classics 2 ## 3 female Matilda History 3 ## 4 female Olivia History 4 ## 5 male Nicholas Classics 3 ## 6 male Paul Classics 0 ## 7 male Nicholas History 5 ## 8 male Paul History 1 --- # 4.2 Same table in several worksheets/files (using the sheet/file name) | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/same-table-in-several-worksheetsfiles-using-the-sheetfile-name.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 4.2 Same table in several worksheets/files (using the sheet/file name) ---------------------------------------------------------------------- ![sheet: humanities](https://nacnudus.github.io/spreadsheet-munging-strategies/images/humanities.png) ![sheet: performance](https://nacnudus.github.io/spreadsheet-munging-strategies/images/performance.png) Because `tidyxl()` imports cells from multiple sheets into the same data frame, tables on separate sheets can be imported by mapping over the different sheets. Just name each sheet in the `xlsx_cell()` call, or don’t name any to import them all. As far as `tidyxl()` is concerned, the particular sheet (aka ‘tab’) that a cell is on is another coordinate like `row` and `col`, so the full location of a cell is its `row`, its `col`, and its `sheet`. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = c("humanities", "performance")) %>% dplyr::filter(!is_blank) %>% select(sheet, row, col, data_type, character, numeric) all_cells ## # A tibble: 16 x 6 ## sheet row col data_type character numeric ## ## 1 humanities 1 2 character Matilda NA ## 2 humanities 1 3 character Nicholas NA ## 3 humanities 2 1 character Classics NA ## 4 humanities 2 2 numeric 1 ## 5 humanities 2 3 numeric 3 ## 6 humanities 3 1 character History NA ## 7 humanities 3 2 numeric 3 ## 8 humanities 3 3 numeric 5 ## 9 performance 1 2 character Matilda NA ## 10 performance 1 3 character Nicholas NA ## 11 performance 2 1 character Music NA ## 12 performance 2 2 numeric 5 ## 13 performance 2 3 numeric 9 ## 14 performance 3 1 character Drama NA ## 15 performance 3 2 numeric 7 ## 16 performance 3 3 numeric 12 To prepare the sheets to be mapped over, use `tidyr::nest()`. The `data` column contains the cells of each sheet. all_cells %>% nest(-sheet) ## Warning: All elements of `...` must be named. ## Did you want `data = c(row, col, data_type, character, numeric)`? ## # A tibble: 2 x 2 ## sheet data ## ## 1 humanities ## 2 performance The function to unpivot each table in this case will be a couple of `behead()` statements. Further clean-up can be saved until the end. unpivot <- function(cells) { cells %>% behead("up", name) %>% behead("left", subject) } After mapping the unpivot function over each sheet of cells, use `tidyr::unnest()` to show every row of data again. all_cells %>% nest(-sheet) %>% mutate(data = map(data, unpivot)) %>% unnest() ## Warning: All elements of `...` must be named. ## Did you want `data = c(row, col, data_type, character, numeric)`? ## Warning: `cols` is now required. ## Please use `cols = c(data)` ## # A tibble: 8 x 8 ## sheet row col data_type character numeric name subject ## ## 1 humanities 2 2 numeric 1 Matilda Classics ## 2 humanities 2 3 numeric 3 Nicholas Classics ## 3 humanities 3 2 numeric 3 Matilda History ## 4 humanities 3 3 numeric 5 Nicholas History ## 5 performance 2 2 numeric 5 Matilda Music ## 6 performance 2 3 numeric 9 Nicholas Music ## 7 performance 3 2 numeric 7 Matilda Drama ## 8 performance 3 3 numeric 12 Nicholas Drama Finally, do the clean-up operations that were saved until now. all_cells %>% nest(-sheet) %>% mutate(data = map(data, unpivot)) %>% unnest() %>% transmute(field = sheet, name, subject, score = numeric) ## Warning: All elements of `...` must be named. ## Did you want `data = c(row, col, data_type, character, numeric)`? ## Warning: `cols` is now required. ## Please use `cols = c(data)` ## # A tibble: 8 x 4 ## field name subject score ## ## 1 humanities Matilda Classics 1 ## 2 humanities Nicholas Classics 3 ## 3 humanities Matilda History 3 ## 4 humanities Nicholas History 5 ## 5 performance Matilda Music 5 ## 6 performance Nicholas Music 9 ## 7 performance Matilda Drama 7 ## 8 performance Nicholas Drama 12 --- # 6 Data validation | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/data-validation.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 6 Data validation ================= TODO: rework the [vignette](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html) ? --- # 7 Formulas | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/formulas.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 7 Formulas ========== TODO: rework the [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html) ? --- # 2.3 Meaningfully formatted rows | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.3 Meaningfully formatted rows ------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/highlights.png) As with [clean, tidy tables](https://nacnudus.github.io/spreadsheet-munging-strategies/clean) , but with a second step to interpret the formatting. Sometimes whole rows in a table are highlighted by formatting them with, say, a bright yellow fill. The highlighting could mean “this observation should be ignored”, or “this product is no longer available”. Different colours could mean different levels of a hierarchy, e.g. green for “pass” and red for “fail”. There are three steps to interpreting this. 1. Import the table, taking only the cell values and ignoring the formatting. 2. Import one column of the table, taking only the formatting and not the cell values. 3. Use `dplyr::bind_cols()` to append the column of formatting to the table of cell values. You can then interpret the formatting however you like. Step 1 is the same as [clean, tidy tables](https://nacnudus.github.io/spreadsheet-munging-strategies/clean) . Step 2 uses `tidyxl::xlsx_cells()` to load the data, `tidyxl::xlsx_formats()`, and several tidyverse functions to link the two and filter for only one column. Why only one column? Because if a whole row is highlighted, then you only need to know the highlighting of one column to know the highlighting of all the others. This is a special case of the following section, [meaningfully formatted cells](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells) . Here `dplyr::bind_cols()` can be used as a shortcut, because we are joining exactly `n` rows of formatting to `n` rows of data. The following sections is a more general case that can be used instead of this procedure. # Step 1: import the table taking only cell values and ignoring the formatting path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "highlights") # Step 2: import one column of the table, taking only the formatting and not the # cell values # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `fill_colour` of the fill colours, by looking up the # local_format_id of each cell in the `fill_colours` pallette. fills <- xlsx_cells(path, sheet = "highlights") %>% dplyr::filter(row >= 2, col == 1) %>% # Omit the header row mutate(fill_colour = fill_colours[local_format_id]) %>% select(fill_colour) # Step 3: append the `fill` column to the rest of the data bind_cols(x, fills) %>% select(Age, Height, fill_colour) ## # A tibble: 3 x 3 ## Age Height fill_colour ## ## 1 1 2 ## 2 3 4 FFFFFF00 ## 3 5 6 Note that the fill colour is expressed as an RGB value with transparency in the first two letters, e.g. `FFFFFF00` is `FF` (opaque), with `FFFF00` (yellow). Here’s another way using only tidyxl and unpivotr. fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb xlsx_cells(path, sheet = "highlights") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) %>% behead("up", header) %>% select(-col, -character) %>% spatter(header) %>% select(-row) ## # A tibble: 3 x 3 ## fill_colour Age Height ## ## 1 1 2 ## 2 FFFFFF00 3 4 ## 3 5 6 --- # Unpivot Complex and Irregular Data Layouts • unpivotr [Skip to contents](https://nacnudus.github.io/unpivotr#main) unpivotr[](https://nacnudus.github.io/unpivotr#unpivotr) ========================================================= [unpivotr](https://github.com/nacnudus/unpivotr) deals with non-tabular data, especially from spreadsheets. Use unpivotr when your source data has any of these ‘features’: * Multi-headered hydra * Meaningful formatting * Headers anywhere but at the top of each column * Non-text headers e.g. dates * Other stuff around the table * Several similar tables in one sheet * Sentinel values * Superscript symbols * Meaningful comments * Nested HTML tables If that list makes your blood boil, you’ll enjoy the function names. * `[behead()](https://nacnudus.github.io/reference/behead.html) ` deals with multi-headered hydra tables one layer of headers at a time, working from the edge of the table inwards. It’s a bit like using `header = TRUE` in `[read.csv()](https://rdrr.io/r/utils/read.table.html) `, but because it’s a function, you can apply it to as many layers of headers as you need. You end up with all the headers in columns. * `[spatter()](https://nacnudus.github.io/reference/spatter.html) ` is like `[tidyr::spread()](https://tidyr.tidyverse.org/reference/spread.html) ` but preserves mixed data types. You get into a mixed-data-type situation by delaying type coercion until _after_ the table is tidy (rather than before, like `[read.csv()](https://rdrr.io/r/utils/read.table.html) ` et al). And yes, it usually follows `[behead()](https://nacnudus.github.io/reference/behead.html) `. More positive, corrective functions: * `[justify()](https://nacnudus.github.io/reference/justify.html) ` aligns column headers before `[behead()](https://nacnudus.github.io/reference/behead.html) `ing, and has deliberate moral overtones. * `[enhead()](https://nacnudus.github.io/reference/enhead.html) ` attaches a header to the body of the data, _a la_ Frankenstein. The effect is the same as `[behead()](https://nacnudus.github.io/reference/behead.html) `, but is more powerful because you can choose exactly which header cells you want, paying attention to formatting (which `[behead()](https://nacnudus.github.io/reference/behead.html) ` doesn’t understand). * `[isolate_sentinels()](https://nacnudus.github.io/reference/isolate_sentinels.html) ` separates meaningful symbols like `"N/A"` or `"confidential"` from the rest of the data, giving them some time alone think about what they’ve done. * `[partition()](https://nacnudus.github.io/reference/partition.html) ` takes a sheet with several tables on it, and slashes into pieces that each contain one table. You can then unpivot each table in turn with `[purrr::map()](https://purrr.tidyverse.org/reference/map.html) ` or similar. Make cells tidy[](https://nacnudus.github.io/unpivotr#make-cells-tidy) ----------------------------------------------------------------------- Unpivotr uses data where each cells is represented by one row in a dataframe. Like this. ![Gif of tidyxl converting cells into a tidy representation of one row per cell](https://nacnudus.github.io/articles/tidy_xlsx.gif) Gif of tidyxl converting cells into a tidy representation of one row per cell What can you do with tidy cells? The best places to start are: * [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) , a free, online cookbook using [tidyxl](https://github.com/nacnudus/tidyxl/) and [unpivotr](https://github.com/nacnudus/unpivotr) * [Screencasts](https://www.youtube.com/watch?v=1sinC7wsS5U) on YouTube. * [Worked examples](https://github.com/nacnudus/ukfarm) on GitHub. Otherwise the basic idea is: 1. Read the data with a specialist tool. * For spreadsheets, use [tidyxl](https://nacnudus.github.io/tidyxl/) . * For plain text files, you might soon be able to use [readr](https://readr.tidyverse.org/) , but for now you’ll have to install a pull-request on that package with `devtools::install_github("tidyverse/readr#760")`. * For tables in html pages, use `unpivotr::tidy_html()` * For data frames, use `[unpivotr::as_cells()](https://nacnudus.github.io/reference/as_cells.html) ` – this should be a last resort, because by the time the data is in a conventional data frame, it is often too late – formatting has been lost, and most data types have been coerced to strings. 2. Either `[behead()](https://nacnudus.github.io/reference/behead.html) ` straight away, else `[dplyr::filter()](https://dplyr.tidyverse.org/reference/filter.html) ` separately for the header cells and the data cells, and then recombine with `[enhead()](https://nacnudus.github.io/reference/enhead.html) `. 3. `[spatter()](https://nacnudus.github.io/reference/spatter.html) ` so that each column has one data type. library(unpivotr) library(tidyverse) #> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ── #> ✔ dplyr 1.1.4 ✔ readr 2.1.5 #> ✔ forcats 1.0.0 ✔ stringr 1.5.1 #> ✔ ggplot2 3.5.1 ✔ tibble 3.2.1 #> ✔ lubridate 1.9.3 ✔ tidyr 1.3.1 #> ✔ purrr 1.0.2 #> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── #> ✖ dplyr::filter() masks stats::filter() #> ✖ dplyr::lag() masks stats::lag() #> ✖ tidyr::pack() masks unpivotr::pack() #> ✖ tidyr::unpack() masks unpivotr::unpack() #> ℹ Use the conflicted package () to force all conflicts to become errors x <- purpose$`up-left left-up` x # A pivot table in a conventional data frame. Four levels of headers, in two #> X2 X3 X4 X5 X6 X7 #> 1 Female Male #> 2 0 - 6 7 - 10 0 - 6 7 - 10 #> 3 Bachelor's degree 15 - 24 7000 27000 13000 #> 4 25 - 44 12000 137000 9000 81000 #> 5 45 - 64 10000 64000 7000 66000 #> 6 65+ 18000 7000 17000 #> 7 Certificate 15 - 24 29000 161000 30000 190000 #> 8 25 - 44 34000 179000 31000 219000 #> 9 45 - 64 30000 210000 23000 199000 #> 10 65+ 12000 77000 8000 107000 #> 11 Diploma 15 - 24 14000 9000 11000 #> 12 25 - 44 10000 66000 8000 47000 #> 13 45 - 64 6000 68000 5000 58000 #> 14 65+ 5000 41000 1000 34000 #> 15 No Qualification 15 - 24 10000 43000 12000 37000 #> 16 25 - 44 11000 36000 21000 50000 #> 17 45 - 64 19000 91000 17000 75000 #> 18 65+ 16000 118000 9000 66000 #> 19 Postgraduate qualification 15 - 24 6000 #> 20 25 - 44 5000 86000 7000 60000 #> 21 45 - 64 6000 55000 6000 68000 #> 22 65+ 13000 18000 # rows and two columns. y <- as_cells(x) # 'Tokenize' or 'melt' the data frame into one row per cell y #> # A tibble: 132 × 4 #> row col data_type chr #> #> 1 1 1 chr #> 2 2 1 chr #> 3 3 1 chr Bachelor's degree #> 4 4 1 chr #> 5 5 1 chr #> 6 6 1 chr #> 7 7 1 chr Certificate #> 8 8 1 chr #> 9 9 1 chr #> 10 10 1 chr #> # ℹ 122 more rows rectify(y) # useful for reviewing the melted form as though in a spreadsheet #> # A tibble: 22 × 7 #> `row/col` `1(A)` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` #> #> 1 1 Female Male #> 2 2 0 - 6 7 - 10 0 - 6 7 - 10 #> 3 3 Bachelor's degree 15 - 24 7000 27000 13000 #> 4 4 25 - 44 12000 137000 9000 81000 #> 5 5 45 - 64 10000 64000 7000 66000 #> 6 6 65+ 18000 7000 17000 #> 7 7 Certificate 15 - 24 29000 161000 30000 190000 #> 8 8 25 - 44 34000 179000 31000 219000 #> 9 9 45 - 64 30000 210000 23000 199000 #> 10 10 65+ 12000 77000 8000 107000 #> # ℹ 12 more rows y %>% behead("up-left", "sex") %>% # Strip headers behead("up", "life-satisfication") %>% # one behead("left-up", "qualification") %>% # by behead("left", "age-band") %>% # one. select(-row, -col, -data_type, count = chr) %>% # cleanup mutate(count = as.integer(count)) #> # A tibble: 80 × 5 #> count sex `life-satisfication` qualification `age-band` #> #> 1 7000 Female 0 - 6 Bachelor's degree 15 - 24 #> 2 12000 Female 0 - 6 Bachelor's degree 25 - 44 #> 3 10000 Female 0 - 6 Bachelor's degree 45 - 64 #> 4 NA Female 0 - 6 Bachelor's degree 65+ #> 5 27000 Female 7 - 10 Bachelor's degree 15 - 24 #> 6 137000 Female 7 - 10 Bachelor's degree 25 - 44 #> 7 64000 Female 7 - 10 Bachelor's degree 45 - 64 #> 8 18000 Female 7 - 10 Bachelor's degree 65+ #> 9 NA Male 0 - 6 Bachelor's degree 15 - 24 #> 10 9000 Male 0 - 6 Bachelor's degree 25 - 44 #> # ℹ 70 more rows Note the compass directions in the code above, which hint to `[behead()](https://nacnudus.github.io/reference/behead.html) ` where to find the header cell for each data cell. * `"up-left"` means the header (`Female`, `Male`) is positioned up and to the left of the columns of data cells it describes. * `"up"` means the header (`0 - 6`, `7 - 10`) is positioned directly above the columns of data cells it describes. * `"left-up"` means the header (`Bachelor's degree`, `Certificate`, etc.) is positioned to the left and upwards of the rows of data cells it describes. * `"left"` means the header (`15 - 24`, `25 - 44`, etc.) is positioned directly to the left of the rows of data cells it describes. Installation[](https://nacnudus.github.io/unpivotr#installation) ----------------------------------------------------------------- # install.packages("devtools") # If you don't already have devtools devtools::install_github("nacnudus/unpivotr", build_vignettes = TRUE) The version 0.4.0 release had somee breaking changes. See `NEWS.md` for details. The previous version can be installed as follow: devtools::install_version("unpivotr", version = "0.3.1", repos = "http://cran.us.r-project.org") Similar projects[](https://nacnudus.github.io/unpivotr#similar-projects) ------------------------------------------------------------------------- [unpivotr](https://github.com/nacnudus/unpivotr) is inspired by [Databaker](https://github.com/sensiblecodeio/databaker) , a collaboration between the [United Kingdom Office of National Statistics](https://www.ons.gov.uk/) and [The Sensible Code Company](https://sensiblecode.io/) . [unpivotr](https://github.com/nacnudus/unpivotr) . [jailbreaker](https://github.com/rsheets/jailbreakr) attempts to extract non-tabular data from spreadsheets into tabular structures automatically via some clever algorithms. [unpivotr](https://github.com/nacnudus/unpivotr) differs by being less magic, and equipping you to express what you want to do. --- # 9.2 Vaccinations | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/vaccinations.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.2 Vaccinations ---------------- [![Tweet by Bob Rudis challenging R users to tidy a particular spreadsheet](https://nacnudus.github.io/spreadsheet-munging-strategies/images/vaccinations_tweet.png)](https://twitter.com/hrbrmstr/status/890200287356620806) This is a real-life example of [implied multiples](https://nacnudus.github.io/spreadsheet-munging-strategies/implied-multiples.html#implied-multiples) . Implied multiples look like a single table, but many of the headers appear more than once. There is a dominant set of headers that are on the same ‘level’ (e.g. in the same row) as the other headers. In this case, there is a small multiple for each year of data. The year headers are highlighted in yellow in the screenshot. ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/vaccinations.png) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/vaccinations.xlsx?raw=true) . [Original source](https://www.cdc.gov/vaccines/imz-managers/coverage/schoolvaxview/data-reports/vacc-coverage.html) . The way to unpivot this is to realise that the year cells represent two different things: the year (obviously) and a statistic (percentage vaccinated). It would have been easier to unpivot if the years had been put into a separate row of headers, so we will pretend that that was in fact the case. 1. Filter for the year cells and store in a variable to `enhead()` later. 2. `behead()` everything else as usual, and then overwite the year headers with `percentage_vaccinated`. 3. `enhead()` the year cells. cells <- xlsx_cells(smungs::vaccinations, "SVV Coverage Trend Data") years <- cells %>% dplyr::filter(row == 3, col >= 1, str_detect(character, "20[0-9]{2}-[0-9]{2}")) %>% select(row, col, year = character) years ## # A tibble: 42 x 3 ## row col year ## ## 1 3 2 2009-10 ## 2 3 8 2010-11 ## 3 3 14 2011-12 ## 4 3 20 2012-13 ## 5 3 26 2013-14 ## 6 3 32 2014-15 ## 7 3 38 2015-16 ## 8 3 44 2009-10 ## 9 3 50 2010-11 ## 10 3 56 2011-12 ## # … with 32 more rows cells %>% select(row, col, data_type, character) %>% behead("up-left", "series") %>% behead("up-left", "population") %>% behead("left", "state") %>% behead("up", "header") %>% mutate(header = if_else(str_detect(header, "20[0-9]{2}-[0-9]{2}"), "percent_vaccinated", header), header = str_replace_all(str_to_lower(header), " ", "_")) %>% enhead(years, "up-left") %>% select(row, series, population, state, year, header, character) %>% spatter(header, character) %>% select(series, population, state, year, percent_vaccinated, percent_surveyed, everything()) ## # A tibble: 2,226 x 11 ## series population state year percent_vaccina… percent_surveyed row footnotes survey_type ## ## 1 Schoo… All kinde… Alab… 2009… 94.0 100.0 4 ≥. *. ** Census ## 2 Schoo… All kinde… Alab… 2010… NA NA 4 NA NA ## 3 Schoo… All kinde… Alab… 2011… 93.6 100.0 4 ≥. * .** Census ## 4 Schoo… All kinde… Alab… 2012… 92.8 100.0 4 ≥. * . ‡… Census ## 5 Schoo… All kinde… Alab… 2013… 92.0 100.0 4 ≥. *. ** Census ## 6 Schoo… All kinde… Alab… 2014… 93.5 100.0 4 ≥. * Census ## 7 Schoo… All kinde… Alab… 2015… 93.1 100.0 4 ≥.* Census ## 8 Schoo… All kinde… Alab… 2009… NReq 100.0 4 *. ** Census ## 9 Schoo… All kinde… Alab… 2010… NA NA 4 NA NA ## 10 Schoo… All kinde… Alab… 2011… NReq 100.0 4 * .** Census ## # … with 2,216 more rows, and 2 more variables: target , ## # total_kindergarten_population --- # 9.6 Cashflows | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/cashflows.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.6 Cashflows ------------- [![Blog post by Davis Vaughan about tidying cashflow data with unpivotr](https://nacnudus.github.io/spreadsheet-munging-strategies/images/cashflows-blog.png)](https://blog.davisvaughan.com/post/tidying-excel-cash-flow-spreadsheets-in-r/) Davis Vaughan kindly [blogged](https://blog.davisvaughan.com/post/tidying-excel-cash-flow-spreadsheets-in-r/) about using unpivotr to tidy spreadsheets of cashflows. Here is an example using unpivotr’s new, more powerful syntax. ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/cashflows.png) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/cashflows.xlsx?raw=true) . [Original source](https://github.com/DavisVaughan/tidying-excel-cashflows-blog-companion) . The techniques are 1. Filter out `TOTAL` rows 2. Create an ordered factor of the months, which follow the fiscal year April to March. This is done using the fact that the months appear in column-order as well as year-order, so we can sort on `col`. cashflows <- xlsx_cells(smungs::cashflows) %>% dplyr::filter(!is_blank, row >= 4L) %>% select(row, col, data_type, character, numeric) %>% behead("up", "month") %>% behead("left-up", "main_header") %>% behead("left", "sub_header") %>% dplyr::filter(month != "TOTALS", !str_detect(sub_header, "otal")) %>% arrange(col) %>% mutate(month = factor(month, levels = unique(month), ordered = TRUE), sub_header = str_trim(sub_header)) %>% select(main_header, sub_header, month, value = numeric) cashflows ## # A tibble: 336 x 4 ## main_header sub_header month value ## ## 1 Cash Inflows (Income): Cash Collections April 2227 ## 2 Cash Inflows (Income): Credit Collections April -4712 ## 3 Cash Inflows (Income): Investment Income April -2412 ## 4 Cash Inflows (Income): Other: April 490 ## 5 Cash Outflows (Expenses): Advertising April -324 ## 6 Cash Outflows (Expenses): Bank Service Charges April 3221 ## 7 Cash Outflows (Expenses): Insurance April 960 ## 8 Cash Outflows (Expenses): Interest April 936 ## 9 Cash Outflows (Expenses): Inventory Purchases April 2522 ## 10 Cash Outflows (Expenses): Maintenance & Repairs April 3883 ## # … with 326 more rows To prove that the data is correct, we can reproduce the total row at the bottom (‘Ending Cash Balance’). cashflows %>% group_by(main_header, month) %>% summarise(value = sum(value)) %>% arrange(month, main_header) %>% dplyr::filter(str_detect(main_header, "ows")) %>% mutate(value = if_else(str_detect(main_header, "Income"), value, -value)) %>% group_by(month) %>% summarise(value = sum(value)) %>% mutate(value = cumsum(value)) ## `summarise()` regrouping by 'main_header' (override with `.groups` argument) ## `summarise()` ungrouping (override with `.groups` argument) ## # A tibble: 12 x 2 ## month value ## * ## 1 April -39895 ## 2 May -43080 ## 3 June -39830 ## 4 July -14108 ## 5 Aug -25194 ## 6 Sept -42963 ## 7 Oct -39635 ## 8 Nov -29761 ## 9 Dec -49453 ## 10 Jan -30359 ## 11 Feb -33747 ## 12 Mar -27016 --- # 9.1 Australian Marriage Survey | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/australian-marriage-survey.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.1 Australian Marriage Survey ------------------------------ [![Tweet by Miles McBain about tidying the Australian Marriage Survey results with R](https://nacnudus.github.io/spreadsheet-munging-strategies/images/ozmarriage_tweet.png)](https://twitter.com/MilesMcBain/status/932257990253645829) These are the results of a survey in 2017 by the Australian Bureau of Statistics that asked, “Should the law be changed to allow same-sex couples to marry?” There are two tables with structures that are similar but different. [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/ozmarriage.xlsx?raw=true) . [Original source](http://www.abs.gov.au/ausstats/abs@.nsf/mf/1800.0) . ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/ozmarriage1.png) ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/ozmarriage2.png) ### 9.1.1 The full code listing cells <- xlsx_cells(smungs::ozmarriage) formats <- xlsx_formats(smungs::ozmarriage) table_1 <- cells %>% dplyr::filter(sheet == "Table 1", row >= 5L, !is_blank) %>% mutate(character = str_trim(character)) %>% behead("up-left", "population") %>% behead("up-left", "response") %>% behead("up", "unit") %>% behead("left", "state") %>% arrange(row, col) %>% select(row, data_type, numeric, state, population, response, unit) %>% spatter(unit) %>% select(-row) state <- cells %>% dplyr::filter(sheet == "Table 2", row >= 5L, col == 1L, !is_blank, formats$local$font$bold[local_format_id]) %>% select(row, col, state = character) table_2 <- cells %>% dplyr::filter(sheet == "Table 2", row >= 5L, !is_blank) %>% mutate(character = str_trim(character)) %>% behead("up-left", "population") %>% behead("up-left", "response") %>% behead("up", "unit") %>% behead("left", "territory") %>% enhead(state, "up-left") %>% arrange(row, col) %>% select(row, data_type, numeric, state, territory, population, response, unit) %>% spatter(unit) %>% select(-row) all_tables <- bind_rows("Table 1" = table_1, "Table 2" = table_2, .id = "sheet") all_tables ## # A tibble: 1,176 x 7 ## sheet state population response `%` no. territory ## ## 1 Table 1 New South Wales Eligible Participants Non-responding 20.5 1065445 ## 2 Table 1 New South Wales Eligible Participants Response clear 79.2 4111200 ## 3 Table 1 New South Wales Eligible Participants Response not clear(a) 0.2 11036 ## 4 Table 1 New South Wales Eligible Participants Total 100 5187681 ## 5 Table 1 New South Wales Response clear No 42.2 1736838 ## 6 Table 1 New South Wales Response clear Total 100 4111200 ## 7 Table 1 New South Wales Response clear Yes 57.8 2374362 ## 8 Table 1 Victoria Eligible Participants Non-responding 18.3 743634 ## 9 Table 1 Victoria Eligible Participants Response clear 81.4 3306727 ## 10 Table 1 Victoria Eligible Participants Response not clear(a) 0.3 11028 ## # … with 1,166 more rows ### 9.1.2 Step by step #### 9.1.2.1 Table 1 The first rows, up to the column-headers, must be filtered out. The trailing rows below the table will be treated us row-headers, but because there is no data to join them to, they will be dropped automatically. That is handy, because otherwise we would have to know where the bottom of the table is, which is likely to change with later editions of the same data. Apart from filtering the first rows, the rest of this example is ‘textbook’. cells <- xlsx_cells(smungs::ozmarriage) table_1 <- cells %>% dplyr::filter(sheet == "Table 1", row >= 5L, !is_blank) %>% mutate(character = str_trim(character)) %>% behead("up-left", "population") %>% behead("up-left", "response") %>% behead("up", "unit") %>% behead("left", "state") %>% arrange(row, col) %>% select(row, data_type, numeric, state, population, response, unit) %>% spatter(unit) %>% select(-row) table_1 ## # A tibble: 63 x 5 ## state population response `%` no. ## ## 1 New South Wales Eligible Participants Non-responding 20.5 1065445 ## 2 New South Wales Eligible Participants Response clear 79.2 4111200 ## 3 New South Wales Eligible Participants Response not clear(a) 0.2 11036 ## 4 New South Wales Eligible Participants Total 100 5187681 ## 5 New South Wales Response clear No 42.2 1736838 ## 6 New South Wales Response clear Total 100 4111200 ## 7 New South Wales Response clear Yes 57.8 2374362 ## 8 Victoria Eligible Participants Non-responding 18.3 743634 ## 9 Victoria Eligible Participants Response clear 81.4 3306727 ## 10 Victoria Eligible Participants Response not clear(a) 0.3 11028 ## # … with 53 more rows #### 9.1.2.2 Table 2 This is like Table 1, broken down by division rather than by state. The snag is that the states are named in the same column as their divisions. Because the state names are formatted in bold, we can isolate them from the division names. With them out of the way, unpivot the rest of the table as normal, and then use `enhead()` at the end to join the state names back on. Since tables 1 and 2 are so similar structurally, they might as well be joined into one. cells <- xlsx_cells(smungs::ozmarriage) formats <- xlsx_formats(smungs::ozmarriage) state <- cells %>% dplyr::filter(sheet == "Table 2", row >= 5L, col == 1L, !is_blank, formats$local$font$bold[local_format_id]) %>% select(row, col, state = character) table_2 <- cells %>% dplyr::filter(sheet == "Table 2", row >= 5L, !is_blank) %>% mutate(character = str_trim(character)) %>% behead("up-left", "population") %>% behead("up-left", "response") %>% behead("up", "unit") %>% behead("left", "territory") %>% enhead(state, "up-left") %>% arrange(row, col) %>% select(row, data_type, numeric, state, territory, population, response, unit) %>% spatter(unit) %>% select(-row) all_tables <- bind_rows("Table 1" = table_1, "Table 2" = table_2, .id = "sheet") %>% select(sheet, state, territory, population, response, `%`, no.) all_tables ## # A tibble: 1,176 x 7 ## sheet state territory population response `%` no. ## ## 1 Table 1 New South Wales Eligible Participants Non-responding 20.5 1065445 ## 2 Table 1 New South Wales Eligible Participants Response clear 79.2 4111200 ## 3 Table 1 New South Wales Eligible Participants Response not clear(a) 0.2 11036 ## 4 Table 1 New South Wales Eligible Participants Total 100 5187681 ## 5 Table 1 New South Wales Response clear No 42.2 1736838 ## 6 Table 1 New South Wales Response clear Total 100 4111200 ## 7 Table 1 New South Wales Response clear Yes 57.8 2374362 ## 8 Table 1 Victoria Eligible Participants Non-responding 18.3 743634 ## 9 Table 1 Victoria Eligible Participants Response clear 81.4 3306727 ## 10 Table 1 Victoria Eligible Participants Response not clear(a) 0.3 11028 ## # … with 1,166 more rows --- # 2.6 Hierarchies in formatting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/hierarchies-in-formatting.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.6 Hierarchies in formatting ----------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/highlight-hierarchy.png) Different kinds of formatting might also represent different levels of a hierarchy, e.g. | | | | --- | --- | | none | good | | italic | satisfactory | | bold | poor | | bold & italic | fail | When each kind of formatting relates to a different level of one hierarchy, import the different kinds of formatting into different columns, and then combine them into a third column, perhaps using `paste()`, or `case_when()`. # Step 1: import the table taking only cell values and ignoring the formatting x <- read_excel(path, sheet = "highlight-hierarchy") x ## # A tibble: 4 x 2 ## Name Score ## ## 1 Matilda 7 ## 2 Nicholas 5 ## 3 Olivia 3 ## 4 Paul 1 # Step 2: import one kind of formatting of one column of the table # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell bold <- xlsx_formats(path)$local$font$bold italic <- xlsx_formats(path)$local$font$italic # Import all the cells, filter out the header row, filter for the first column, # and create a new column `fill` of the fill colours, by looking up the # local_format_id of each cell in the `formats` pallette. formats <- xlsx_cells(path, sheet = "highlight-hierarchy") %>% dplyr::filter(row >= 2, col == 1) %>% # Omit the header row mutate(bold = bold[local_format_id], italic = italic[local_format_id]) %>% mutate(grade = case_when(bold & italic ~ "fail", bold ~ "poor", italic ~ "satisfactory", TRUE ~ "good")) %>% select(bold, italic, grade) # Step 3: append the `fill` column to the rest of the data bind_cols(x, formats) ## # A tibble: 4 x 5 ## Name Score bold italic grade ## ## 1 Matilda 7 FALSE FALSE good ## 2 Nicholas 5 FALSE TRUE satisfactory ## 3 Olivia 3 TRUE FALSE poor ## 4 Paul 1 TRUE TRUE fail Here it is again, using only tidyxl and unpivotr. bold <- xlsx_formats(path)$local$font$bold italic <- xlsx_formats(path)$local$font$italic xlsx_cells(path, sheet = "highlight-hierarchy") %>% mutate(bold = bold[local_format_id], italic = italic[local_format_id]) %>% mutate(grade = case_when(bold & italic ~ "fail", bold ~ "poor", italic ~ "satisfactory", TRUE ~ "good")) %>% select(row, col, data_type, character, numeric, bold, italic, grade) %>% behead("up", header) %>% select(-col) %>% spatter(header) ## # A tibble: 4 x 6 ## row bold italic grade Name Score ## ## 1 2 FALSE FALSE good Matilda 7 ## 2 3 FALSE TRUE satisfactory Nicholas 5 ## 3 4 TRUE FALSE poor Olivia 3 ## 4 5 TRUE TRUE fail Paul 1 --- # 5.3 In-cell formatting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/in-cell-formatting.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5.3 In-cell formatting ---------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/in-cell-formatting.png) The previous section was about formatting applied at the level of cells. What about when multiple formats are applied within a single cell? A single word in a string might be a different colour, to stand out. Unlike cell-level formatting, in-cell formatting is very limited, so it can be provided as a data frame with the following columns. * bold * italic * underline * strike * vertAlign * size * color\_rgb * color\_theme * color\_indexed * color\_tint * font * family * scheme There is one of these data frames for each cell, and they are kept in a list-column called `character_formatted`. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "in-cell formatting") %>% select(address, character_formatted) ## # A tibble: 9 x 2 ## address character_formatted ## ## 1 A1 ## 2 A3 ## 3 B3 ## 4 A4 ## 5 B4 ## 6 A5 ## 7 B5 ## 8 A6 ## 9 B6 The way to access these data frames is via `tidyr::unnest()`. In this example, a single cell has a long string of words, where each word is formatted differently. xlsx_cells(path, sheet = "in-cell formatting") %>% dplyr::filter(address == "A1") %>% select(address, character_formatted) %>% unnest() ## Warning: `cols` is now required. ## Please use `cols = c(character_formatted)` ## # A tibble: 9 x 15 ## address character bold italic underline strike vertAlign size color_rgb color_theme ## ## 1 A1 in-cell: FALSE FALSE FALSE 0 FF000000 NA ## 2 A1 bold, TRUE FALSE FALSE 0 FF000000 NA ## 3 A1 italic, FALSE TRUE FALSE 0 FF000000 NA ## 4 A1 underlin… FALSE FALSE single FALSE 0 FF000000 NA ## 5 A1 striketh… FALSE FALSE TRUE 0 FF000000 NA ## 6 A1 superscr… FALSE FALSE FALSE superscr… 0 FF000000 NA ## 7 A1 red, FALSE FALSE FALSE 0 FFFF0000 NA ## 8 A1 arial, FALSE FALSE FALSE 0 NA ## 9 A1 size 14 FALSE FALSE FALSE 0 NA ## # … with 5 more variables: color_indexed , color_tint , font , family , ## # scheme It’s hard to think of a plausible example, so what follows is an implausible one that nevertheless occurred in real life. --- # 4.1 Small multiples with all headers present for each multiple | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/small-multiples-with-all-headers-present-for-each-multiple.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 4.1 Small multiples with all headers present for each multiple -------------------------------------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/small-multiples.png) The code to import one of these multiples will be simple. cells %>% behead("up-left", subject) %>% behead("up", header) %>% select(-col, -local_format_id) %>% spatter(header) %>% select(-row) The first table is in rows 1 to 4, columns 1 to 3, so we start by writing the code to import only that table. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "small-multiples") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric, local_format_id) table1 <- dplyr::filter(all_cells, row %in% 1:4, col %in% 1:3) table1 %>% behead("up-left", subject) %>% behead("up", header) %>% select(-col, -local_format_id) %>% spatter(header) %>% select(-row) ## # A tibble: 2 x 4 ## subject Grade Name Score ## ## 1 Classics F Matilda 1 ## 2 Classics D Olivia 2 We wrap that code in a function, to be applied to each separate table. unpivot <- function(cells) { cells %>% behead("up-left", subject) %>% behead("up", header) %>% select(-col, -local_format_id) %>% spatter(header) %>% select(-row) } Now we partition the spreadsheet into the separate tables. This is done by identifying a corner cell in each table. formats <- xlsx_formats(path) italic <- which(formats$local$font$italic) corners <- all_cells %>% dplyr::filter(local_format_id %in% italic) %>% select(row, col) partitions <- partition(all_cells, corners) partitions ## # A tibble: 4 x 3 ## corner_row corner_col cells ## ## 1 1 1 ## 2 1 5 ## 3 6 1 ## 4 6 5 Finally, map the unpivoting function over the partitions, and combine the results. partitions %>% mutate(cells = map(cells, unpivot)) %>% unnest() %>% select(-corner_row, -corner_col) ## Warning: `cols` is now required. ## Please use `cols = c(cells)` ## # A tibble: 8 x 4 ## subject Grade Name Score ## ## 1 Classics F Matilda 1 ## 2 Classics D Olivia 2 ## 3 History D Matilda 3 ## 4 History C Olivia 4 ## 5 Music B Matilda 5 ## 6 Music B Olivia 6 ## 7 Drama A Matilda 7 ## 8 Drama A Olivia 8 --- # 5.1 An example formatting lookup | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/an-example-formatting-lookup.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5.1 An example formatting lookup -------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/formatting.png) This example shows how to look up whether a cell is bold. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") cells <- xlsx_cells(path, sheet = "formatting") %>% select(row, col, character, style_format, local_format_id) cells ## # A tibble: 14 x 5 ## row col character style_format local_format_id ## ## 1 1 1 bold Normal 6 ## 2 2 1 italic Normal 8 ## 3 3 1 underline Normal 51 ## 4 4 1 strikethrough Normal 52 ## 5 5 1 red text Normal 12 ## 6 6 1 font size 14 Normal 53 ## 7 7 1 font arial Normal 54 ## 8 8 1 yellow fill Normal 11 ## 9 9 1 black border Normal 43 ## 10 10 1 thick border Normal 55 ## 11 11 1 dashed border Normal 56 ## 12 12 1 row height 30 Normal 1 ## 13 13 2 column width 16.76 Normal 1 ## 14 14 1 Bad' style Explanatory Text 57 formats <- xlsx_formats(path) bold <- formats$local$font$bold # The list of lists of lists of vectors bold ## [1] FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE ## [16] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [46] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE mutate(cells, bold = bold[local_format_id]) ## # A tibble: 14 x 6 ## row col character style_format local_format_id bold ## ## 1 1 1 bold Normal 6 TRUE ## 2 2 1 italic Normal 8 FALSE ## 3 3 1 underline Normal 51 FALSE ## 4 4 1 strikethrough Normal 52 FALSE ## 5 5 1 red text Normal 12 FALSE ## 6 6 1 font size 14 Normal 53 FALSE ## 7 7 1 font arial Normal 54 FALSE ## 8 8 1 yellow fill Normal 11 FALSE ## 9 9 1 black border Normal 43 FALSE ## 10 10 1 thick border Normal 55 FALSE ## 11 11 1 dashed border Normal 56 FALSE ## 12 12 1 row height 30 Normal 1 FALSE ## 13 13 2 column width 16.76 Normal 1 FALSE ## 14 14 1 Bad' style Explanatory Text 57 FALSE A quick way to see what formatting definitions exist is to use `str()`. (Scroll past this for now – you don’t need to memorise it). formats <- xlsx_formats(path) str(formats) ## List of 2 ## $ local:List of 6 ## ..$ numFmt : chr [1:59] "General" "General" "General" "General" ... ## ..$ font :List of 10 ## .. ..$ bold : logi [1:59] FALSE TRUE TRUE FALSE FALSE TRUE ... ## .. ..$ italic : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ underline: chr [1:59] NA NA NA NA ... ## .. ..$ strike : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ vertAlign: chr [1:59] NA NA NA NA ... ## .. ..$ size : num [1:59] 11 11 11 11 11 11 11 11 11 11 ... ## .. ..$ color :List of 4 ## .. .. ..$ rgb : chr [1:59] "FF000000" "FF000000" "FF000000" "FF000000" ... ## .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ name : chr [1:59] "Calibri" "Calibri" "Calibri" "Calibri" ... ## .. ..$ family : int [1:59] 2 2 2 2 2 2 2 2 2 2 ... ## .. ..$ scheme : chr [1:59] NA NA NA NA ... ## ..$ fill :List of 2 ## .. ..$ patternFill :List of 3 ## .. .. ..$ fgColor :List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ bgColor :List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ patternType: chr [1:59] NA NA NA NA ... ## .. ..$ gradientFill:List of 8 ## .. .. ..$ type : chr [1:59] NA NA NA NA ... ## .. .. ..$ degree: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ left : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ right : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ top : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ bottom: num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ stop1 :List of 2 ## .. .. .. ..$ position: num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ color :List of 4 ## .. .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. ..$ stop2 :List of 2 ## .. .. .. ..$ position: num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ color :List of 4 ## .. .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## ..$ border :List of 12 ## .. ..$ diagonalDown: logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ diagonalUp : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ outline : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ left :List of 2 ## .. .. ..$ style: chr [1:59] NA "thin" NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA "FF000000" NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ right :List of 2 ## .. .. ..$ style: chr [1:59] NA "thin" NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA "FF000000" NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ start :List of 2 ## .. .. ..$ style: chr [1:59] NA NA NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ end :List of 2 ## .. .. ..$ style: chr [1:59] NA NA NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ top :List of 2 ## .. .. ..$ style: chr [1:59] NA "thin" NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA "FF000000" NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ bottom :List of 2 ## .. .. ..$ style: chr [1:59] NA NA "thin" NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA "FF000000" NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ diagonal :List of 2 ## .. .. ..$ style: chr [1:59] NA NA NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ vertical :List of 2 ## .. .. ..$ style: chr [1:59] NA NA NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. ..$ horizontal :List of 2 ## .. .. ..$ style: chr [1:59] NA NA NA NA ... ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ theme : chr [1:59] NA NA NA NA ... ## .. .. .. ..$ indexed: int [1:59] NA NA NA NA NA NA NA NA NA NA ... ## .. .. .. ..$ tint : num [1:59] NA NA NA NA NA NA NA NA NA NA ... ## ..$ alignment :List of 8 ## .. ..$ horizontal : chr [1:59] "general" "center" "general" "general" ... ## .. ..$ vertical : chr [1:59] "bottom" "bottom" "bottom" "bottom" ... ## .. ..$ wrapText : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ readingOrder : chr [1:59] "context" "context" "context" "context" ... ## .. ..$ indent : int [1:59] 0 0 0 0 0 0 0 0 0 0 ... ## .. ..$ justifyLastLine: logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ shrinkToFit : logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. ..$ textRotation : int [1:59] 0 0 0 0 0 0 0 0 0 0 ... ## ..$ protection:List of 2 ## .. ..$ locked: logi [1:59] TRUE TRUE TRUE TRUE TRUE TRUE ... ## .. ..$ hidden: logi [1:59] FALSE FALSE FALSE FALSE FALSE FALSE ... ## $ style:List of 6 ## ..$ numFmt : Named chr [1:2] "General" "General" ## .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## ..$ font :List of 10 ## .. ..$ bold : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ italic : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ underline: Named chr [1:2] NA NA ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ strike : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ vertAlign: Named chr [1:2] NA NA ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ size : Named num [1:2] 11 11 ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ color :List of 4 ## .. .. ..$ rgb : Named chr [1:2] "FF000000" "FF9C0006" ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ name : Named chr [1:2] "Calibri" "Calibri" ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ family : Named int [1:2] 2 2 ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ scheme : Named chr [1:2] NA NA ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## ..$ fill :List of 2 ## .. ..$ patternFill :List of 3 ## .. .. ..$ fgColor :List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA "FFFFC7CE" ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ bgColor :List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA "FFCCCCFF" ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ patternType: Named chr [1:2] NA "solid" ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ gradientFill:List of 8 ## .. .. ..$ type : Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ degree: Named int [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ left : Named num [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ right : Named num [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ top : Named num [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ bottom: Named num [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ stop1 :List of 2 ## .. .. .. ..$ position: Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ color :List of 4 ## .. .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ stop2 :List of 2 ## .. .. .. ..$ position: Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ color :List of 4 ## .. .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## ..$ border :List of 12 ## .. ..$ diagonalDown: Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ diagonalUp : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ outline : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ left :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ right :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ start :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ end :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ top :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ bottom :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ diagonal :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ vertical :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ horizontal :List of 2 ## .. .. ..$ style: Named chr [1:2] NA NA ## .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. ..$ color:List of 4 ## .. .. .. ..$ rgb : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ theme : Named chr [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ indexed: Named int [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. .. .. ..$ tint : Named num [1:2] NA NA ## .. .. .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## ..$ alignment :List of 8 ## .. ..$ horizontal : Named chr [1:2] "general" "general" ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ vertical : Named chr [1:2] "bottom" "bottom" ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ wrapText : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ readingOrder : Named chr [1:2] "context" "context" ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ indent : Named int [1:2] 0 0 ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ justifyLastLine: Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ shrinkToFit : Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ textRotation : Named int [1:2] 0 0 ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## ..$ protection:List of 2 ## .. ..$ locked: Named logi [1:2] TRUE TRUE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" ## .. ..$ hidden: Named logi [1:2] FALSE FALSE ## .. .. ..- attr(*, "names")= chr [1:2] "Normal" "Explanatory Text" Why is this so complicated? For one thing, there are too many types of formatting available to include in the data frame given by `xlsx_cells()`. Consider borders: each cell can have a border on each of its four sides, as well as through the middle of the cell horizontally, vertically, diagonally up and diagonally down. Each border can have its own colour and linetype. Colour can be expressed as an RGB value, a theme number with or without a tint, or an index number. To express that in a data frame would take (4 sides + 4 through the middle) \* (4 ways to express colour + 1 linetype) = 40 columns. Just for borders. Instead, Excel dynamically defines combinations of formatting, as they occur, and gives ID numbers to those combinations. Each cell has a formatting ID, which is used to look up its particular combination of formats. Note that this means two cells that are both bold can have different formatting IDs, e.g. if one is also italic. There is also a hierarchy of formatting. The first formatting to be applied is the ‘style’. Every cell has a style, which by default is the ‘normal’ style. You can reformat all cells of the ‘normal’ style at once by updating the ‘normal’ style. Style formats are available under `xlsx_formats()$style` When you modify the format of a particular cell, then that modification is local to that cell. The cell’s local formatting is available under `xlsx_formats()$local`. Both `$style` and `$local` have the same structure, so it’s easy to switch from checking a cell’s style-level formatting to its local formatting. Here’s an example of looking up both the local bold formatting and the style-level bold formatting of a cell. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") cells <- xlsx_cells(path, sheet = "formatting") %>% select(row, col, character, style_format, local_format_id) %>% dplyr::filter(row == 1, col == 1) cells ## # A tibble: 1 x 5 ## row col character style_format local_format_id ## ## 1 1 1 bold Normal 6 formats <- xlsx_formats(path) local_bold <- formats$local$font$bold local_bold ## [1] FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE ## [16] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [46] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE style_bold <- formats$style$font$bold style_bold ## Normal Explanatory Text ## FALSE FALSE mutate(cells, style_bold = style_bold[style_format], local_bold = local_bold[local_format_id]) ## # A tibble: 1 x 7 ## row col character style_format local_format_id style_bold local_bold ## ## 1 1 1 bold Normal 6 FALSE TRUE Most of the time you will use the local formatting. You only need to check the style formatting when styles have been used in the spreadsheet (rare) and you want to ignore any local modifications of that style for particular cells. Conditional formatting is an obvious omission. It isn’t supported by tidyxl because it doesn’t encode any new information; it’s responds to cell values, which you already have. If you think you need it, feel free to open an [issue](https://github.com/nacnudus/tidyxl/issues) . --- # 3 Pivot tables | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 3 Pivot tables ============== ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) This part introduces pivot tables. [Tidyxl](https://nacnudus.github.io/tidyxl) and [unpivotr](https://nacnudus.github.io/unpivotr) come into their own here, and are (as far as I know) the only packages to acknowledge the intuitive grammar of pivot tables. Pivot tables are ones with more than one row of column headers, or more than one column of row headers, or both (and there can be more complex arrangements). Tables in that form take up less space on a page or a screen than ‘tidy’ tables, and are easier for humans to read. But most software can’t interpret or traverse data in that form; it must first be reshaped into a long, ‘tidy’ form, with a single row of column headers. It takes a lot of code to reshape a pivot table into a ‘tidy’ one, and the code has to be bespoke for each table. There’s no general solution, because it is ambiguous whether a given cell is part of a header or part of the data. There are some ambiguities in ‘tidy’ tables, too, which is why most functions for reading csv files allow you to specify whether the first row of the data is a header, and how many rows to skip before the data begins. Functions often guess, but they can never be certain. Pivot tables, being more complex, are so much more ambiguous that it isn’t reasonable to import them with a single function. A better way is to break the problem down into steps: 1. Identify which cells are headers, and which are data. 2. State how the data cells relate to the header cells. The first step is a matter of traversing the cells, which is _much easier_ if you load them with the [tidyxl](https://nacnudus.github.io/tidyxl) package, or pass the table through `as_cells()` in the [unpivotr](https://nacnudus.github.io/unpivotr) package. This gives you a table of cells and their properties; one row of the table describes one cell of the source table or spreadsheet. The first two properties are the row and column position of the cell, which makes it easy to filter for cells in a particular region of the spreadsheet. If the first row of cells is a header row, then you can filter for `row == 1`. Here is an example of a pivot table where the first two rows, and the first two columns, are headers. The other cells contain the data. First, see how the cells are laid out in the source file by importing it with readxl. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") original <- read_excel(path, sheet = "pivot-annotations", col_names = FALSE) ## New names: ## * `` -> ...1 ## * `` -> ...2 ## * `` -> ...3 ## * `` -> ...4 ## * `` -> ...5 ## * ... print(original, n = Inf) ## # A tibble: 6 x 6 ## ...1 ...2 ...3 ...4 ...5 ...6 ## ## 1 Female Male ## 2 Matilda Olivia Nicholas Paul ## 3 Humanities Classics 1 2 3 0 ## 4 History 3 4 5 1 ## 5 Performance Music 5 6 9 2 ## 6 Drama 7 8 12 3 Compare that with the long set of cells, one per row, that tidyxl gives. (Only a few properties of each cell are shown, to make it easier to read). cells <- xlsx_cells(path, sheets = "pivot-annotations") select(cells, row, col, data_type, character, numeric) %>% print(cells, n = 20) ## # A tibble: 32 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 5 blank NA ## 3 2 6 character Male NA ## 4 2 7 blank NA ## 5 3 4 character Matilda NA ## 6 3 5 character Olivia NA ## 7 3 6 character Nicholas NA ## 8 3 7 character Paul NA ## 9 4 2 character Humanities NA ## 10 4 3 character Classics NA ## 11 4 4 numeric 1 ## 12 4 5 numeric 2 ## 13 4 6 numeric 3 ## 14 4 7 numeric 0 ## 15 5 2 blank NA ## 16 5 3 character History NA ## 17 5 4 numeric 3 ## 18 5 5 numeric 4 ## 19 5 6 numeric 5 ## 20 5 7 numeric 1 ## # … with 12 more rows A similar result is obtained via `unpivotr::as_cells()`. original <- read_excel(path, sheet = "pivot-annotations", col_names = FALSE) ## New names: ## * `` -> ...1 ## * `` -> ...2 ## * `` -> ...3 ## * `` -> ...4 ## * `` -> ...5 ## * ... as_cells(original) %>% arrange(row, col) %>% print(n = 20) ## # A tibble: 36 x 4 ## row col data_type chr ## ## 1 1 1 chr ## 2 1 2 chr ## 3 1 3 chr Female ## 4 1 4 chr ## 5 1 5 chr Male ## 6 1 6 chr ## 7 2 1 chr ## 8 2 2 chr ## 9 2 3 chr Matilda ## 10 2 4 chr Olivia ## 11 2 5 chr Nicholas ## 12 2 6 chr Paul ## 13 3 1 chr Humanities ## 14 3 2 chr Classics ## 15 3 3 chr 1 ## 16 3 4 chr 2 ## 17 3 5 chr 3 ## 18 3 6 chr 0 ## 19 4 1 chr ## 20 4 2 chr History ## # … with 16 more rows (One difference is that `read_excel()` has filled in some missing cells with blanks, which `as_cells()` retains. Another is that `read_excel()` has coerced all data types to `character`, whereas `xlsx_cells()` preserved the original data types.) The tidyxl version is easier to traverse, because it describes the position of each cell as well as the value. To filter for the first row of headers: dplyr::filter(cells, row == 2, !is_blank) %>% select(row, col, character, numeric) ## # A tibble: 2 x 4 ## row col character numeric ## ## 1 2 4 Female NA ## 2 2 6 Male NA Or to filter for cells containing data (in this case, we know that only data cells are numeric) dplyr::filter(cells, data_type == "numeric") %>% select(row, col, numeric) ## # A tibble: 16 x 3 ## row col numeric ## ## 1 4 4 1 ## 2 4 5 2 ## 3 4 6 3 ## 4 4 7 0 ## 5 5 4 3 ## 6 5 5 4 ## 7 5 6 5 ## 8 5 7 1 ## 9 6 4 5 ## 10 6 5 6 ## 11 6 6 9 ## 12 6 7 2 ## 13 7 4 7 ## 14 7 5 8 ## 15 7 6 12 ## 16 7 7 3 By identifying the header cells separately from the data cells, and knowing exactly where they are on the sheet, we can associated the data cells with the relevant headers. To a human it is intuitive that the cells below and to the right of the header `Male` represent males, and that ones to the right of and below the header `Postgraduate qualification` represent people with postgraduate qualifications, but it isn’t so obvious to the computer. How would the computer know that the header `Male` doesn’t also relate to the column of cells below and to the left, beginning with `2`? This section shows how you can express the relationships between headers and data cells, using the [unpivotr](https://nacnudus.github.io/unpivotr) package. --- # 5.4 Multiple pieces of information in a single cell, with meaningful formatting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/multiple-pieces-of-information-in-a-single-cell-with-meaningful-formatting.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 5.4 Multiple pieces of information in a single cell, with meaningful formatting ------------------------------------------------------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/in-cell-formatting.png) The above table of products and their production readiness combines three pieces of information in a single cell. Believe it or not, this is based on a real-life example. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") xlsx_cells(path, sheet = "in-cell formatting") %>% dplyr::filter(address != "A1") %>% rectify() ## # A tibble: 4 x 3 ## `row/col` `1(A)` `2(B)` ## ## 1 3 ID Count ## 2 4 A1-TEST 1 ## 3 5 A2-PRODUCTION 2 ## 4 6 A3-PRODUCTION 3 In the `ID` column, the first section `"A1"`, `"A2"`, `"A3"` is the product ID. The second section `"TEST"`, `"PRODUCTION"` is the production readiness, and the formatting of `"TEST"` and `"PRODUCTION"` shows whether or not manufacturing failed. In the file, one of those strings is formatted red with a strikethrough, indicating failure. One way to extract the formatting is by unnesting, as above, but in this case we can get away with mapping over the nested data frames and pulling out a single value. strikethrough <- xlsx_cells(path, sheet = "in-cell formatting") %>% dplyr::filter(address != "A1", col == 1) %>% mutate(strikethrough = map_lgl(character_formatted, ~ any(.x$strike))) %>% select(row, col, character, strikethrough) This can then be joined onto the rest of the table, in the same way as the section “Already a tidy table but with meaningful formatting of single cells”. cells <- xlsx_cells(path, sheet = "in-cell formatting") %>% dplyr::filter(address != "A1") %>% select(row, col, data_type, character, numeric) strikethrough <- xlsx_cells(path, sheet = "in-cell formatting") %>% dplyr::filter(address != "A1", col == 1) %>% mutate(strikethrough = map_lgl(character_formatted, ~ any(.x$strike))) %>% select(row, strikethrough) left_join(cells, strikethrough, by = "row") %>% behead("up", header) %>% select(-col) %>% spatter(header) %>% select(ID, strikethrough, Count) ## # A tibble: 3 x 3 ## ID strikethrough Count ## ## 1 A1-TEST NA 1 ## 2 A2-PRODUCTION TRUE 2 ## 3 A3-PRODUCTION NA 3 --- # 9.3 US Crime | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.3 US Crime ------------ [![Tweet by Alberto Cairo about a spreadsheet that violates Hadley Wickham’s principles of tidy data](https://nacnudus.github.io/spreadsheet-munging-strategies/images/us-crime-tweet.jpg)](https://twitter.com/albertocairo/status/963133927530483712) These are two tables of numbers of crimes in the USA, by state and category of crime. Confusingly, they’re numbered Table 2 and Table 3. Table 1 exists but isn’t included in this case study because it is so straightforward. ### 9.3.1 Table 2 ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/us-crime-2.jpg) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/us-crime-2.xlsx?raw=true) . [Original source](https://ucr.fbi.gov/crime-in-the-u.s/2016/crime-in-the-u.s.-2016/tables/table-2) . #### 9.3.1.1 Simple version This is straightforward to import as long as you don’t care to organise the hierarchies of crimes and areas. For example, Conneticut is within the division New England, which itself is within the region Northeast, but if you don’t need to express those relationships in the data then you can ignore the bold formatting. The only slight snag is that the header cells in row 5 are blank. There is a header for the units “Rate per 100,000”, but no header for the units “Count” – the cells in those positions are empty. It would be a problem if the cells didn’t exist at all, because `behead("up", "unit")` wouldn’t be able to associate data cells with missing header cells. Fortunately they do exist (because they have formatting), they are just empty or `NA`. To make sure they aren’t ignored, use `drop_na = FALSE` in `behead()`, and then later fill the blanks in the `units` column with `"Count"`. cells <- xlsx_cells(smungs::us_crime_2) %>% mutate(character = map_chr(character_formatted, ~ ifelse(is.null(.x), character, .x$character[1])), character = str_replace_all(character, "\n", " ")) cells %>% dplyr::filter(row >= 4L) %>% select(row, col, data_type, character, numeric) %>% behead("up-left", "crime") %>% behead("up", "unit", drop_na = FALSE) %>% behead("left-up", "area") %>% behead("left", "year") %>% behead("left", "population") %>% dplyr::filter(year != "Percent change") %>% mutate(unit = if_else(unit == "", "Count", unit)) %>% select(row, data_type, numeric, unit, area, year, population, crime) %>% spatter(unit) %>% select(-row) ## # A tibble: 1,320 x 6 ## area year population crime Count `Rate per 100,00… ## ## 1 United States T… 2015 320896618 Aggravated assault 7.64e5 238. ## 2 United States T… 2015 320896618 Burglary 1.59e6 495. ## 3 United States T… 2015 320896618 Larceny-theft 5.72e6 1784. ## 4 United States T… 2015 320896618 Motor vehicle theft 7.13e5 222. ## 5 United States T… 2015 320896618 Murder and nonnegligent mans… 1.59e4 4.9 ## 6 United States T… 2015 320896618 Property crime 8.02e6 2500. ## 7 United States T… 2015 320896618 Rape (legacy definition) 9.13e4 28.4 ## 8 United States T… 2015 320896618 Rape (revised definition) 1.26e5 39.3 ## 9 United States T… 2015 320896618 Robbery 3.28e5 102. ## 10 United States T… 2015 320896618 Violent crime 1.23e6 385. ## # … with 1,310 more rows #### 9.3.1.2 Complex version If you do mind about grouping states within divisions within regions, and crimes within categories, then you have more work to do using `enhead()` rather than `behead()`. 1. Select the header cells at each level of the hierarchy and store them in their own variables. For example, filter for the bold cells in row 4, which are the categories of crimes, and store them in the `categories` variable. 2. Select the data cells, and use `enhead()` to join them to the headers. In fact the headers `unit`, `year`, `population` can be handled by `behead()`, because they aren’t hierarchichal, so only the variables `category`, `crime`, `region`, `division` and `state` are handled by `enhead()`. cells <- xlsx_cells(smungs::us_crime_2) %>% mutate(character = map_chr(character_formatted, ~ ifelse(is.null(.x), character, .x$character[1])), character = str_replace_all(character, "\n", " ")) formats <- xlsx_formats(smungs::us_crime_2) categories <- cells %>% dplyr::filter(row == 4L, data_type == "character", formats$local$font$bold[local_format_id]) %>% select(row, col, category = character) categories ## # A tibble: 2 x 3 ## row col category ## ## 1 4 4 Violent crime ## 2 4 16 Property crime crimes <- cells %>% dplyr::filter(row == 4L, data_type == "character") %>% mutate(character = if_else(character %in% categories$category, "Total", character)) %>% select(row, col, crime = character) crimes ## # A tibble: 13 x 3 ## row col crime ## ## 1 4 1 Area ## 2 4 2 Year ## 3 4 3 Population ## 4 4 4 Total ## 5 4 6 Murder and nonnegligent manslaughter ## 6 4 8 Rape (revised definition) ## 7 4 10 Rape (legacy definition) ## 8 4 12 Robbery ## 9 4 14 Aggravated assault ## 10 4 16 Total ## 11 4 18 Burglary ## 12 4 20 Larceny-theft ## 13 4 22 Motor vehicle theft regions <- cells %>% dplyr::filter(row >= 6L, col == 1L, data_type == "character", formats$local$font$bold[local_format_id]) %>% select(row, col, region = character) regions ## # A tibble: 5 x 3 ## row col region ## ## 1 6 1 United States Total ## 2 9 1 Northeast ## 3 45 1 Midwest ## 4 90 1 South ## 5 153 1 West divisions <- cells %>% dplyr::filter(row >= 6L, col == 1L, data_type == "character", !formats$local$font$bold[local_format_id], !str_detect(character, "^ {5}")) %>% select(row, col, division = character) divisions ## # A tibble: 21 x 3 ## row col division ## ## 1 12 1 New England ## 2 33 1 Middle Atlantic ## 3 48 1 East North Central ## 4 66 1 West North Central ## 5 93 1 South Atlantic ## 6 123 1 East South Central ## 7 138 1 West South Central ## 8 156 1 Mountain ## 9 183 1 Pacific ## 10 201 1 Puerto Rico ## # … with 11 more rows states <- cells %>% dplyr::filter(row >= 6L, col == 1L, data_type == "character") %>% mutate(character = if_else(str_detect(character, "^ {5}"), str_trim(character), "Total")) %>% select(row, col, state = character) states ## # A tibble: 77 x 3 ## row col state ## ## 1 6 1 Total ## 2 9 1 Total ## 3 12 1 Total ## 4 15 1 Connecticut ## 5 18 1 Maine ## 6 21 1 Massachusetts ## 7 24 1 New Hampshire ## 8 27 1 Rhode Island ## 9 30 1 Vermont ## 10 33 1 Total ## # … with 67 more rows cells %>% dplyr::filter(row >= 5L, col >= 2L) %>% select(row, col, data_type, character, numeric) %>% behead("up", "unit") %>% behead("left", "year") %>% behead("left", "population") %>% enhead(categories, "up-left") %>% enhead(crimes, "up-left") %>% enhead(regions, "left-up") %>% enhead(divisions, "left-up", drop = FALSE) %>% enhead(states, "left-up", drop = FALSE) %>% dplyr::filter(year != "Percent change") %>% select(value = numeric, category, crime, region, division, state, year, population) ## # A tibble: 2,640 x 8 ## value category crime region division state year population ## ## 1 42121 Violent cri… Total Northea… New Engla… Total 2015 14710229 ## 2 286. Violent cri… Total Northea… New Engla… Total 2015 14710229 ## 3 41598 Violent cri… Total Northea… New Engla… Total 2016 14735525 ## 4 282. Violent cri… Total Northea… New Engla… Total 2016 14735525 ## 5 326 Violent cri… Murder and nonnegligent … Northea… New Engla… Total 2015 14710229 ## 6 2.2 Violent cri… Murder and nonnegligent … Northea… New Engla… Total 2015 14710229 ## 7 292 Violent cri… Murder and nonnegligent … Northea… New Engla… Total 2016 14735525 ## 8 2 Violent cri… Murder and nonnegligent … Northea… New Engla… Total 2016 14735525 ## 9 4602 Violent cri… Rape (revised definition) Northea… New Engla… Total 2015 14710229 ## 10 31.3 Violent cri… Rape (revised definition) Northea… New Engla… Total 2015 14710229 ## # … with 2,630 more rows ### 9.3.2 Table 3 ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/us-crime-3.jpg) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/us-crime-3.xlsx?raw=true) . [Original source](https://ucr.fbi.gov/crime-in-the-u.s/2016/crime-in-the-u.s.-2016/tables/table-3) . This table is confusing to humans, let alone computers. The `Population` column seems to belong to a different table altogether, so that’s how we’ll treat it. 1. Import the `Population` column and the state/area headers to the left. 2. Import the crime-related column headers, and the state/area headers to the left. 3. Join the two datasets. The `statistic` header ends up having blank values due to the cells being blank, so these are manually filled in. The hierarchy of crime (e.g. ‘robbery’ is within ‘violent crime’) is ignored. That would be handled in the same way as for [Table 2](https://nacnudus.github.io/spreadsheet-munging-strategies/us-crime.html#us-crime-2) . cells <- xlsx_cells(smungs::us_crime_3) %>% mutate(character = map_chr(character_formatted, ~ ifelse(is.null(.x), character, .x$character[1])), character = str_replace_all(character, "\n", " ")) population <- cells %>% dplyr::filter(row >= 5L, col <= 4L) %>% behead("left-up", "state") %>% behead("left-up", "area") %>% behead("left", "statistic", drop_na = FALSE) %>% mutate(statistic = case_when(is.na(statistic) ~ "Population", statistic == "" ~ "Population", TRUE ~ str_trim(statistic))) %>% dplyr::filter(data_type == "numeric", !str_detect(area, regex("total", ignore_case = TRUE)), statistic != "Estimated total") %>% select(data_type, numeric, state, area, statistic) %>% spatter(statistic) crime <- cells %>% dplyr::filter(row >= 4, col != 5L) %>% behead("left-up", "state") %>% behead("left-up", "area") %>% behead("left", "statistic", formatters = list(character = str_trim)) %>% behead("up", "crime") %>% dplyr::filter(data_type == "numeric", !str_detect(area, regex("total", ignore_case = TRUE)), !is.na(statistic), statistic != "") %>% mutate(statistic = case_when(statistic == "Area actually reporting" ~ "Actual", statistic == "Estimated total" ~ "Estimated")) %>% select(data_type, numeric, state, area, statistic, crime) %>% spatter(statistic) left_join(population, crime) ## Joining, by = c("state", "area") ## # A tibble: 1,480 x 7 ## state area `Area actually repo… Population crime Actual Estimated ## ## 1 ALABA… Cities outside … 0.966 520422 Aggravated assa… 2.84e+3 2914 ## 2 ALABA… Cities outside … 0.966 520422 Burglary 4.17e+3 4275 ## 3 ALABA… Cities outside … 0.966 520422 Larceny- theft 1.43e+4 14641 ## 4 ALABA… Cities outside … 0.966 520422 Motor vehicle … 1.34e+3 1375 ## 5 ALABA… Cities outside … 0.966 520422 Murder and nonn… 4.10e+1 42 ## 6 ALABA… Cities outside … 0.966 520422 Population 9.66e-1 1 ## 7 ALABA… Cities outside … 0.966 520422 Property crime 1.98e+4 20291 ## 8 ALABA… Cities outside … 0.966 520422 Rape (legacy def… 1.87e+2 193 ## 9 ALABA… Cities outside … 0.966 520422 Rape (revised de… 2.63e+2 269 ## 10 ALABA… Cities outside … 0.966 520422 Robbery 4.10e+2 421 ## # … with 1,470 more rows --- # 9.4 Toronto Transit Commission | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/toronto-transit-commission.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.4 Toronto Transit Commission ------------------------------ [![Tweet by Sharla Gelfand about tidying Toronto Transit Commission data with R](https://nacnudus.github.io/spreadsheet-munging-strategies/images/toronto-tweet.png)](https://twitter.com/sharlagelfand/status/996739127562391552) This table shows the number of trips recorded on the Toronto Transit Commission per year, by type of ticket, person, vehicle, and weeday/weekend/holiday. Sharla Gelfand’s annotated screenshot explains the structure, and see her excellent [blog post](https://sharlagelfand.netlify.com/posts/tidy-ttc/) for how she wrangled it with standard tidyverse tools. I show here an alternative method with tidyxl and unpivotr. [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/toronto_transit.xlsx?raw=true) . [Original source](https://portal0.cf.opendata.inter.sandbox-toronto.ca/dataset/ttc-ridership-analysis) . ### 9.4.1 The full code listing cells <- xlsx_cells(smungs::toronto_transit) %>% dplyr::filter(!is_blank, row >= 6) fare <- cells %>% dplyr::filter(col == 2, !str_detect(character, "^ "), !str_detect(character, "TOTAL")) %>% select(row, col, fare = character) cells %>% behead("up", "year", formatters = list(character = str_trim)) %>% behead("left-up", "context") %>% behead("left", "media", formatters = list(character = str_trim)) %>% enhead(fare, "left-up") %>% dplyr::filter(!str_detect(media, "TOTAL")) %>% separate(year, c("year", "note"), sep = " ", fill = "right") %>% select(year, context, fare, media, count = numeric) ## # A tibble: 1,188 x 5 ## year context fare media count ## ## 1 2017 WHO ADULT TOKENS 76106 ## 2 2016 WHO ADULT TOKENS 102073 ## 3 2015 WHO ADULT TOKENS 110945 ## 4 2014 WHO ADULT TOKENS 111157 ## 5 2013 WHO ADULT TOKENS 112360 ## 6 2012 WHO ADULT TOKENS 117962 ## 7 2011 WHO ADULT TOKENS 124748 ## 8 2010 WHO ADULT TOKENS 120366 ## 9 2009 WHO ADULT TOKENS 114686 ## 10 2008 WHO ADULT TOKENS 94210 ## # … with 1,178 more rows ### 9.4.2 Step by step Although the annotations point out that there are really three separate tables (`WHO`, `WHERE` and `WHEN`), they can be imported as one. Column 2 has two levels of headers in it: the fare in bold (“ADULT”, “BUS”, etc.), and the media used to pay for it indented by a few spaces (“TOKENS”, “WEEKLY PASS”, etc.). Because `behead()` can’t distinguish between different levels of headers in the same column, we need to put the bold fare headers into a separate variable on their own, and `enhead()` them back onto the rest of the table later. Unfortunately the fare headers in the “WHEN” context aren’t bold, so rather than filter for bold headers, instead we filter for headers that aren’t indented by spaces. We also filter out any “TOTAL” headers. cells <- xlsx_cells(smungs::toronto_transit) %>% dplyr::filter(!is_blank, row >= 6) fare <- cells %>% dplyr::filter(col == 2, !str_detect(character, "^ "), # Filter out indented headers !str_detect(character, "TOTAL")) %>% # Filteroout totals select(row, col, fare = character) fare ## # A tibble: 7 x 3 ## row col fare ## ## 1 7 2 ADULT ## 2 21 2 SENIOR/STUDENT ## 3 31 2 CHILDREN ## 4 43 2 BUS ## 5 46 2 RAIL ## 6 53 2 WEEKDAY ## 7 54 2 WEEKEND/HOLIDAY ttc <- cells %>% behead("up", "year") %>% behead("left-up", "context") %>% behead("left", "media") %>% enhead(fare, "left-up") %>% dplyr::filter(!str_detect(media, "TOTAL")) %>% select(year, context, fare, media, count = numeric) ttc ## # A tibble: 1,188 x 5 ## year context fare media count ## ## 1 "2017" WHO ADULT " TOKENS" 76106 ## 2 "2016" WHO ADULT " TOKENS" 102073 ## 3 " 2015 *" WHO ADULT " TOKENS" 110945 ## 4 "2014" WHO ADULT " TOKENS" 111157 ## 5 "2013" WHO ADULT " TOKENS" 112360 ## 6 "2012" WHO ADULT " TOKENS" 117962 ## 7 "2011" WHO ADULT " TOKENS" 124748 ## 8 "2010" WHO ADULT " TOKENS" 120366 ## 9 "2009" WHO ADULT " TOKENS" 114686 ## 10 "2008" WHO ADULT " TOKENS" 94210 ## # … with 1,178 more rows There’s a bit more cosmetic cleaning to do. The indentation can be trimmed from the `media` and the `year` headers, and the asterisk removed from the year `2015 *`. ttc %>% mutate(year = str_trim(year), media = str_trim(media)) %>% separate(year, c("year", "note"), sep = " ", fill = "right") %>% select(-note) ## # A tibble: 1,188 x 5 ## year context fare media count ## ## 1 2017 WHO ADULT TOKENS 76106 ## 2 2016 WHO ADULT TOKENS 102073 ## 3 2015 WHO ADULT TOKENS 110945 ## 4 2014 WHO ADULT TOKENS 111157 ## 5 2013 WHO ADULT TOKENS 112360 ## 6 2012 WHO ADULT TOKENS 117962 ## 7 2011 WHO ADULT TOKENS 124748 ## 8 2010 WHO ADULT TOKENS 120366 ## 9 2009 WHO ADULT TOKENS 114686 ## 10 2008 WHO ADULT TOKENS 94210 ## # … with 1,178 more rows --- # 9.7 School performance | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/school-performance.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 9.7 School performance ---------------------- A certain United States state education department provides its schools with spreadsheets of statistics. I bet the children in that state get a great education, because there’s at least one R enthusiast on the staff whose curiosity has never left them. ### 9.7.1 Sheet 1 The first sheet is an example of [mixed headers in column 1 being distinguished by bold formatting](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html#mixed-levels-of-headers-in-the-same-rowcolumn-distinguished-by-formatting) . Filter for the bold cells in column 1 and assign them to a variable. Then `behead()` the other headers, and finally `enhead()` the bold headers back on. ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/school1.png) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/school.xlsx?raw=true) , modified from an original source provided to the author. cells <- xlsx_cells(smungs::school, "Sheet1") %>% dplyr::filter(!is_blank) formats <- xlsx_formats(smungs::school) bold_headers <- cells %>% dplyr::filter(col == 1L, formats$local$font$bold[local_format_id]) %>% select(row, col, bold_header = character) cells %>% behead("up-left", "metric") %>% behead("left", "plain-header") %>% enhead(bold_headers, "left-up") %>% select(row, data_type, numeric, metric, `plain-header`) %>% spatter(metric) %>% select(-row) ## # A tibble: 21 x 10 ## `plain-header` `% Advanced` `% Needs Improv… `% Proficient` `% Proficient o… ## ## 1 All Students 0.515 0.0297 0.446 0.960 ## 2 Economically … 0.333 0.0667 0.567 0.9 ## 3 Non-Economica… 0.592 0.0141 0.394 0.986 ## 4 Students w/ D… NA NA NA NA ## 5 Non-Disabled 0.565 0.0217 0.413 0.978 ## 6 ELL NA NA NA NA ## 7 Non-ELL 0.525 0.0202 0.444 0.970 ## 8 African Amer.… NA NA NA NA ## 9 Asian NA NA NA NA ## 10 Hispanic/Lati… NA NA NA NA ## # … with 11 more rows, and 5 more variables: `% Warning/ Failing` , CPI , `Median ## # SGP` , `N Included` , `N Included in SGP` ### 9.7.2 Sheet 2 The second sheet is variation on [two clear rows of text column headers, left aligned](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html#two-clear-rows-of-text-column-headers-left-aligned-1) . Here, there are three rows of colum headers. The first row is left-aligned, and the second and third rows are directly above the data cells. But the second row is blank above columns D and E. That doesn’t actually matter; in the output, `header_2` will be `NA` for data from those columns. ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/school2.png) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/school.xlsx?raw=true) , modified from an original source provided to the author. xlsx_cells(smungs::school, "Sheet2") %>% select(row, col, address, data_type, character, numeric, is_blank) %>% mutate(character = str_trim(character)) %>% behead("up-left", "header_1") %>% behead("up", "header_2") %>% behead("up", "header_3") %>% behead("left", "classroom") %>% dplyr::filter(!is_blank, !is.na(header_3)) %>% arrange(col, row) ## # A tibble: 32 x 11 ## row col address data_type character numeric is_blank header_1 header_2 header_3 ## ## 1 5 4 D5 character 10 NA FALSE MCAS Su… MCAS Gr… ## 2 6 4 D6 character 10 NA FALSE MCAS Su… MCAS Gr… ## 3 7 4 D7 character 10 NA FALSE MCAS Su… MCAS Gr… ## 4 8 4 D8 character 10 NA FALSE MCAS Su… MCAS Gr… ## 5 5 5 E5 character 4 NA FALSE MCAS Su… # Tested ## 6 6 5 E6 character 8 NA FALSE MCAS Su… # Tested ## 7 7 5 E7 character 5 NA FALSE MCAS Su… # Tested ## 8 8 5 E8 character 10 NA FALSE MCAS Su… # Tested ## 9 5 6 F5 numeric 0.342 FALSE Possibl… Total P… % ## 10 6 6 F6 numeric 0.319 FALSE Possibl… Total P… % ## # … with 22 more rows, and 1 more variable: classroom ### 9.7.3 Sheet 3 The third sheet is variation on [two clear rows of text column headers, left aligned](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html#two-clear-rows-of-text-column-headers-left-aligned-1) , with a nasty catch. The creator of the spreadsheet didn’t merge cells to make space for more words. They didn’t even ‘centre across selection’ (which is sometimes safer than merging cells). Instead, they wrote each word on a separate line, meaning it is ambiguous whether a cell part of another header, or a header in its own right. ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/school3.png) [Download the file](https://github.com/nacnudus/smungs/blob/master/inst/extdata/school.xlsx?raw=true) , modified from an original source provided to the author. Compare columns C and D. Column C has a single header, “Avg Years w/ Class Data”, written across four cells. Column D has two levels of headers, “Years in MA” first, then “% 3+” nested within it (and written across two cells). There’s no way for a machine to tell which cells are whole headers, and which are parts of headers. We can deal with this by first treating every cell as a header in its own right, and then concatenating the headers of rows 2 to 5. Using the `"up-left"` direction, headers like “Years in MA” in cell D4 will be carried to the right, which is good. Unfortunately so will headers like “# Students” in cell B2, which we’ll just have to put up with. cells <- xlsx_cells(smungs::school, "Sheet3") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) x <- cells %>% behead("left", "place") %>% behead("up-left", "category") %>% behead("up-left", "metric-cell-1") %>% # Treat every cell in every row as a header behead("up-left", "metric-cell-2") %>% behead("up-left", "metric-cell-3") %>% behead("up-left", "metric-cell-4") %>% behead("up-left", "metric-cell-5") glimpse(x) ## Rows: 36 ## Columns: 12 ## $ row 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8… ## $ col 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, … ## $ data_type "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "… ## $ character NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,… ## $ numeric 1.000000000, 1.000000000, 1.000000000, 0.842777337, 0.896170213, 0.… ## $ place "State (All Students)", "Region", "School", "State (All Students)",… ## $ category "STUDENTS", "STUDENTS", "STUDENTS", "EDUCATOR EXPERIENCE", "EDUCATO… ## $ `metric-cell-1` "# Students", "# Students", "# Students", "# Students", "# Students… ## $ `metric-cell-2` "Avg", "Avg", "Avg", "Avg", "Avg", "Avg", "Avg", "Avg", "Avg", "Avg… ## $ `metric-cell-3` "Years w/", "Years w/", "Years w/", "Years in MA", "Years in MA", "… ## $ `metric-cell-4` "Class", "Class", "Class", "%", "%", "%", "%", "%", "%", "%", "%", … ## $ `metric-cell-5` "Data", "Data", "Data", "3+", "3+", "3+", "1-2", "1-2", "1-2", "0-1… Above you can see that every cell in every header row has been treated as a header in its own right, e.g. `"Avg"` is a level-2 header, and `"Years w/"` is a level-3 header. The next step is to paste them together into a single header. x <- x %>% # Replace NA with "" otherwise unite() will spell it as "NA". # This is a common irritation. # https://stackoverflow.com/questions/13673894/suppress-nas-in-paste mutate_at(vars(starts_with("metric-cell-")), replace_na, "") %>% unite("metric", starts_with("metric-cell-"), sep = " ") %>% mutate(metric = str_trim(metric)) glimpse(x) ## Rows: 36 ## Columns: 8 ## $ row 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7, 8, 9, 7… ## $ col 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10… ## $ data_type "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeri… ## $ character NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… ## $ numeric 1.000000000, 1.000000000, 1.000000000, 0.842777337, 0.896170213, 0.846153… ## $ place "State (All Students)", "Region", "School", "State (All Students)", "Regi… ## $ category "STUDENTS", "STUDENTS", "STUDENTS", "EDUCATOR EXPERIENCE", "EDUCATOR EXPE… ## $ metric "# Students Avg Years w/ Class Data", "# Students Avg Years w/ Class Data… Now the headers are manageable. They aren’t perfect – the `"# Students"` header has leaked into `"# Students Avg Years w/ Class Data"`, but that can be cleaned up manually later. At least `"# Students Avg Years w/ Class Data"` is within the `"STUDENTS"` category, which is the hard part. Spreading this data is the final step to make it easy to work with. x %>% select(place, category, metric, numeric) %>% spread(place, numeric) %>% print(n = Inf) ## # A tibble: 12 x 5 ## category metric Region School `State (All Student… ## ## 1 EDUCATOR EXPERIENCE # Students Avg Years in MA %… 0.0439 0 0.0535 ## 2 EDUCATOR EXPERIENCE # Students Avg Years in MA %… 0.0599 0.154 0.104 ## 3 EDUCATOR EXPERIENCE # Students Avg Years in MA %… 0.896 0.846 0.843 ## 4 EDUCATOR EXPERIENCE # Students PTS % Non- PTS 0.248 0.0684 0.247 ## 5 EDUCATOR EXPERIENCE # Students PTS Years in MA %… 0.752 0.932 0.753 ## 6 EDUCATOR QUALIFICATI… # Students % % In Field 0.944 1 0.903 ## 7 EDUCATOR QUALIFICATI… # Students % % Non-SEI Endor… NA NA NA ## 8 EDUCATOR QUALIFICATI… # Students % % SEI Endorsed NA NA NA ## 9 EDUCATOR QUALIFICATI… # Students % Long Term Subs 0.00182 0 0.0112 ## 10 EDUCATOR QUALIFICATI… # Students % Out of Field Fi… 0.0556 0 0.0965 ## 11 STUDENTS # Students 625 116 738499 ## 12 STUDENTS # Students Avg Years w/ Clas… 1 1 1 --- # 2.4 Meaningfully formatted cells | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.4 Meaningfully formatted cells -------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/annotations.png) If single cells are highlighted, rather than whole rows, then the highlights probably indicate something about the column rather than the row. For example, a highlighted cell in a column called “age” of a table of medical patients, might mean “the age of this patient is uncertain”. One way to deal with this is to create a new column in the final table for each column in the original that has any highlighted cells. For example, if highlighted cells mean “this value is uncertain”, and some cells in the `age` and `height` columns are highlighted, then you could create two new columns: `uncertain_age`, and `uncertain_height`, by following the procedure of [meaningfully formatted rows](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows) for each column `age` and `height`. # Step 1: import the table taking only cell values and ignoring the formatting path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "annotations") # Step 2: import one column of the table, taking only the formatting and not the # cell values # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create new columns `something_fill` of the fill colours, by looking up the # local_format_id of each cell in the `formats` pallette. fills <- xlsx_cells(path, sheet = "annotations") %>% dplyr::filter(row >= 2, col >= 2) %>% # Omit the header row and name column mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) %>% spread(col, fill_colour) %>% select(-row) %>% set_names(paste0(colnames(x)[-1], "_fill")) fills ## # A tibble: 3 x 2 ## Age_fill Height_fill ## ## 1 ## 2 FFFFFF00 ## 3 FF92D050 # Step 3: append the `fill` column to the rest of the data bind_cols(x, fills) ## # A tibble: 3 x 5 ## Name Age Height Age_fill Height_fill ## ## 1 Matilda 1 2 ## 2 Nicholas 3 4 FFFFFF00 ## 3 Olivia 5 6 FF92D050 Here’s the same thing, but using only tidyxl and unpivotr fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb cells <- xlsx_cells(path, sheet = "annotations") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) cells ## # A tibble: 12 x 6 ## row col data_type character numeric fill_colour ## ## 1 1 1 character Name NA ## 2 1 2 character Age NA ## 3 1 3 character Height NA ## 4 2 1 character Matilda NA ## 5 2 2 numeric 1 ## 6 2 3 numeric 2 ## 7 3 1 character Nicholas NA ## 8 3 2 numeric 3 FFFFFF00 ## 9 3 3 numeric 4 ## 10 4 1 character Olivia NA ## 11 4 2 numeric 5 ## 12 4 3 numeric 6 FF92D050 values <- cells %>% select(-fill_colour) %>% behead("up", header) %>% select(-col) %>% spatter(header) values ## # A tibble: 3 x 4 ## row Age Height Name ## ## 1 2 1 2 Matilda ## 2 3 3 4 Nicholas ## 3 4 5 6 Olivia fills <- cells %>% behead("up", header) %>% mutate(header = paste0(header, "_fill")) %>% select(row, header, fill_colour) %>% spread(header, fill_colour) fills ## # A tibble: 3 x 4 ## row Age_fill Height_fill Name_fill ## ## 1 2 ## 2 3 FFFFFF00 ## 3 4 FF92D050 left_join(values, fills, by = "row") %>% select(-row) ## # A tibble: 3 x 6 ## Age Height Name Age_fill Height_fill Name_fill ## ## 1 1 2 Matilda ## 2 3 4 Nicholas FFFFFF00 ## 3 5 6 Olivia FF92D050 Another way would be to make the table what I call “extra-tidy”. If it is tidy, then each row is an observation, and each column is a variable. To make it “extra-tidy”, you `gather()` the variables so that each row is _one observation of one variable_. This works best when every variable has the same data type, otherwise the values will be coerced, probably to a character. # Tidy (x <- read_excel(path, sheet = "annotations")) ## # A tibble: 3 x 3 ## Name Age Height ## ## 1 Matilda 1 2 ## 2 Nicholas 3 4 ## 3 Olivia 5 6 # Extra-tidy extra_tidy <- x %>% gather(variable, value, -Name) %>% arrange(Name, variable) extra_tidy ## # A tibble: 6 x 3 ## Name variable value ## ## 1 Matilda Age 1 ## 2 Matilda Height 2 ## 3 Nicholas Age 3 ## 4 Nicholas Height 4 ## 5 Olivia Age 5 ## 6 Olivia Height 6 With an extra-tidy dataset, the formatting can now be appended to the values of individual variables, rather than to whole observations. # Extra-tidy, with row and column numbers of the original variables extra_tidy <- read_excel(path, sheet = "annotations") %>% mutate(row = row_number() + 1L) %>% gather(variable, value, -row, -Name) %>% group_by(row) %>% mutate(col = row_number() + 1L) %>% ungroup() %>% select(row, col, Name, variable, value) %>% arrange(row, col) extra_tidy ## # A tibble: 6 x 5 ## row col Name variable value ## ## 1 2 2 Matilda Age 1 ## 2 2 3 Matilda Height 2 ## 3 3 2 Nicholas Age 3 ## 4 3 3 Nicholas Height 4 ## 5 4 2 Olivia Age 5 ## 6 4 3 Olivia Height 6 # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `uncertain` based on the fill colours, by looking up # the local_format_id of each cell in the `formats` pallette. fills <- xlsx_cells(path, sheet = "annotations") %>% dplyr::filter(row >= 2, col >= 2) %>% # Omit the header row and name column mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) fills ## # A tibble: 6 x 3 ## row col fill_colour ## ## 1 2 2 ## 2 2 3 ## 3 3 2 FFFFFF00 ## 4 3 3 ## 5 4 2 ## 6 4 3 FF92D050 # Step 3: append the `fill` column to the rest of the data left_join(extra_tidy, fills, by = c("row", "col")) ## # A tibble: 6 x 6 ## row col Name variable value fill_colour ## ## 1 2 2 Matilda Age 1 ## 2 2 3 Matilda Height 2 ## 3 3 2 Nicholas Age 3 FFFFFF00 ## 4 3 3 Nicholas Height 4 ## 5 4 2 Olivia Age 5 ## 6 4 3 Olivia Height 6 FF92D050 Here’s the same extra-tidy version, but using only tidyxl and unpivotr. fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb xlsx_cells(path, sheet = "annotations") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) %>% behead("left", Name) %>% behead("up", variable) %>% select(-data_type, -character, value = numeric) ## # A tibble: 6 x 6 ## row col value fill_colour Name variable ## ## 1 2 2 1 Matilda Age ## 2 2 3 2 Matilda Height ## 3 3 2 3 FFFFFF00 Nicholas Age ## 4 3 3 4 Nicholas Height ## 5 4 2 5 Olivia Age ## 6 4 3 6 FF92D050 Olivia Height --- # 3.1 Simple unpivoting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 3.1 Simple unpivoting --------------------- The `behead()` function takes one level of headers from a pivot table and makes it part of the data. Think of it like `tidyr::gather()`, except that it works when there is more than one row of headers (or more than one column of row-headers), and it only works on tables that have first come through `as_cells()` or `tidyxl::xlsx_cells()`. ### 3.1.1 Two clear rows of text column headers, left-aligned ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) Here we have a pivot table with two rows of column headers. The first row of headers is left-aligned, so `"Female"` applies to the first two columns of data, and `"Male"` applies to the next two. The second row of headers has a header in every column. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(col >= 4, !is_blank) %>% # Ignore the row headers in this example select(row, col, data_type, character, numeric) all_cells ## # A tibble: 22 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 4 numeric 1 ## 8 4 5 numeric 2 ## 9 4 6 numeric 3 ## 10 4 7 numeric 0 ## # … with 12 more rows The `behead()` function takes the ‘melted’ output of `as_cells()`, `tidyxl::xlsx_cells()`, or a previous `behead()`, and three more arguments to specify how the header cells relate to the data cells. The outermost header is the top row, `"Female" NA "Male" NA`. The `"Female"` and `"Male"` headers are up-and-to-the-left-of the data cells. We express this as `"up-left"`. We also give the headers a name, `sex`, and say which column of `all_cells` contains the value of the header cells – it’s usually the `character` column. all_cells %>% behead("up-left", sex) ## # A tibble: 20 x 6 ## row col data_type character numeric sex ## ## 1 3 4 character Matilda NA Female ## 2 3 5 character Olivia NA Female ## 3 4 4 numeric 1 Female ## 4 4 5 numeric 2 Female ## 5 5 4 numeric 3 Female ## 6 5 5 numeric 4 Female ## 7 6 4 numeric 5 Female ## 8 6 5 numeric 6 Female ## 9 7 4 numeric 7 Female ## 10 7 5 numeric 8 Female ## 11 3 6 character Nicholas NA Male ## 12 3 7 character Paul NA Male ## 13 4 6 numeric 3 Male ## 14 4 7 numeric 0 Male ## 15 5 6 numeric 5 Male ## 16 5 7 numeric 1 Male ## 17 6 6 numeric 9 Male ## 18 6 7 numeric 2 Male ## 19 7 6 numeric 12 Male ## 20 7 7 numeric 3 Male That did half the job. The value 2 in row 4 column 5 is indeed a score of a female. But the value `"matilda"` in row 3 column 4 isn’t a population – it’s another header. The next step is to strip that second level of column headers. This time, the direction is `"up"`, because the headers are directly up from the associated data cells, and we call it `name`, because it represents names of people. all_cells %>% behead("up-left", sex) %>% behead("up", `name`) ## # A tibble: 16 x 7 ## row col data_type character numeric sex name ## ## 1 4 4 numeric 1 Female Matilda ## 2 4 5 numeric 2 Female Olivia ## 3 5 4 numeric 3 Female Matilda ## 4 5 5 numeric 4 Female Olivia ## 5 6 4 numeric 5 Female Matilda ## 6 6 5 numeric 6 Female Olivia ## 7 7 4 numeric 7 Female Matilda ## 8 7 5 numeric 8 Female Olivia ## 9 4 6 numeric 3 Male Nicholas ## 10 4 7 numeric 0 Male Paul ## 11 5 6 numeric 5 Male Nicholas ## 12 5 7 numeric 1 Male Paul ## 13 6 6 numeric 9 Male Nicholas ## 14 6 7 numeric 2 Male Paul ## 15 7 6 numeric 12 Male Nicholas ## 16 7 7 numeric 3 Male Paul A final step is a normal clean-up. We drop the `row`, `col` and `character` columns, and we rename the `numeric` column to `score`, which is what it represents. all_cells %>% behead("up-left", sex) %>% behead("up", `name`) %>% select(score = numeric, sex, `name`) ## # A tibble: 16 x 3 ## score sex name ## ## 1 1 Female Matilda ## 2 2 Female Olivia ## 3 3 Female Matilda ## 4 4 Female Olivia ## 5 5 Female Matilda ## 6 6 Female Olivia ## 7 7 Female Matilda ## 8 8 Female Olivia ## 9 3 Male Nicholas ## 10 0 Male Paul ## 11 5 Male Nicholas ## 12 1 Male Paul ## 13 9 Male Nicholas ## 14 2 Male Paul ## 15 12 Male Nicholas ## 16 3 Male Paul ### 3.1.2 Two clear rows and columns of text headers, top-aligned and left-aligned ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) There are no new techniques are used, just more directions: `"left"` for headers directly to the left of the data cells, and `"left-up"` for headers left-then-up from the data cells. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 28 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 2 character Humanities NA ## 8 4 3 character Classics NA ## 9 4 4 numeric 1 ## 10 4 5 numeric 2 ## # … with 18 more rows all_cells %>% behead("up-left", sex) %>% # As before behead("up", `name`) %>% # As before behead("left-up", field) %>% # Left-and-above behead("left", subject) %>% # Directly left rename(score = numeric) %>% select(-row, -col, -character) ## # A tibble: 16 x 6 ## data_type score sex name field subject ## ## 1 numeric 1 Female Matilda Humanities Classics ## 2 numeric 2 Female Olivia Humanities Classics ## 3 numeric 3 Female Matilda Humanities History ## 4 numeric 4 Female Olivia Humanities History ## 5 numeric 3 Male Nicholas Humanities Classics ## 6 numeric 0 Male Paul Humanities Classics ## 7 numeric 5 Male Nicholas Humanities History ## 8 numeric 1 Male Paul Humanities History ## 9 numeric 5 Female Matilda Performance Music ## 10 numeric 6 Female Olivia Performance Music ## 11 numeric 7 Female Matilda Performance Drama ## 12 numeric 8 Female Olivia Performance Drama ## 13 numeric 9 Male Nicholas Performance Music ## 14 numeric 2 Male Paul Performance Music ## 15 numeric 12 Male Nicholas Performance Drama ## 16 numeric 3 Male Paul Performance Drama ### 3.1.3 Multiple rows or columns of headers, with meaningful formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) This is a combination of the previous section with [meaningfully formatted rows](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows) . The section [meaninfully formatted cells](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells) doesn’t work here, because the unpivoting of multiple rows/columns of headers complicates the relationship between the data and the formatting. 1. Unpivot the multiple rows/columns of headers, as above, but keep the `row` and `col` of each data cell. 2. Collect the `row`, `col` and formatting of each data cell. 3. Join the data to the formatting by the `row` and `col`. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 28 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 2 character Humanities NA ## 8 4 3 character Classics NA ## 9 4 4 numeric 1 ## 10 4 5 numeric 2 ## # … with 18 more rows unpivoted <- all_cells %>% behead("up-left", sex) %>% # As before behead("up", `name`) %>% # As before behead("left-up", field) %>% # Left-and-above behead("left", subject) %>% # Directly left rename(score = numeric) %>% select(-character) # Retain the row and col for now unpivoted ## # A tibble: 16 x 8 ## row col data_type score sex name field subject ## ## 1 4 4 numeric 1 Female Matilda Humanities Classics ## 2 4 5 numeric 2 Female Olivia Humanities Classics ## 3 5 4 numeric 3 Female Matilda Humanities History ## 4 5 5 numeric 4 Female Olivia Humanities History ## 5 4 6 numeric 3 Male Nicholas Humanities Classics ## 6 4 7 numeric 0 Male Paul Humanities Classics ## 7 5 6 numeric 5 Male Nicholas Humanities History ## 8 5 7 numeric 1 Male Paul Humanities History ## 9 6 4 numeric 5 Female Matilda Performance Music ## 10 6 5 numeric 6 Female Olivia Performance Music ## 11 7 4 numeric 7 Female Matilda Performance Drama ## 12 7 5 numeric 8 Female Olivia Performance Drama ## 13 6 6 numeric 9 Male Nicholas Performance Music ## 14 6 7 numeric 2 Male Paul Performance Music ## 15 7 6 numeric 12 Male Nicholas Performance Drama ## 16 7 7 numeric 3 Male Paul Performance Drama # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb fill_colours ## [1] NA NA NA NA NA NA NA NA ## [9] "FFFFFF00" "FF92D050" "FFFFFF00" NA NA "FFFFFF00" NA NA ## [17] NA NA NA NA NA "FFFFFF00" "FFFFFF00" NA ## [25] NA "FFFFFF00" NA NA NA NA NA NA ## [33] NA NA NA NA NA NA NA NA ## [41] NA NA NA NA NA NA NA NA ## [49] NA NA NA NA NA NA NA NA ## [57] "FFFFC7CE" NA NA # Import all the cells, filter out the header row, filter for the first column, # and create a new column `approximate` based on the fill colours, by looking up # the local_format_id of each cell in the `formats` pallette. annotations <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(row >= 4, col >= 4) %>% # Omit the headers mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) annotations ## # A tibble: 16 x 3 ## row col fill_colour ## ## 1 4 4 ## 2 4 5 FFFFFF00 ## 3 4 6 ## 4 4 7 ## 5 5 4 FFFFFF00 ## 6 5 5 ## 7 5 6 ## 8 5 7 ## 9 6 4 ## 10 6 5 ## 11 6 6 ## 12 6 7 ## 13 7 4 ## 14 7 5 ## 15 7 6 FFFFFF00 ## 16 7 7 left_join(unpivoted, annotations, by = c("row", "col")) %>% select(-row, -col) ## # A tibble: 16 x 7 ## data_type score sex name field subject fill_colour ## ## 1 numeric 1 Female Matilda Humanities Classics ## 2 numeric 2 Female Olivia Humanities Classics FFFFFF00 ## 3 numeric 3 Female Matilda Humanities History FFFFFF00 ## 4 numeric 4 Female Olivia Humanities History ## 5 numeric 3 Male Nicholas Humanities Classics ## 6 numeric 0 Male Paul Humanities Classics ## 7 numeric 5 Male Nicholas Humanities History ## 8 numeric 1 Male Paul Humanities History ## 9 numeric 5 Female Matilda Performance Music ## 10 numeric 6 Female Olivia Performance Music ## 11 numeric 7 Female Matilda Performance Drama ## 12 numeric 8 Female Olivia Performance Drama ## 13 numeric 9 Male Nicholas Performance Music ## 14 numeric 2 Male Paul Performance Music ## 15 numeric 12 Male Nicholas Performance Drama FFFFFF00 ## 16 numeric 3 Male Paul Performance Drama ### 3.1.4 Mixed headers and notes in the same row/column, distinguished by formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-notes.png) This needs two passes over each row/column that contains a mixture. The first pass, with `behead_if()` is to deal with the cells that are headers, and the second pass, with `dplyr::filter()` removes the remaining cells that are notes. The `behead_if()` function takes predicate functions to choose which cells are headers. # only treat bold cells beginning "Country: " as a header cells %>% behead_if(formats$local$font$bold[local_format_id], # true for bold cells str_detect(character, "^Country: "), # true for "Country: ..." direction = "left-up", # argument must be named name = "country_name") %>% dplyr::filter(col != 1L) # discard remaining cells Note that the `direction` and `name` arguments must now be named, because they follow the `...`. After `behead_if()`, any cells that haven’t been treated as headers will still exist, so if you want to discard them then use `dplyr::filter()` on the column or row number. In the screenshot above, cells with italic or red text aren’t headers, even though they are in amongst header cells. First, identify the IDs of formats that have italic or red text. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") formats <- xlsx_formats(path) italic <- formats$local$font$italic # For 'red' we can either look for the RGB code for red "FFFF0000" red <- "FFFF0000" # Or we can find out what that code is by starting from a cell that we know is # red. red_cell_format_id <- xlsx_cells(path, sheets = "pivot-notes") %>% dplyr::filter(row == 5, col == 2) %>% pull(local_format_id) red_cell_format_id ## [1] 40 red <- formats$local$font$color$rgb[red_cell_format_id] red ## [1] "FFFF0000" Now we use `behead_if()`, filtering out cells with the format IDs of red or italic cells. cells <- xlsx_cells(path, sheets = "pivot-notes") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric, local_format_id) %>% print() ## # A tibble: 31 x 6 ## row col data_type character numeric local_format_id ## ## 1 2 4 character Female NA 18 ## 2 2 6 character Male NA 18 ## 3 2 7 character 0 = absent NA 39 ## 4 3 4 character Matilda NA 20 ## 5 3 5 character Olivia NA 21 ## 6 3 6 character Nicholas NA 20 ## 7 3 7 character Paul NA 21 ## 8 4 2 character Humanities NA 18 ## 9 4 3 character Classics NA 19 ## 10 4 4 numeric 1 33 ## # … with 21 more rows cells %>% behead_if(!italic[local_format_id], # not italic direction = "up-left", name = "sex") %>% dplyr::filter(row != min(row)) %>% # discard non-header cells behead("up", "name") %>% behead_if(formats$local$font$color$rgb[local_format_id] != red, # not red direction = "left-up", name = "field") %>% dplyr::filter(col != min(col)) %>% # discard non-headere cells behead("left", "subject") %>% select(sex, name, field, subject, score = numeric) ## # A tibble: 16 x 5 ## sex name field subject score ## ## 1 Male Nicholas Humanities Classics 3 ## 2 Male Paul Humanities Classics 0 ## 3 Male Nicholas Humanities History 5 ## 4 Male Paul Humanities History 1 ## 5 Female Matilda Humanities Classics 1 ## 6 Female Olivia Humanities Classics 2 ## 7 Female Matilda Humanities History 3 ## 8 Female Olivia Humanities History 4 ## 9 Male Nicholas Performance Music 9 ## 10 Male Paul Performance Music 2 ## 11 Male Nicholas Performance Drama 12 ## 12 Male Paul Performance Drama 3 ## 13 Female Matilda Performance Music 5 ## 14 Female Olivia Performance Music 6 ## 15 Female Matilda Performance Drama 7 ## 16 Female Olivia Performance Drama 8 ### 3.1.5 Mixed levels of headers in the same row/column, distinguished by formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-hierarchy.png) Normally different levels of headers are in different rows, or different columns, like [Two clear rows of text column headers, left-aligned](https://nacnudus.github.io/spreadsheet-munging-strategies/2Rl) . But sometimes they coexist in the same row or column, and are distinguishable by formatting, e.g. by indentation, or bold for the top level, italic for the mid level, and plain for the lowest level. In this example, there is a single column of row headers, where the levels are shown by different amounts of indentation. The indentation is done by formatting, rather than by leading spaces or tabs. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") formats <- xlsx_formats(path) formats$local$alignment$indent ## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 ## [46] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 We can use the indentation with `behead_if()` to make two passes over the column of row headers, first for the unindented headers, then for the indented headers. cells <- xlsx_cells(path, sheets = "pivot-hierarchy") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric, local_format_id) %>% print() ## # A tibble: 16 x 6 ## row col data_type character numeric local_format_id ## ## 1 2 3 character Matilda NA 18 ## 2 2 4 character Nicholas NA 42 ## 3 3 2 character Humanities NA 18 ## 4 4 2 character Classics NA 44 ## 5 4 3 numeric 1 20 ## 6 4 4 numeric 3 45 ## 7 5 2 character History NA 44 ## 8 5 3 numeric 3 20 ## 9 5 4 numeric 5 45 ## 10 6 2 character Performance NA 20 ## 11 7 2 character Music NA 44 ## 12 7 3 numeric 5 20 ## 13 7 4 numeric 9 45 ## 14 8 2 character Drama NA 46 ## 15 8 3 numeric 7 24 ## 16 8 4 numeric 12 47 cells %>% behead_if(formats$local$alignment$indent[local_format_id] == 0, direction = "left-up", name = "field") %>% behead("left", "subject") %>% behead("up", "name") %>% select(field, subject, name, score = numeric) ## # A tibble: 8 x 4 ## field subject name score ## ## 1 Humanities Classics Matilda 1 ## 2 Humanities Classics Nicholas 3 ## 3 Humanities History Matilda 3 ## 4 Humanities History Nicholas 5 ## 5 Performance Music Matilda 5 ## 6 Performance Music Nicholas 9 ## 7 Performance Drama Matilda 7 ## 8 Performance Drama Nicholas 12 --- # 3.2 Complex unpivoting | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-complex.html# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 3.2 Complex unpivoting ---------------------- When `behead()` isn’t powerful enough (it makes certain assumptions, and it doesn’t understand formatting), then you can get much more control by using `enhead()`, which joins together two separate data frames of data cells and header cells. This kind of unpivoting is always done in two stages. 1. Identify which cells are headers, and which are data 2. State how the data cells relate to the header cells. ### 3.2.1 Two clear rows of text column headers, left-aligned ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) The first stage, identifying header vs data cells, is simply filtering. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(col >= 4, !is_blank) %>% # Ignore the row headers in this example select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 22 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 4 numeric 1 ## 8 4 5 numeric 2 ## 9 4 6 numeric 3 ## 10 4 7 numeric 0 ## # … with 12 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 6 x 5 ## `row/col` `4(D)` `5(E)` `6(F)` `7(G)` ## ## 1 2 Female Male ## 2 3 Matilda Olivia Nicholas Paul ## 3 4 1 2 3 0 ## 4 5 3 4 5 1 ## 5 6 5 6 9 2 ## 6 7 7 8 12 3 first_header_row <- dplyr::filter(all_cells, row == 2) %>% select(row, col, sex = character) # the title of this header is 'sex' # the cells are text cells (`"Female"` and `"Male"`) so take the value in the # '`character` column. first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 4 Female ## 2 2 6 Male second_header_row <- dplyr::filter(all_cells, row == 3) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. second_header_row ## # A tibble: 4 x 3 ## row col name ## ## 1 3 4 Matilda ## 2 3 5 Olivia ## 3 3 6 Nicholas ## 4 3 7 Paul data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is exam scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` The second stage is to declare how the data cells relate to each row of column headers. Starting from the point of view of a data cell, the relevant column header from the second row of headers is the one directly `"up"`. enhead(data_cells, second_header_row, "up") ## # A tibble: 16 x 4 ## row col score name ## ## 1 4 4 1 Matilda ## 2 4 5 2 Olivia ## 3 4 6 3 Nicholas ## 4 4 7 0 Paul ## 5 5 4 3 Matilda ## 6 5 5 4 Olivia ## 7 5 6 5 Nicholas ## 8 5 7 1 Paul ## 9 6 4 5 Matilda ## 10 6 5 6 Olivia ## 11 6 6 9 Nicholas ## 12 6 7 2 Paul ## 13 7 4 7 Matilda ## 14 7 5 8 Olivia ## 15 7 6 12 Nicholas ## 16 7 7 3 Paul The first row of headers, from the point of view of a data cell, is either directly up, or up-then-left. enhead(data_cells, first_header_row, "up-left") ## # A tibble: 16 x 4 ## row col score sex ## ## 1 4 4 1 Female ## 2 4 5 2 Female ## 3 5 4 3 Female ## 4 5 5 4 Female ## 5 6 4 5 Female ## 6 6 5 6 Female ## 7 7 4 7 Female ## 8 7 5 8 Female ## 9 4 6 3 Male ## 10 4 7 0 Male ## 11 5 6 5 Male ## 12 5 7 1 Male ## 13 6 6 9 Male ## 14 6 7 2 Male ## 15 7 6 12 Male ## 16 7 7 3 Male Piping everything together, we get a complete, tidy dataset, and can finally drop the `row` and `col` columns. data_cells %>% enhead(first_header_row, "up-left") %>% enhead(second_header_row, "up") %>% select(-row, -col) ## # A tibble: 16 x 3 ## score sex name ## ## 1 1 Female Matilda ## 2 2 Female Olivia ## 3 3 Female Matilda ## 4 4 Female Olivia ## 5 5 Female Matilda ## 6 6 Female Olivia ## 7 7 Female Matilda ## 8 8 Female Olivia ## 9 3 Male Nicholas ## 10 0 Male Paul ## 11 5 Male Nicholas ## 12 1 Male Paul ## 13 9 Male Nicholas ## 14 2 Male Paul ## 15 12 Male Nicholas ## 16 3 Male Paul ### 3.2.2 Two clear columns of text row headers, top-aligned ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) This is almost the same as [Two clear rows of text column headers, left-aligned](https://nacnudus.github.io/spreadsheet-munging-strategies/2RL) , but with different directions: `"left"` for directly left, and `"left-up"` for left-then-up. (`"up-left"` and `"left-up"` look like synonyms. They happen to be synonyms in `enhead()`, but they aren’t in `behead()`. In this example, the table has no column headers, only row headers. This is artificial here, but sometimes table are deliberately laid out in transpose form: the first column contains the headers, and the data extends in columns from left to right instead of from top to bottom. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(row >= 3, !is_blank) %>% # Ignore the column headers in this example select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 26 x 5 ## row col data_type character numeric ## ## 1 3 4 character Matilda NA ## 2 3 5 character Olivia NA ## 3 3 6 character Nicholas NA ## 4 3 7 character Paul NA ## 5 4 2 character Humanities NA ## 6 4 3 character Classics NA ## 7 4 4 numeric 1 ## 8 4 5 numeric 2 ## 9 4 6 numeric 3 ## 10 4 7 numeric 0 ## # … with 16 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 5 x 7 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` `7(G)` ## ## 1 3 Matilda Olivia Nicholas Paul ## 2 4 Humanities Classics 1 2 3 0 ## 3 5 History 3 4 5 1 ## 4 6 Performance Music 5 6 9 2 ## 5 7 Drama 7 8 12 3 first_header_col <- dplyr::filter(all_cells, col == 2) %>% select(row, col, field = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. first_header_col ## # A tibble: 2 x 3 ## row col field ## ## 1 4 2 Humanities ## 2 6 2 Performance second_header_col <- dplyr::filter(all_cells, col == 3) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. second_header_col ## # A tibble: 4 x 3 ## row col subject ## ## 1 4 3 Classics ## 2 5 3 History ## 3 6 3 Music ## 4 7 3 Drama data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is examp scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells %>% enhead(first_header_col, "left-up") %>% enhead(second_header_col, "left") %>% select(-row, -col) ## # A tibble: 16 x 3 ## score field subject ## ## 1 1 Humanities Classics ## 2 2 Humanities Classics ## 3 3 Humanities Classics ## 4 0 Humanities Classics ## 5 3 Humanities History ## 6 4 Humanities History ## 7 5 Humanities History ## 8 1 Humanities History ## 9 5 Performance Music ## 10 6 Performance Music ## 11 9 Performance Music ## 12 2 Performance Music ## 13 7 Performance Drama ## 14 8 Performance Drama ## 15 12 Performance Drama ## 16 3 Performance Drama ### 3.2.3 Two clear rows and columns of text headers, top-aligned and left-aligned ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) This is a combination of the previous two sections. No new techniques are used. 1. Identify which cells are headers, and which are data 2. State how the data cells relate to the header cells. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 28 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 2 character Humanities NA ## 8 4 3 character Classics NA ## 9 4 4 numeric 1 ## 10 4 5 numeric 2 ## # … with 18 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 6 x 7 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` `7(G)` ## ## 1 2 Female Male ## 2 3 Matilda Olivia Nicholas Paul ## 3 4 Humanities Classics 1 2 3 0 ## 4 5 History 3 4 5 1 ## 5 6 Performance Music 5 6 9 2 ## 6 7 Drama 7 8 12 3 first_header_row <- dplyr::filter(all_cells, row == 2) %>% select(row, col, sex = character) # the title of this header is 'sex' # the cells are text cells (`"Female"` and `"Male"`) so take the value in the # '`character` column. first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 4 Female ## 2 2 6 Male second_header_row <- dplyr::filter(all_cells, row == 3) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. second_header_row ## # A tibble: 4 x 3 ## row col name ## ## 1 3 4 Matilda ## 2 3 5 Olivia ## 3 3 6 Nicholas ## 4 3 7 Paul first_header_col <- dplyr::filter(all_cells, col == 2) %>% select(row, col, field = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. first_header_col ## # A tibble: 2 x 3 ## row col field ## ## 1 4 2 Humanities ## 2 6 2 Performance second_header_col <- dplyr::filter(all_cells, col == 3) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. second_header_col ## # A tibble: 4 x 3 ## row col subject ## ## 1 4 3 Classics ## 2 5 3 History ## 3 6 3 Music ## 4 7 3 Drama data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is examp scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells %>% enhead(first_header_row, "up-left") %>% enhead(second_header_row, "up") %>% enhead(first_header_col, "left-up") %>% enhead(second_header_col, "left") %>% select(-row, -col) ## # A tibble: 16 x 5 ## score sex name field subject ## ## 1 1 Female Matilda Humanities Classics ## 2 2 Female Olivia Humanities Classics ## 3 3 Female Matilda Humanities History ## 4 4 Female Olivia Humanities History ## 5 3 Male Nicholas Humanities Classics ## 6 0 Male Paul Humanities Classics ## 7 5 Male Nicholas Humanities History ## 8 1 Male Paul Humanities History ## 9 5 Female Matilda Performance Music ## 10 6 Female Olivia Performance Music ## 11 7 Female Matilda Performance Drama ## 12 8 Female Olivia Performance Drama ## 13 9 Male Nicholas Performance Music ## 14 2 Male Paul Performance Music ## 15 12 Male Nicholas Performance Drama ## 16 3 Male Paul Performance Drama ### 3.2.4 Centre-aligned headers ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-centre-aligned.png) Headers aren’t always aligned to one side of the data cells that they describe. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-centre-aligned") rectify(all_cells) ## # A tibble: 10 x 10 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` `7(G)` `8(H)` `9(I)` `10(J)` ## ## 1 2 Female Male ## 2 3 Leah Matilda Olivia Lenny Max Nicholas Paul ## 3 4 Classics 3 1 2 4 3 3 0 ## 4 5 Humanities History 8 3 4 7 5 5 1 ## 5 6 Literature 1 1 9 3 12 7 5 ## 6 7 Philosophy 5 10 10 8 2 5 12 ## 7 8 Languages 5 4 5 9 8 3 8 ## 8 9 Music 4 10 10 2 4 5 6 ## 9 10 Performance Dance 4 5 6 4 12 9 2 ## 10 11 Drama 2 7 8 6 1 12 3 Looking at that table, it’s not immediately obvious where the boundary between `Female` and `Male` falls, or between `Humanities` and `Performance`. A naive approach would be to match the inner headers to the outer ones by proximity, and there are four directions to do so: `"up-ish"`, `"left-ish"`, `"down-ish"`, and `"right-ish"`. But in this case, those directions are too naive. * `Languages` is closest to the `Performance` header, but is a humanity. * `Lenny` is the same distance from `Female` as from `Male`. You can fix this by justifying the header cells towards one side of the data cells that they describe, and then use a direction like `"up-left"` as usual. Do this with `justify()`, providing the header cells with a second set of cells at the positions you want the header cells to move to. * `header_cells` is the cells whose value will be used as the header * `corner_cells` is the cells whose position is in one corner of the domain of the header (e.g. the top-left-hand corner). In the original spreadsheet, the borders mark the boundaries. So the corner cells of the headers can be found by filtering for cells with a particular border. all_cells <- xlsx_cells(path, sheets = "pivot-centre-aligned") %>% select(row, col, is_blank, data_type, character, numeric, local_format_id) formats <- xlsx_formats(path) top_borders <- which(!is.na(formats$local$border$top$style)) left_borders <- which(!is.na(formats$local$border$left$style)) first_header_row_corners <- dplyr::filter(all_cells, row == 2, local_format_id %in% left_borders) %>% select(row, col) first_header_row_corners ## # A tibble: 2 x 2 ## row col ## ## 1 2 4 ## 2 2 7 first_header_col_corners <- dplyr::filter(all_cells, col == 2, local_format_id %in% top_borders) %>% select(row, col) first_header_col_corners ## # A tibble: 2 x 2 ## row col ## ## 1 4 2 ## 2 9 2 Next, get the first row and first column of header cells as usual. first_header_row <- dplyr::filter(all_cells, !is_blank, row == 2) %>% select(row, col, sex = character) # the title of this header is 'sex' # the cells are text cells (`"Female"` and `"Male"`) so take the value in the # '`character` column. first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 5 Female ## 2 2 9 Male first_header_col <- dplyr::filter(all_cells, !is_blank, col == 2) %>% select(row, col, field = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. first_header_col ## # A tibble: 2 x 3 ## row col field ## ## 1 5 2 Humanities ## 2 10 2 Performance And now justify the header cells to the same positions as the corner cells. first_header_row <- justify(first_header_row, first_header_row_corners) first_header_col <- justify(first_header_col, first_header_col_corners) first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 4 Female ## 2 2 7 Male first_header_col ## # A tibble: 2 x 3 ## row col field ## ## 1 4 2 Humanities ## 2 9 2 Performance The rest of this example is the same as “Two clear rows and columns of text headers, top-aligned and left-aligned”. second_header_row <- dplyr::filter(all_cells, row == 3) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. second_header_row ## # A tibble: 7 x 3 ## row col name ## ## 1 3 4 Leah ## 2 3 5 Matilda ## 3 3 6 Olivia ## 4 3 7 Lenny ## 5 3 8 Max ## 6 3 9 Nicholas ## 7 3 10 Paul second_header_col <- dplyr::filter(all_cells, col == 3) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. second_header_col ## # A tibble: 8 x 3 ## row col subject ## ## 1 4 3 Classics ## 2 5 3 History ## 3 6 3 Literature ## 4 7 3 Philosophy ## 5 8 3 Languages ## 6 9 3 Music ## 7 10 3 Dance ## 8 11 3 Drama data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is examp scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells %>% enhead(first_header_row, "up-left") %>% enhead(second_header_row, "up") %>% enhead(first_header_col, "left-up") %>% enhead(second_header_col, "left") %>% select(-row, -col) ## # A tibble: 56 x 5 ## score sex name field subject ## ## 1 3 Female Leah Humanities Classics ## 2 1 Female Matilda Humanities Classics ## 3 2 Female Olivia Humanities Classics ## 4 8 Female Leah Humanities History ## 5 3 Female Matilda Humanities History ## 6 4 Female Olivia Humanities History ## 7 1 Female Leah Humanities Literature ## 8 1 Female Matilda Humanities Literature ## 9 9 Female Olivia Humanities Literature ## 10 5 Female Leah Humanities Philosophy ## # … with 46 more rows ### 3.2.5 Multiple rows or columns of headers, with meaningful formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-annotations.png) This is a combination of the previous section with [Meaningfully formatted cells](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells) . The section [Meaningfully formatted rows](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows) doesn’t work here, because the unpivoting of multiple rows/columns of headers complicates the relationship between the data and the formatting. 1. Unpivot the multiple rows/columns of headers, as above, but keep the `row` and `col` of each data cell. 2. Collect the `row`, `col` and formatting of each data cell. 3. Join the data to the formatting by the `row` and `col`. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 28 x 5 ## row col data_type character numeric ## ## 1 2 4 character Female NA ## 2 2 6 character Male NA ## 3 3 4 character Matilda NA ## 4 3 5 character Olivia NA ## 5 3 6 character Nicholas NA ## 6 3 7 character Paul NA ## 7 4 2 character Humanities NA ## 8 4 3 character Classics NA ## 9 4 4 numeric 1 ## 10 4 5 numeric 2 ## # … with 18 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 6 x 7 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` `7(G)` ## ## 1 2 Female Male ## 2 3 Matilda Olivia Nicholas Paul ## 3 4 Humanities Classics 1 2 3 0 ## 4 5 History 3 4 5 1 ## 5 6 Performance Music 5 6 9 2 ## 6 7 Drama 7 8 12 3 first_header_row <- dplyr::filter(all_cells, row == 2) %>% select(row, col, sex = character) # the title of this header is 'sex' # the cells are text cells (`"Female"` and `"Male"`) so take the value in the # '`character` column. first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 4 Female ## 2 2 6 Male second_header_row <- dplyr::filter(all_cells, row == 3) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. second_header_row ## # A tibble: 4 x 3 ## row col name ## ## 1 3 4 Matilda ## 2 3 5 Olivia ## 3 3 6 Nicholas ## 4 3 7 Paul first_header_col <- dplyr::filter(all_cells, col == 2) %>% select(row, col, field = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. first_header_col ## # A tibble: 2 x 3 ## row col field ## ## 1 4 2 Humanities ## 2 6 2 Performance second_header_col <- dplyr::filter(all_cells, col == 3) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. second_header_col ## # A tibble: 4 x 3 ## row col subject ## ## 1 4 3 Classics ## 2 5 3 History ## 3 6 3 Music ## 4 7 3 Drama data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is exam scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` unpivoted <- data_cells %>% enhead(first_header_row, "up-left") %>% enhead(second_header_row, "up") %>% enhead(first_header_col, "left-up") %>% enhead(second_header_col, "left") # Don't delet the `row` and `col` columns yet, because we need them to join on # the formatting # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `approximate` based on the fill colours, by looking up # the local_format_id of each cell in the `formats` pallette. annotations <- xlsx_cells(path, sheets = "pivot-annotations") %>% dplyr::filter(row >= 4, col >= 4) %>% # Omit the headers mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) annotations ## # A tibble: 16 x 3 ## row col fill_colour ## ## 1 4 4 ## 2 4 5 FFFFFF00 ## 3 4 6 ## 4 4 7 ## 5 5 4 FFFFFF00 ## 6 5 5 ## 7 5 6 ## 8 5 7 ## 9 6 4 ## 10 6 5 ## 11 6 6 ## 12 6 7 ## 13 7 4 ## 14 7 5 ## 15 7 6 FFFFFF00 ## 16 7 7 left_join(unpivoted, annotations, by = c("row", "col")) %>% select(-row, -col) ## # A tibble: 16 x 6 ## score sex name field subject fill_colour ## ## 1 1 Female Matilda Humanities Classics ## 2 2 Female Olivia Humanities Classics FFFFFF00 ## 3 3 Female Matilda Humanities History FFFFFF00 ## 4 4 Female Olivia Humanities History ## 5 3 Male Nicholas Humanities Classics ## 6 0 Male Paul Humanities Classics ## 7 5 Male Nicholas Humanities History ## 8 1 Male Paul Humanities History ## 9 5 Female Matilda Performance Music ## 10 6 Female Olivia Performance Music ## 11 7 Female Matilda Performance Drama ## 12 8 Female Olivia Performance Drama ## 13 9 Male Nicholas Performance Music ## 14 2 Male Paul Performance Music ## 15 12 Male Nicholas Performance Drama FFFFFF00 ## 16 3 Male Paul Performance Drama ### 3.2.6 Mixed headers and notes in the same row/column, distinguished by formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-notes.png) This doesn’t use any new techniques. The trick is, when selecting a row or column of header cells, to filter out ones that have the ‘wrong’ formatting (formatting that shows they aren’t really headers). In this example, cells with italic or red text aren’t headers, even if they are in amongst header cells. First, identify the IDs of formats that have italic or red text. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") formats <- xlsx_formats(path) italic <- which(formats$local$font$italic) # For 'red' we can either look for the RGB code for red "FFFF0000" red <- which(formats$local$font$color$rgb == "FFFF0000") red ## [1] 12 13 14 40 41 # Or we can find out what that code is by starting from a cell that we know is # red. red_cell_format_id <- xlsx_cells(path, sheets = "pivot-notes") %>% dplyr::filter(row == 5, col == 2) %>% pull(local_format_id) red_cell_format_id ## [1] 40 red_rgb <- formats$local$font$color$rgb[red_cell_format_id] red <- which(formats$local$font$color$rgb == red_rgb) red ## [1] 12 13 14 40 41 Now we select the headers, filtering out cells with the format IDs of red or italic cells. all_cells <- xlsx_cells(path, sheets = "pivot-notes") %>% dplyr::filter(!is_blank) %>% select(row, col, character, numeric, local_format_id) %>% print() ## # A tibble: 31 x 5 ## row col character numeric local_format_id ## ## 1 2 4 Female NA 18 ## 2 2 6 Male NA 18 ## 3 2 7 0 = absent NA 39 ## 4 3 4 Matilda NA 20 ## 5 3 5 Olivia NA 21 ## 6 3 6 Nicholas NA 20 ## 7 3 7 Paul NA 21 ## 8 4 2 Humanities NA 18 ## 9 4 3 Classics NA 19 ## 10 4 4 1 33 ## # … with 21 more rows first_header_row <- dplyr::filter(all_cells, row == 2, !(local_format_id %in% c(red, italic))) %>% select(row, col, sex = character) # the title of this header is 'sex' # the cells are text cells (`"Female"` and `"Male"`) so take the value in the # '`character` column. first_header_row ## # A tibble: 2 x 3 ## row col sex ## ## 1 2 4 Female ## 2 2 6 Male first_header_col <- dplyr::filter(all_cells, col == 2, !(local_format_id %in% c(red, italic))) %>% select(row, col, qualification = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. first_header_col ## # A tibble: 2 x 3 ## row col qualification ## ## 1 4 2 Humanities ## 2 6 2 Performance second_header_col <- dplyr::filter(all_cells, col == 3) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. data_cells %>% enhead(first_header_row, "up-left") %>% enhead(first_header_col, "left-up") %>% select(-row, -col) ## # A tibble: 16 x 3 ## score sex qualification ## ## 1 1 Female Humanities ## 2 2 Female Humanities ## 3 3 Female Humanities ## 4 4 Female Humanities ## 5 3 Male Humanities ## 6 0 Male Humanities ## 7 5 Male Humanities ## 8 1 Male Humanities ## 9 5 Female Performance ## 10 6 Female Performance ## 11 7 Female Performance ## 12 8 Female Performance ## 13 9 Male Performance ## 14 2 Male Performance ## 15 12 Male Performance ## 16 3 Male Performance ### 3.2.7 Mixed levels of headers in the same row/column, distinguished by formatting ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-hierarchy.png) Normally different levels of headers are in different rows, or different columns, like [Two clear rows of text column headers, left-aligned](https://nacnudus.github.io/spreadsheet-munging-strategies/2Rl) . But sometimes they coexist in the same row or column, and are distinguishable by formatting, e.g. bold for the top level, italic for the mid level, and plain for the lowest level. In this example, there is a single column of row headers, where the levels are shown by different amounts of indentation. The indentation is done by formatting, rather than by leading spaces or tabs. The first step is to find the format IDs of all the different levels of indentation. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") formats <- xlsx_formats(path) indent0 <- which(formats$local$alignment$indent == 0) indent1 <- which(formats$local$alignment$indent == 1) indent0 ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ## [31] 31 32 33 34 35 36 37 38 39 40 41 42 43 45 47 48 49 50 51 52 53 54 55 56 57 58 59 indent1 ## [1] 44 46 Now we use these format IDs to indentify the different levels of headers in the first column. all_cells <- xlsx_cells(path, sheets = "pivot-hierarchy") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric, local_format_id) %>% print() ## # A tibble: 16 x 6 ## row col data_type character numeric local_format_id ## ## 1 2 3 character Matilda NA 18 ## 2 2 4 character Nicholas NA 42 ## 3 3 2 character Humanities NA 18 ## 4 4 2 character Classics NA 44 ## 5 4 3 numeric 1 20 ## 6 4 4 numeric 3 45 ## 7 5 2 character History NA 44 ## 8 5 3 numeric 3 20 ## 9 5 4 numeric 5 45 ## 10 6 2 character Performance NA 20 ## 11 7 2 character Music NA 44 ## 12 7 3 numeric 5 20 ## 13 7 4 numeric 9 45 ## 14 8 2 character Drama NA 46 ## 15 8 3 numeric 7 24 ## 16 8 4 numeric 12 47 field <- dplyr::filter(all_cells, col == 2, local_format_id %in% indent0) %>% select(row, col, field = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. field ## # A tibble: 2 x 3 ## row col field ## ## 1 3 2 Humanities ## 2 6 2 Performance subject <- dplyr::filter(all_cells, col == 2, local_format_id %in% indent1) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. subject ## # A tibble: 4 x 3 ## row col subject ## ## 1 4 2 Classics ## 2 5 2 History ## 3 7 2 Music ## 4 8 2 Drama name <- dplyr::filter(all_cells, row == 2) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. name ## # A tibble: 2 x 3 ## row col name ## ## 1 2 3 Matilda ## 2 2 4 Nicholas data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is exam scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells %>% enhead(field, "left-up") %>% enhead(subject, "left") %>% enhead(name, "up") %>% select(-row, -col) ## # A tibble: 8 x 4 ## score field subject name ## ## 1 1 Humanities Classics Matilda ## 2 3 Humanities Classics Nicholas ## 3 3 Humanities History Matilda ## 4 5 Humanities History Nicholas ## 5 5 Performance Music Matilda ## 6 9 Performance Music Nicholas ## 7 7 Performance Drama Matilda ## 8 12 Performance Drama Nicholas ### 3.2.8 Repeated rows/columns of headers within the table ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-repeated-headers.png) Repetitions can simply be ignored. Select one of the sets of headers, and use it for all the data. In this example, the data cells are easy to distinguish from the headers mixed in among them, because only the data cells have the `numeric` data type. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-repeated-headers") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric) %>% print() ## # A tibble: 80 x 5 ## row col data_type character numeric ## ## 1 2 4 character Term 1 NA ## 2 2 5 character Term 2 NA ## 3 2 6 character Term 3 NA ## 4 3 2 character Classics NA ## 5 3 3 character Matilda NA ## 6 3 4 numeric 1 ## 7 3 5 numeric 8 ## 8 3 6 numeric 7 ## 9 4 3 character Nicholas NA ## 10 4 4 numeric 3 ## # … with 70 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 20 x 6 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` `6(F)` ## ## 1 2 Term 1 Term 2 Term 3 ## 2 3 Classics Matilda 1 8 7 ## 3 4 Nicholas 3 1 2 ## 4 5 Olivia 4 0 1 ## 5 6 Paul 2 4 8 ## 6 7 Term 1 Term 2 Term 3 ## 7 8 History Matilda 4 7 3 ## 8 9 Nicholas 3 5 5 ## 9 10 Olivia 9 8 5 ## 10 11 Paul 6 2 0 ## 11 12 Term 1 Term 2 Term 3 ## 12 13 Music Matilda 2 9 9 ## 13 14 Nicholas 1 7 7 ## 14 15 Olivia 0 3 5 ## 15 16 Paul 2 2 3 ## 16 17 Term 1 Term 2 Term 3 ## 17 18 Drama Matilda 9 8 9 ## 18 19 Nicholas 1 3 4 ## 19 20 Olivia 6 1 4 ## 20 21 Paul 6 0 2 # The 'term' headers appear four times, but only the first one is needed. term <- dplyr::filter(all_cells, row == 2) %>% select(row, col, term = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. term ## # A tibble: 3 x 3 ## row col term ## ## 1 2 4 Term 1 ## 2 2 5 Term 2 ## 3 2 6 Term 3 subject <- dplyr::filter(all_cells, col == 2) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. subject ## # A tibble: 4 x 3 ## row col subject ## ## 1 3 2 Classics ## 2 8 2 History ## 3 13 2 Music ## 4 18 2 Drama name <- dplyr::filter(all_cells, col == 3) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. name ## # A tibble: 16 x 3 ## row col name ## ## 1 3 3 Matilda ## 2 4 3 Nicholas ## 3 5 3 Olivia ## 4 6 3 Paul ## 5 8 3 Matilda ## 6 9 3 Nicholas ## 7 10 3 Olivia ## 8 11 3 Paul ## 9 13 3 Matilda ## 10 14 3 Nicholas ## 11 15 3 Olivia ## 12 16 3 Paul ## 13 18 3 Matilda ## 14 19 3 Nicholas ## 15 20 3 Olivia ## 16 21 3 Paul # The data cells are distinguished from the 'term' headers by their data type -- # the data cells are numeric, whereas the term headers are character. data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is exam scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells ## # A tibble: 48 x 3 ## row col score ## ## 1 3 4 1 ## 2 3 5 8 ## 3 3 6 7 ## 4 4 4 3 ## 5 4 5 1 ## 6 4 6 2 ## 7 5 4 4 ## 8 5 5 0 ## 9 5 6 1 ## 10 6 4 2 ## # … with 38 more rows data_cells %>% enhead(term, "up") %>% enhead(subject, "up-left") %>% enhead(name, "left") %>% select(-row, -col) ## # A tibble: 48 x 4 ## score term subject name ## ## 1 1 Term 1 Classics Matilda ## 2 8 Term 2 Classics Matilda ## 3 7 Term 3 Classics Matilda ## 4 3 Term 1 Classics Nicholas ## 5 1 Term 2 Classics Nicholas ## 6 2 Term 3 Classics Nicholas ## 7 4 Term 1 Classics Olivia ## 8 0 Term 2 Classics Olivia ## 9 1 Term 3 Classics Olivia ## 10 2 Term 1 Classics Paul ## # … with 38 more rows ### 3.2.9 Headers amongst the data ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/pivot-header-within-data.png) This happens when what is actually a row-header, instead of being presented to the left of the data, is presented above the data. (Alternatively, what is actually a column header, instead of being presented above the data, is presented to the side.) The way to handle it is to _pretend_ that it is a row header, and use the `"left-up"` direction as normal. path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") all_cells <- xlsx_cells(path, sheets = "pivot-header-within-data") %>% dplyr::filter(!is_blank) %>% select(row, col, data_type, character, numeric, local_format_id) %>% print() ## # A tibble: 80 x 6 ## row col data_type character numeric local_format_id ## ## 1 2 3 character Classics NA 2 ## 2 3 3 character Term 1 NA 20 ## 3 3 4 character Term 2 NA 37 ## 4 3 5 character Term 3 NA 21 ## 5 4 2 character Matilda NA 18 ## 6 4 3 numeric 4 18 ## 7 4 4 numeric 0 27 ## 8 4 5 numeric 7 19 ## 9 5 2 character Nicholas NA 20 ## 10 5 3 numeric 4 20 ## # … with 70 more rows # View the cells in their original positions on the spreadsheet rectify(all_cells) ## # A tibble: 24 x 5 ## `row/col` `2(B)` `3(C)` `4(D)` `5(E)` ## ## 1 2 Classics ## 2 3 Term 1 Term 2 Term 3 ## 3 4 Matilda 4 0 7 ## 4 5 Nicholas 4 6 2 ## 5 6 Olivia 9 9 9 ## 6 7 Paul 5 0 0 ## 7 8 History ## 8 9 Term 1 Term 2 Term 3 ## 9 10 Matilda 0 4 2 ## 10 11 Nicholas 2 5 2 ## # … with 14 more rows bold <- which(xlsx_formats(path)$local$font$bold) # The subject headers, though mixed with the data and the 'term' headers, are # distinguishable by the data type "character" and by being bold. subject <- dplyr::filter(all_cells, col == 3, data_type == "character", local_format_id %in% bold) %>% select(row, col, subject = character) # The title of this header is 'subject' # The cells are text cells (`"history"`, etc.) so take the value in the # '`character` column. subject ## # A tibble: 4 x 3 ## row col subject ## ## 1 2 3 Classics ## 2 8 3 History ## 3 14 3 Music ## 4 20 3 Drama # We only need one set of the 'term' headers term <- dplyr::filter(all_cells, row == 3, data_type == "character") %>% select(row, col, term = character) # the title of this header is 'field', meaning 'group of subjects'. # The cells are text cells (`"Humanities"`, `"Performance"`) so take the value # in the '`character` column. term ## # A tibble: 3 x 3 ## row col term ## ## 1 3 3 Term 1 ## 2 3 4 Term 2 ## 3 3 5 Term 3 name <- dplyr::filter(all_cells, col == 2) %>% select(row, col, name = character) # The title of this header is 'name'. # The cells are text cells, so take the value in the '`character` column. name ## # A tibble: 16 x 3 ## row col name ## ## 1 4 2 Matilda ## 2 5 2 Nicholas ## 3 6 2 Olivia ## 4 7 2 Paul ## 5 10 2 Matilda ## 6 11 2 Nicholas ## 7 12 2 Olivia ## 8 13 2 Paul ## 9 16 2 Matilda ## 10 17 2 Nicholas ## 11 18 2 Olivia ## 12 19 2 Paul ## 13 22 2 Matilda ## 14 23 2 Nicholas ## 15 24 2 Olivia ## 16 25 2 Paul # The data cells are distinguished from the 'subject' headers by their data # type -- the data cells are numeric, whereas the term headers are character. data_cells <- dplyr::filter(all_cells, data_type == "numeric") %>% select(row, col, score = numeric) # The data is exam scores in certain subjects, so give the data that title. # The data is numeric, so select only that 'value'. If some of the data was # also text or true/false, then you would select the `character` and `logical` # columns as well as `numeric` data_cells ## # A tibble: 48 x 3 ## row col score ## ## 1 4 3 4 ## 2 4 4 0 ## 3 4 5 7 ## 4 5 3 4 ## 5 5 4 6 ## 6 5 5 2 ## 7 6 3 9 ## 8 6 4 9 ## 9 6 5 9 ## 10 7 3 5 ## # … with 38 more rows data_cells %>% enhead(subject, "left-up") %>% enhead(term, "up") %>% enhead(name, "left") %>% select(-row, -col) ## # A tibble: 48 x 4 ## score subject term name ## ## 1 4 Classics Term 1 Matilda ## 2 0 Classics Term 2 Matilda ## 3 7 Classics Term 3 Matilda ## 4 4 Classics Term 1 Nicholas ## 5 6 Classics Term 2 Nicholas ## 6 2 Classics Term 3 Nicholas ## 7 9 Classics Term 1 Olivia ## 8 9 Classics Term 2 Olivia ## 9 9 Classics Term 3 Olivia ## 10 5 Classics Term 1 Paul ## # … with 38 more rows --- # Read Untidy Excel Files • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl#main) tidyxl[](https://nacnudus.github.io/tidyxl#tidyxl) =================================================== [tidyxl](https://github.com/nacnudus/tidyxl) imports non-tabular data from Excel files into R. It exposes cell content, position, formatting and comments in a tidy structure for further manipulation, especially by the [unpivotr](https://github.com/nacnudus/unpivotr) package. It supports the xml-based file formats ‘.xlsx’ and ‘.xlsm’ via the embedded [RapidXML](https://rapidxml.sourceforge.net/) C++ library. It does not support the binary file formats ‘.xlsb’ or ‘.xls’. It also provides a function `[xlex()](https://nacnudus.github.io/reference/xlex.html) ` for tokenizing formulas. See the [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html) for details. It is useful for detecting ‘spreadsheet smells’ (poor practice such as embedding constants in formulas, or using deep levels of nesting), and for understanding the dependency structures within spreadsheets. Make cells tidy[](https://nacnudus.github.io/tidyxl#make-cells-tidy) --------------------------------------------------------------------- Tidyxl represents each cell as one row in a dataframe. Like this. ![Gif of tidyxl converting cells into a tidy representation of one row per cell](https://nacnudus.github.io/reference/figures/tidy_xlsx.gif) What can you do with tidy cells? The best places to start are: * [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) , a free, online cookbook using [tidyxl](https://github.com/nacnudus/tidyxl) and [unpivotr](https://github.com/nacnudus/unpivotr) * [Screencasts](https://www.youtube.com/watch?v=1sinC7wsS5U) on YouTube. * [Worked examples](https://github.com/nacnudus/ukfarm) on GitHub. Otherwise see examples below. **_Breaking changes_** The version 1.0.0 release has some breaking changes. See `NEWS.md` for details. The previous version can be installed as follows. devtools::install_version("tidyxl", version = "0.2.3", repos = "http://cran.us.r-project.org") Installation[](https://nacnudus.github.io/tidyxl#installation) --------------------------------------------------------------- devtools::install_github("nacnudus/tidyxl") Examples[](https://nacnudus.github.io/tidyxl#examples) ------------------------------------------------------- The package includes a spreadsheet, ‘titanic.xlsx’, which contains the following pivot table: ftable(Titanic, row.vars = 1:2) #> Age Child Adult #> Survived No Yes No Yes #> Class Sex #> 1st Male 0 5 118 57 #> Female 0 1 4 140 #> 2nd Male 0 11 154 14 #> Female 0 13 13 80 #> 3rd Male 35 13 387 75 #> Female 17 14 89 76 #> Crew Male 0 0 670 192 #> Female 0 0 3 20 The multi-row column headers make this difficult to import. A popular package for importing spreadsheets coerces the pivot table into a dataframe. It treats the second header row as though it were observations. titanic <- system.file("extdata/titanic.xlsx", package = "tidyxl") readxl::read_excel(titanic) #> New names: #> • `` -> `...1` #> • `` -> `...2` #> • `` -> `...5` #> • `` -> `...7` #> # A tibble: 10 × 7 #> ...1 ...2 Age Child ...5 Adult ...7 #> #> 1 Survived No Yes No Yes #> 2 Class Sex #> 3 1st Male 0 5 118 57 #> 4 Female 0 1 4 140 #> 5 2nd Male 0 11 154 14 #> 6 Female 0 13 13 80 #> 7 3rd Male 35 13 387 75 #> 8 Female 17 14 89 76 #> 9 Crew Male 0 0 670 192 #> 10 Female 0 0 3 20 [tidyxl](https://github.com/nacnudus/tidyxl) doesn’t coerce the pivot table into a data frame. Instead, it represents each cell in its own row, where it describes the cell’s address, value and other properties. library(tidyxl) x <- xlsx_cells(titanic) dplyr::glimpse(x) #> Rows: 60 #> Columns: 24 #> $ sheet "Sheet1", "Sheet1", "Sheet1", "Sheet1", "Sheet1", … #> $ address "C1", "D1", "E1", "F1", "G1", "C2", "D2", "E2", "F… #> $ row 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4,… #> $ col 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 1, 2, 1, 2, 4, 5, 6,… #> $ is_blank FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FAL… #> $ content "0", "1", NA, "2", NA, "3", "4", "5", "4", "5", "6… #> $ data_type "character", "character", "blank", "character", "b… #> $ error NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ logical NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ numeric NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ date NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… #> $ character "Age", "Child", NA, "Adult", NA, "Survived", "No",… #> $ character_formatted [], [], , […\ #> $ formula NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ is_array FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…\ #> $ formula_ref NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ formula_group NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ comment NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ height 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15…\ #> $ width 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.…\ #> $ row_outline_level 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…\ #> $ col_outline_level 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…\ #> $ style_format "Normal", "Normal", "Normal", "Normal", "Normal", …\ #> $ local_format_id 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 2, 3, 3, 1, 1, 1,…\ \ In this structure, the cells can be found by filtering.\ \ x[x$data_type == "character", c("address", "character")]\ #> # A tibble: 22 × 2\ #> address character\ #> \ #> 1 C1 Age\ #> 2 D1 Child\ #> 3 F1 Adult\ #> 4 C2 Survived\ #> 5 D2 No\ #> 6 E2 Yes\ #> 7 F2 No\ #> 8 G2 Yes\ #> 9 A3 Class\ #> 10 B3 Sex\ #> # … with 12 more rows\ x[x$row == 4, c("address", "character", "numeric")]\ #> # A tibble: 6 × 3\ #> address character numeric\ #> \ #> 1 A4 1st NA\ #> 2 B4 Male NA\ #> 3 D4 0\ #> 4 E4 5\ #> 5 F4 118\ #> 6 G4 57\ \ Specific sheets can be requested using `xlsx_cells(file, sheet)`, and the names of all sheets in a file are given by `[xlsx_sheet_names()](https://nacnudus.github.io/reference/xlsx_sheet_names.html) `.\ \ ### Formatting[](https://nacnudus.github.io/tidyxl#formatting)\ \ The original spreadsheet has formatting applied to the cells. This can also be retrieved using [tidyxl](https://github.com/nacnudus/tidyxl)\ , with the `[xlsx_formats()](https://nacnudus.github.io/reference/xlsx_formats.html) ` function.\ \ ![Screenshot of a spreadsheet with meaningful formatting](https://nacnudus.github.io/articles/titanic-screenshot.png)\ \ Formatting is available by using the columns `local_format_id` and `style_format` as indexes into a separate list-of-lists structure. ‘Local’ formatting is the most common kind, applied to individual cells. ‘Style’ formatting is usually applied to blocks of cells, and defines several formats at once. Here is a screenshot of the styles buttons in Excel.\ \ ![Screenshot of a formatting options available in Excel](https://nacnudus.github.io/articles/styles-screenshot.png)\ \ Formatting can be looked up as follows.\ \ # Bold\ formats <- xlsx_formats(titanic)\ formats$local$font$bold\ #> [1] FALSE TRUE FALSE FALSE\ x[x$local_format_id %in% which(formats$local$font$bold),\ c("address", "character")]\ #> # A tibble: 4 × 2\ #> address character\ #> \ #> 1 C1 Age\ #> 2 C2 Survived\ #> 3 A3 Class\ #> 4 B3 Sex\ \ # Yellow fill\ formats$local$fill$patternFill$fgColor$rgb\ #> [1] NA NA NA "FFFFFF00"\ x[x$local_format_id %in%\ which(formats$local$fill$patternFill$fgColor$rgb == "FFFFFF00"),\ c("address", "numeric")]\ #> # A tibble: 2 × 2\ #> address numeric\ #> \ #> 1 F11 3\ #> 2 G11 20\ \ # Styles by name\ formats$style$font$name["Normal"]\ #> Normal\ #> "Calibri"\ head(x[x$style_format == "Normal", c("address", "character")])\ #> # A tibble: 6 × 2\ #> address character\ #> \ #> 1 C1 Age\ #> 2 D1 Child\ #> 3 E1 \ #> 4 F1 Adult\ #> 5 G1 \ #> 6 C2 Survived\ \ # In-cell formatting is available in the `character_formatted` column as a data\ # frame, one row per substring.\ examples <- system.file("/extdata/examples.xlsx", package = "tidyxl")\ xlsx_cells(examples)$character_formatted[77]\ #> [[1]]\ #> # A tibble: 16 × 14\ #> character bold italic under…¹ strike vertA…² size color…³ color…⁴ color…⁵\ #> \ #> 1 "in-cell" FALSE FALSE FALSE 0 NA NA\ #> 2 "bold" TRUE FALSE FALSE 0 FF0000… 2 NA\ #> 3 "italic" FALSE TRUE FALSE 0 FF0000… 2 NA\ #> 4 "underline" FALSE FALSE single FALSE 0 FF0000… 2 NA\ #> 5 "underline… FALSE FALSE double FALSE 0 FF0000… 2 NA\ #> 6 "singleacc… FALSE FALSE single… FALSE 0 FF0000… 2 NA\ #> 7 "doubleacc… FALSE FALSE double… FALSE 0 FF0000… 2 NA\ #> 8 "strikethr… FALSE FALSE TRUE 0 FF0000… 2 NA\ #> 9 "subscript" FALSE FALSE FALSE subscr… 0 FF0000… 2 NA\ #> 10 "superscri… FALSE FALSE FALSE supers… 0 FF0000… 2 NA\ #> 11 "red" FALSE FALSE FALSE 0 FFFF00… NA NA\ #> 12 "theme" FALSE FALSE FALSE 0 FFC050… 6 NA\ #> 13 "tint" FALSE FALSE FALSE 0 FFC050… 6 NA\ #> 14 "size" FALSE FALSE FALSE 0 FFFF00… NA NA\ #> 15 "arial" FALSE FALSE FALSE 0 FFFF00… NA NA\ #> 16 "UTF8Stéph… FALSE FALSE FALSE 0 FFFF00… NA NA\ #> # … with 4 more variables: color_tint , font , family ,\ #> # scheme , and abbreviated variable names ¹​underline, ²​vertAlign,\ #> # ³​color_rgb, ⁴​color_theme, ⁵​color_indexed\ \ To see all the available kinds of formats, use `str(formats)`.\ \ ### Comments[](https://nacnudus.github.io/tidyxl#comments)\ \ Comments are available alongside cell values.\ \ x[!is.na(x$comment), c("address", "comment")]\ #> # A tibble: 1 × 2\ #> address comment\ #> \ #> 1 G11 All women in the crew worked in the victualling department.\ \ ### Formulas[](https://nacnudus.github.io/tidyxl#formulas)\ \ Formulas are available, but with a few quirks.\ \ options(width = 120)\ y <- xlsx_cells(examples, "Sheet1")\ y[!is.na(y$formula),\ c("address", "formula", "is_array", "formula_ref", "formula_group",\ "error", "logical", "numeric", "date", "character")]\ #> # A tibble: 32 × 10\ #> address formula is_array formula_ref formula_group error logical numeric date character\ #> \ #> 1 A1 "1/0" FALSE NA #DIV/0! NA NA NA \ #> 2 A14 "1=1" FALSE NA TRUE NA NA \ #> 3 A15 "A4+1" FALSE NA NA 1338 NA \ #> 4 A16 "DATE(2017,1,18)" FALSE NA NA NA 2017-01-18 00:00:00 \ #> 5 A17 "\"Hello, World!\"" FALSE NA NA NA NA Hello, Wo…\ #> 6 A19 "$A$18+1" FALSE NA NA 2 NA \ #> 7 B19 "A18+2" FALSE NA NA 3 NA \ #> 8 A20 "$A$18+1" FALSE A20:A21 0 NA 2 NA \ #> 9 B20 "A19+2" FALSE B20:B21 1 NA 4 NA \ #> 10 A21 "$A$18+1" FALSE 0 NA 2 NA \ #> # … with 22 more rows\ \ The top five cells show that the results of formulas are available as usual in the columns `error`, `logical`, `numeric`, `date`, and `character`.\ \ #### Shared formulas[](https://nacnudus.github.io/tidyxl#shared-formulas)\ \ Cells `A20` and `A21` illustrate how formulas are normalised before being written to file, using the `formula_ref` and `formula_group` columns. When there is a group of cells whose formulas only differ by cell reference (e.g. “=A1+1”, “=A2+1”, “=A3+1”, etc.), only one formula in each group is written to the file, so `tidyxl` infers what the formulas in the other cells in the group must be, from their relative positions.\ \ #### Array formulas[](https://nacnudus.github.io/tidyxl#array-formulas)\ \ There are two kinds of array formulas: ones that compute over arrays, and ones whose output is an array (of cells).\ \ Both kinds are distinguished in spreadsheet programs by curly braces, e.g. `{SUM(A19:A21*B19:B21)}`. In `tidyxl`, the curly braces are ommitted (as they are from the file itself), and instead the `is_array` column has the value `TRUE`.\ \ The first kind (those that compute over arrays) is illustrated by cell `A22`.\ \ The second kind (those whose value is spread across an array of cells) is illustrated by cells `A23` and `A24`. The formula is only given in the top-left cell (`A23`), which is also the only cell that describes the range of cells containing the result, in the `formula-ref` column. The results themselves are stored in all relevant cells (`A23` and `A24`). Unlike shared formulas, there is no `formula_group` to associate the cells of an array formula’s result. If you need to do identify those cells, use the [cellranger](https://github.com/rsheets/cellranger)\ package and the `formula_ref` column.\ \ #### Formulas referring to other files[](https://nacnudus.github.io/tidyxl#formulas-referring-to-other-files)\ \ Cell `A25` contains a formula that refers to another file. The `[1]` is an index into a table of files. The roadmap for [tidyxl](https://github.com/nacnudus/tidyxl)\ includes de-referencing such numbers.\ \ #### Tokenizing formulas[](https://nacnudus.github.io/tidyxl#tokenizing-formulas)\ \ The function `[xlex()](https://nacnudus.github.io/reference/xlex.html) ` separates formulas into tokens of different types, and gives their depth within a nested formula. Its name is a bad pun on ‘Excel’ and ‘lexer’. Try the [online demo](https://duncan-garmonsway.shinyapps.io/xlex/)\ , or install the more experimental [lexl](https://nacnudus.github.io/lexl/)\ package to run `demo_lexl()` locally.\ \ It is useful for detecting spreadsheet smells, which are poor practices in spreadsheet design, such as deep nests of functions, or embedding constants in formulas.\ \ x <- xlex("MIN(3,MAX(2,A1))")\ x\ #> root\ #> ¦-- MIN function\ #> °-- ( fun_open\ #> ¦-- 3 number\ #> ¦-- , separator\ #> ¦-- MAX function\ #> °-- ( fun_open\ #> ¦-- 2 number\ #> ¦-- , separator\ #> °-- A1 ref\ #> °-- ) fun_close\ #> °-- ) fun_close\ \ See the [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html)\ for more examples and details.\ \ ### Named ranges[](https://nacnudus.github.io/tidyxl#named-ranges)\ \ Names are imported with `[xlsx_names()](https://nacnudus.github.io/reference/xlsx_names.html) `. Also called ‘named formulas’ and ‘defined names’, these are usually used to name particular cells or ranges, making formulas that refer to them more readable. Ones that _are_ ranges are identifed by the `is_range` column (using `[is_range()](https://nacnudus.github.io/reference/is_range.html) `), making it easier to match the names to the cells returned by `[xlsx_cells()](https://nacnudus.github.io/reference/xlsx_cells.html) ` – e.g. by using the [`cellranger`](https://github.com/rsheets/cellranger)\ package.\ \ When the scope of the name is within a particular sheet, rather than global, the sheet name is given.\ \ xlsx_names(examples)\ #> rId sheet name formula comment hidden is_range\ #> 1 1 Sheet1 named_local_formula MAX(Sheet1!$A$129:$A$130)+1 FALSE FALSE\ #> 2 4 E09904.2 sheet_beyond_chart E09904.2!$A$1,E09904.2!$C$1 FALSE TRUE\ #> 3 NA intersection Sheet1!$B:$B Sheet1!$8:$8 FALSE TRUE\ #> 4 NA named_global_formula Sheet1!$A$129-1 FALSE FALSE\ #> 5 NA named_range Sheet1!$A$129 My comment FALSE TRUE\ \ ### Data validation rules[](https://nacnudus.github.io/tidyxl#data-validation-rules)\ \ Data validation rules are imported with `[xlsx_validation()](https://nacnudus.github.io/reference/xlsx_validation.html) `. These rules control what values may be entered into a cell, and are often used to create a drop-down list in a cell. Read the [vignette](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html)\ for details.\ \ xlsx_validation(examples)\ #> # A tibble: 15 × 14\ #> sheet ref type opera…¹ formu…² formu…³ allow…⁴ show_…⁵ promp…⁶ promp…⁷ show_…⁸ error…⁹ error…˟ error…˟\ #> \ #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE messag… messag… TRUE error … error … stop\ #> 2 Sheet1 A108 list $B$108 TRUE TRUE TRUE warning\ #> 3 Sheet1 A110 date between 2017-0… 2017-0… TRUE TRUE TRUE stop\ #> 4 Sheet1 A111 time between 00:00:… 09:00:… TRUE TRUE TRUE stop\ #> 5 Sheet1 A112 textLe… between 0 9 TRUE TRUE TRUE stop\ #> 6 Sheet1 A114 whole notBet… 0 9 TRUE TRUE TRUE stop\ #> 7 Sheet1 A115,A121:A122 whole equal 0 TRUE TRUE TRUE stop\ #> 8 Sheet1 A116 whole notEqu… 0 TRUE TRUE TRUE stop\ #> 9 Sheet1 A117 whole greate… 0 TRUE TRUE TRUE stop\ #> 10 Sheet1 A119 whole greate… 0 TRUE TRUE TRUE stop\ #> 11 Sheet1 A120 whole lessTh… 0 TRUE TRUE TRUE stop\ #> 12 Sheet1 A118 whole lessTh… 0 TRUE TRUE TRUE stop\ #> 13 Sheet1 A107 decimal notBet… 0 9 FALSE FALSE FALSE stop\ #> 14 Sheet1 A113 custom A113<=… TRUE TRUE TRUE stop\ #> 15 Sheet1 A109 list $B$108 TRUE TRUE TRUE inform…\ #> # … with abbreviated variable names ¹​operator, ²​formula1, ³​formula2, ⁴​allow_blank, ⁵​show_input_message, ⁶​prompt_title,\ #> # ⁷​prompt_body, ⁸​show_error_message, ⁹​error_title, ˟​error_body, ˟​error_symbol\ \ Philosophy[](https://nacnudus.github.io/tidyxl#philosophy)\ \ -----------------------------------------------------------\ \ Information in in many spreadsheets cannot be easily imported into R. Why?\ \ Most R packages that import spreadsheets have difficulty unless the layout of the spreadsheet conforms to a strict definition of a ‘table’, e.g.:\ \ * observations in rows\ * variables in columns\ * a single header row\ * all information represented by characters, whether textual, logical, or numeric\ \ These rules are designed to eliminate ambiguity in the interpretation of the information. But most spreadsheeting software relaxes these rules in a trade of ambiguity for expression via other media:\ \ * proximity (other than headers, i.e. other than being the first value at the top of a column)\ * formatting (colours and borders)\ \ Humans can usually resolve the ambiguities with contextual knowledge, but computers are limited by their ignorance. Programmers are hampered by:\ \ * their language’s expressiveness\ * loss of information in transfer from spreadsheet to programming library\ \ Information is lost when software discards it in order to force the data into tabular form. Sometimes date formatting is retained, but mostly formatting is lost, and position has to be inferred again.\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ addresses the programmer’s problems by not discarding information. It imports the content, position and formatting of cells, leaving it up to the user to associate the different forms of information, and to re-encode them in tabular form without loss. The [unpivotr](https://github.com/nacnudus/unpivotr)\ package has been developed to assist with that step.\ \ Similar projects[](https://nacnudus.github.io/tidyxl#similar-projects)\ \ -----------------------------------------------------------------------\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ was originally derived from [readxl](https://github.com/tidyverse/readxl)\ and still contains some of the same code, hence it inherits the GPL-3 licence. [readxl](https://github.com/tidyverse/readxl)\ is intended for importing tabular data with a single row of column headers, whereas [tidyxl](https://github.com/nacnudus/tidyxl)\ is more general, and less magic.\ \ The [rsheets](https://github.com/rsheets)\ project of several R packages is in the early stages of importing spreadsheet information from Excel and Google Sheets into R, manipulating it, and potentially parsing and processing formulas and writing out to spreadsheet files. In particular, [jailbreaker](https://github.com/rsheets/jailbreakr)\ attempts to extract non-tabular data from spreadsheets into tabular structures automatically via some clever algorithms.\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ differs from [rsheets](https://github.com/rsheets)\ in scope ([tidyxl](https://github.com/nacnudus/tidyxl)\ will never import charts, for example), and implementation ([tidyxl](https://github.com/nacnudus/tidyxl)\ is implemented mainly in C++ and is quite fast, only a little slower than [readxl](https://github.com/tidyverse/readxl)\ ). [unpivotr](https://github.com/nacnudus/unpivotr)\ is a package related to [tidyxl](https://github.com/nacnudus/tidyxl)\ that provides tools for unpivoting complex and non-tabular data layouts using I not AI (intelligence, not artificial intelligence). In this way it corresponds to [jailbreaker](https://github.com/rsheets/jailbreakr)\ , but with a different philosophy.\ \ Mailing list[](https://nacnudus.github.io/tidyxl#mailing-list)\ \ ---------------------------------------------------------------\ \ For bugs and/or issues, create a new issue on [GitHub](https://github.com/nacnudus/tidyxl/issues)\ For other questions or comments, please subscribe to the [tidyxl-devel mailing list](https://groups.google.com/forum/#!forum/tidyxl-devel)\ . You must be a member to post messages, but anyone can read the archived discussions. --- # 2.4 Meaningfully formatted cells | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.4 Meaningfully formatted cells -------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/annotations.png) If single cells are highlighted, rather than whole rows, then the highlights probably indicate something about the column rather than the row. For example, a highlighted cell in a column called “age” of a table of medical patients, might mean “the age of this patient is uncertain”. One way to deal with this is to create a new column in the final table for each column in the original that has any highlighted cells. For example, if highlighted cells mean “this value is uncertain”, and some cells in the `age` and `height` columns are highlighted, then you could create two new columns: `uncertain_age`, and `uncertain_height`, by following the procedure of [meaningfully formatted rows](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows) for each column `age` and `height`. # Step 1: import the table taking only cell values and ignoring the formatting path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "annotations") # Step 2: import one column of the table, taking only the formatting and not the # cell values # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create new columns `something_fill` of the fill colours, by looking up the # local_format_id of each cell in the `formats` pallette. fills <- xlsx_cells(path, sheet = "annotations") %>% dplyr::filter(row >= 2, col >= 2) %>% # Omit the header row and name column mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) %>% spread(col, fill_colour) %>% select(-row) %>% set_names(paste0(colnames(x)[-1], "_fill")) fills ## # A tibble: 3 x 2 ## Age_fill Height_fill ## ## 1 ## 2 FFFFFF00 ## 3 FF92D050 # Step 3: append the `fill` column to the rest of the data bind_cols(x, fills) ## # A tibble: 3 x 5 ## Name Age Height Age_fill Height_fill ## ## 1 Matilda 1 2 ## 2 Nicholas 3 4 FFFFFF00 ## 3 Olivia 5 6 FF92D050 Here’s the same thing, but using only tidyxl and unpivotr fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb cells <- xlsx_cells(path, sheet = "annotations") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) cells ## # A tibble: 12 x 6 ## row col data_type character numeric fill_colour ## ## 1 1 1 character Name NA ## 2 1 2 character Age NA ## 3 1 3 character Height NA ## 4 2 1 character Matilda NA ## 5 2 2 numeric 1 ## 6 2 3 numeric 2 ## 7 3 1 character Nicholas NA ## 8 3 2 numeric 3 FFFFFF00 ## 9 3 3 numeric 4 ## 10 4 1 character Olivia NA ## 11 4 2 numeric 5 ## 12 4 3 numeric 6 FF92D050 values <- cells %>% select(-fill_colour) %>% behead("up", header) %>% select(-col) %>% spatter(header) values ## # A tibble: 3 x 4 ## row Age Height Name ## ## 1 2 1 2 Matilda ## 2 3 3 4 Nicholas ## 3 4 5 6 Olivia fills <- cells %>% behead("up", header) %>% mutate(header = paste0(header, "_fill")) %>% select(row, header, fill_colour) %>% spread(header, fill_colour) fills ## # A tibble: 3 x 4 ## row Age_fill Height_fill Name_fill ## ## 1 2 ## 2 3 FFFFFF00 ## 3 4 FF92D050 left_join(values, fills, by = "row") %>% select(-row) ## # A tibble: 3 x 6 ## Age Height Name Age_fill Height_fill Name_fill ## ## 1 1 2 Matilda ## 2 3 4 Nicholas FFFFFF00 ## 3 5 6 Olivia FF92D050 Another way would be to make the table what I call “extra-tidy”. If it is tidy, then each row is an observation, and each column is a variable. To make it “extra-tidy”, you `gather()` the variables so that each row is _one observation of one variable_. This works best when every variable has the same data type, otherwise the values will be coerced, probably to a character. # Tidy (x <- read_excel(path, sheet = "annotations")) ## # A tibble: 3 x 3 ## Name Age Height ## ## 1 Matilda 1 2 ## 2 Nicholas 3 4 ## 3 Olivia 5 6 # Extra-tidy extra_tidy <- x %>% gather(variable, value, -Name) %>% arrange(Name, variable) extra_tidy ## # A tibble: 6 x 3 ## Name variable value ## ## 1 Matilda Age 1 ## 2 Matilda Height 2 ## 3 Nicholas Age 3 ## 4 Nicholas Height 4 ## 5 Olivia Age 5 ## 6 Olivia Height 6 With an extra-tidy dataset, the formatting can now be appended to the values of individual variables, rather than to whole observations. # Extra-tidy, with row and column numbers of the original variables extra_tidy <- read_excel(path, sheet = "annotations") %>% mutate(row = row_number() + 1L) %>% gather(variable, value, -row, -Name) %>% group_by(row) %>% mutate(col = row_number() + 1L) %>% ungroup() %>% select(row, col, Name, variable, value) %>% arrange(row, col) extra_tidy ## # A tibble: 6 x 5 ## row col Name variable value ## ## 1 2 2 Matilda Age 1 ## 2 2 3 Matilda Height 2 ## 3 3 2 Nicholas Age 3 ## 4 3 3 Nicholas Height 4 ## 5 4 2 Olivia Age 5 ## 6 4 3 Olivia Height 6 # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `uncertain` based on the fill colours, by looking up # the local_format_id of each cell in the `formats` pallette. fills <- xlsx_cells(path, sheet = "annotations") %>% dplyr::filter(row >= 2, col >= 2) %>% # Omit the header row and name column mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, fill_colour) fills ## # A tibble: 6 x 3 ## row col fill_colour ## ## 1 2 2 ## 2 2 3 ## 3 3 2 FFFFFF00 ## 4 3 3 ## 5 4 2 ## 6 4 3 FF92D050 # Step 3: append the `fill` column to the rest of the data left_join(extra_tidy, fills, by = c("row", "col")) ## # A tibble: 6 x 6 ## row col Name variable value fill_colour ## ## 1 2 2 Matilda Age 1 ## 2 2 3 Matilda Height 2 ## 3 3 2 Nicholas Age 3 FFFFFF00 ## 4 3 3 Nicholas Height 4 ## 5 4 2 Olivia Age 5 ## 6 4 3 Olivia Height 6 FF92D050 Here’s the same extra-tidy version, but using only tidyxl and unpivotr. fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb xlsx_cells(path, sheet = "annotations") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) %>% behead("left", Name) %>% behead("up", variable) %>% select(-data_type, -character, value = numeric) ## # A tibble: 6 x 6 ## row col value fill_colour Name variable ## ## 1 2 2 1 Matilda Age ## 2 2 3 2 Matilda Height ## 3 3 2 3 FFFFFF00 Nicholas Age ## 4 3 3 4 Nicholas Height ## 5 4 2 5 Olivia Age ## 6 4 3 6 FF92D050 Olivia Height --- # Detecting Spreadsheet Smells with xlex() • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/articles/smells.html#main) ![](https://nacnudus.github.io/tidyxl/articles/smells.html) Detecting Spreadsheet Smells with xlex() ======================================== #### Duncan Garmonsway #### 2023-12-21 Source: [`vignettes/smells.Rmd`](https://github.com/nacnudus/tidyxl/blob/HEAD/vignettes/smells.Rmd) `smells.Rmd` The function `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` separates formulas into tokens of different types, and gives their depth within a nested formula. Its name is a bad pun on ‘Excel’ and ‘lexer’. Try the [online demo](https://duncan-garmonsway.shinyapps.io/xlex/) or install the more experimental [lexl](https://nacnudus.github.io/lexl/) package to run `demo_lexl()` locally. It is useful for detecting spreadsheet smells, which are poor practices in spreadsheet design, such as deep nests of functions, or embedding constants in formulas. Inspecting the parse tree[](https://nacnudus.github.io/tidyxl/articles/smells.html#inspecting-the-parse-tree) -------------------------------------------------------------------------------------------------------------- Here’s an example with a simple formula `MIN(3,MAX(2,A1))` (the `=` symbol at the beginning of the formula is implied, because Excel doesn’t write it to the file). library(tidyxl) x <- xlex("MIN(3,MAX(2,A1))") x ## root ## ¦-- MIN function ## °-- ( fun_open ## ¦-- 3 number ## ¦-- , separator ## ¦-- MAX function ## °-- ( fun_open ## ¦-- 2 number ## ¦-- , separator ## °-- A1 ref ## °-- ) fun_close ## °-- ) fun_close Detecting constants inside formulas[](https://nacnudus.github.io/tidyxl/articles/smells.html#detecting-constants-inside-formulas) ---------------------------------------------------------------------------------------------------------------------------------- A smelly spreadsheet is distributed with the `tidyxl` package. It comes from the famous Enron subpoena, made available by [Felienne Hermans](https://www.felienne.com/archives/3634) . How does it look at a glance? Here’s a screenshot of part of one sheet, showing the formulas rather than the cell values. It’s a financial plan, using formulas to forecast the rest of the year, and the plan for the following year. What we want to see is whether the formulas have any embedded constants; ones that are hidden from our attention, but that are driving the forecasts. While we could read each formula, one by one, it would be a lot easier to visualise the ones containing constants. We can do this with `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` and a graph plotting library like `ggplot2`. The first step, after importing the spreadsheet, is to tokenize the formulas, using `xlsx()`. Let’s tokenize one formula to see what it looks like. ### One formula[](https://nacnudus.github.io/tidyxl/articles/smells.html#one-formula) library(dplyr) ## ## Attaching package: 'dplyr' ## The following objects are masked from 'package:stats': ## ## filter, lag ## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union library(tidyr) library(purrr) library(ggplot2) # The original filename was "barry_tycholiz__848__2002 Plan Worksheet CC107322.xlsx" sheet <- tidy_xlsx(system.file("extdata/enron-constants.xlsx", package = "tidyxl"), "Detail Breakdown")$data[[1]] ## Warning: 'tidy_xlsx()' is deprecated. ## Use 'xlsx_cells()' or 'xlsx_formats()' instead. sheet$formula[22] ## [1] "(C8/7)*12-48000" xlex(sheet$formula[22]) ## root ## °-- ( paren_open ## ¦-- C8 ref ## ¦-- / operator ## °-- 7 number ## ¦-- ) paren_close ## ¦-- * operator ## ¦-- 12 number ## ¦-- - operator ## °-- 48000 number The formula is `(C8/7)*12-48000`, and `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` separates it into its components. There are the parentheses, the operators (division, multiplication, subtraction), a reference to another cell (`C8`), and a few numeric constants: 7, 12, and 48000. What could they be? The 7 is probably the 7th month, July, because of the column header “July YTD”. A year-to-date figure is being divided by 7, then multiplied by 12 to forecast the year-end figure. The 48000 is more mysterious – perhaps a future payment is expected. Embedding these constants inside a formula is bad practice. Better practice would be to put the constants into their own cells, where they could be annotated with their meaning, and perhaps even named. Then formulas could refer to them, by name, e.g. (Compensation/MonthsToDate)*12Months-FuturePayments ### Many formulas[](https://nacnudus.github.io/tidyxl/articles/smells.html#many-formulas) The `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` function isn’t vectorized (because it returns a data frame), so we map it over each of the formulas to create a ‘nest’ column of individual data frames. tokens <- sheet %>% filter(!is.na(formula)) %>% select(row, col, formula) %>% mutate(tokens = map(formula, xlex)) %>% select(-formula) tokens ## # A tibble: 154 × 3 ## row col tokens ## ## 1 8 4 ## 2 9 4 ## 3 10 4 ## 4 12 4 ## 5 13 4 ## 6 14 3 ## 7 14 4 ## 8 14 5 ## 9 14 6 ## 10 17 4 ## # ℹ 144 more rows Then we can unnest the data frames and filter for tokens that are constants, to find out which cells have constants in their formulas. constants <- tokens %>% unnest(tokens) %>% filter(type %in% c("error", "bool", "number", "text")) constants ## # A tibble: 201 × 5 ## row col level type token ## ## 1 8 4 1 number 7 ## 2 8 4 0 number 12 ## 3 8 4 0 number 48000 ## 4 9 4 1 number 7 ## 5 9 4 0 number 12 ## 6 10 4 1 number 7 ## 7 10 4 0 number 12 ## 8 12 4 1 number 7 ## 9 12 4 0 number 12 ## 10 13 4 1 number 7 ## # ℹ 191 more rows Which constants are most common? Unsurprisingly, 12 and 7 are almost equally abundant, but there are also lots of 6s and 9s – two- and three-quarterly figures? Then there are some 150000s and the familiar 48000s, followed by some fractions that look like percentages, and then several one-offs. constants %>% count(token, sort = TRUE) %>% print(n = Inf) ## # A tibble: 24 × 2 ## token n ## ## 1 12 59 ## 2 7 58 ## 3 6 30 ## 4 9 30 ## 5 150000 4 ## 6 48000 2 ## 7 0.05 1 ## 8 0.1 1 ## 9 0.35 1 ## 10 0.5 1 ## 11 1.05 1 ## 12 10 1 ## 13 10000 1 ## 14 12000 1 ## 15 13000 1 ## 16 15000 1 ## 17 2000 1 ## 18 25000 1 ## 19 5000 1 ## 20 5320 1 ## 21 7314 1 ## 22 7800 1 ## 23 866 1 ## 24 95000 1 ### Visualising constants[](https://nacnudus.github.io/tidyxl/articles/smells.html#visualising-constants) A final step is to visualize the spreadsheet, highlighting cells that hide constants in their formulas. We already have a data frame of cells with constants, so we join it back to the full dataset, and pass the result into `ggplot`. This time there doesn’t seem to be any particular pattern, which is perhaps suspicious in itself. has_constants <- constants %>% distinct(row, col) %>% mutate(has_constant = TRUE) %>% right_join(sheet, by = c("row", "col")) %>% filter(!is_blank) %>% select(row, col, has_constant) %>% replace_na(list(has_constant = FALSE)) has_constants ## # A tibble: 412 × 3 ## row col has_constant ## ## 1 8 4 TRUE ## 2 9 4 TRUE ## 3 10 4 TRUE ## 4 12 4 TRUE ## 5 13 4 TRUE ## 6 17 4 TRUE ## 7 17 6 TRUE ## 8 18 4 TRUE ## 9 19 4 TRUE ## 10 20 4 TRUE ## # ℹ 402 more rows has_constants %>% # filter(row <= 28) %>% ggplot(aes(col, row, fill = has_constant)) + geom_tile() + scale_y_reverse() + theme(legend.position = "top") ![](https://nacnudus.github.io/tidyxl/articles/smells_files/figure-html/unnamed-chunk-7-1.png) Detecting deeply nested formulas[](https://nacnudus.github.io/tidyxl/articles/smells.html#detecting-deeply-nested-formulas) ---------------------------------------------------------------------------------------------------------------------------- Using the same techniques as for detecting constants, we map `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` over the formulas in a spreadsheet, unnest the result, and filter for tokens with particular properties. In this case, we are interested in the `level` of each token, which tells how deeply a token is nested in other functions and expressions. This time, we use another spreadsheet from the Enron corpus. First, an illustration. Notice that inside the first function, the level increases to 1. Inside the second function, the level increases to 2. xlex("MAX(3,MIN(2,4))") ## root ## ¦-- MAX function ## °-- ( fun_open ## ¦-- 3 number ## ¦-- , separator ## ¦-- MIN function ## °-- ( fun_open ## ¦-- 2 number ## ¦-- , separator ## °-- 4 number ## °-- ) fun_close ## °-- ) fun_close Now let’s apply the same test to all the formulas in a sheet. The deepest level of nesting turns out to be 7, and is seen in all the cells in row 171. # The original filename was "albert_meyers__1__1-25act.xlsx" sheet <- tidy_xlsx(system.file("extdata/enron-nested.xlsx", package = "tidyxl"), "Preschedule")$data[[1]] ## Warning: 'tidy_xlsx()' is deprecated. ## Use 'xlsx_cells()' or 'xlsx_formats()' instead. deepest <- sheet %>% filter(!is.na(formula)) %>% mutate(tokens = map(formula, xlex)) %>% select(row, col, tokens) %>% unnest(tokens) %>% filter(level == max(level)) %>% distinct(row, col, level) deepest ## # A tibble: 48 × 3 ## row col level ## ## 1 171 2 7 ## 2 171 3 7 ## 3 171 4 7 ## 4 171 5 7 ## 5 171 6 7 ## 6 171 7 7 ## 7 171 8 7 ## 8 171 9 7 ## 9 171 10 7 ## 10 171 11 7 ## # ℹ 38 more rows Do you wonder what those formulas look like? sheet %>% filter(row == 171, col == 2) %>% pull(formula) # Aaaaaaaaaaarghhhhhhhh! ## [1] "((IF((103-B$89)=103,0,(103-B$89)))+(IF((200-B$95)=200,0,(200-B$95)))+(IF((196-B$98)=196,0,(196-B$98)))+(IF((200-B$101)=200,0,(200-B$101)))+(IF((70-B$104)=70,0,(MIN(40,(70-B$104))))+(IF((78-B$109)=78,0,(MIN(50,(78-B$109)))))+(IF((103-B$114)=103,0,(MIN(66,(103-B$114)))))+(IF((195-B$119-B$124-B$129-B$134-B$139)=195,0,(MIN(70,(195-B$119-B$124-B$129-B$134-B$139)))))+(IF((64-B$144)=64,0,(MIN(50,(64-B$144)))))+(IF((48-B$149)=48,0,(MIN(20,(48-B$149)))))+(IF((44-B$154)=44,0,(MIN(20,(44-B$154)))))+(IF((130-B$159)=130,0,(MIN(20,(130-B$159)))))))" --- # Data Validation Rules • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html#main) ![](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html) Data Validation Rules ===================== #### Duncan Garmonsway #### 2023-12-21 Source: [`vignettes/data-validation-rules.Rmd`](https://github.com/nacnudus/tidyxl/blob/HEAD/vignettes/data-validation-rules.Rmd) `data-validation-rules.Rmd` What data validation rules are[](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html#what-data-validation-rules-are) --------------------------------------------------------------------------------------------------------------------------------------- Data validation rules control what constants can be entered into a cell, e.g. any whole number between 0 and 9, or one of several values from another part of the spreadsheet. ‘xlsx\_validation()’ returns each of the data validation rules in an xlsx file, and the ranges of cells to which each rule applies. Here is a rule that restricts input to integers between 0 and 9 inclusive, or no value (blank). If any other value is attempted, then an error message is displayed with the imaginative title “message title”, the informative body text “message body”, and a “stop” symbol. library(tidyxl) library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union library(tidyr) examples <- system.file("extdata/examples.xlsx", package = "tidyxl") glimpse(xlsx_validation(examples)[1, ]) #> Rows: 1 #> Columns: 14 #> $ sheet "Sheet1" #> $ ref "A106" #> $ type "whole" #> $ operator "between" #> $ formula1 "0" #> $ formula2 "9" #> $ allow_blank TRUE #> $ show_input_message TRUE #> $ prompt_title "message title" #> $ prompt_body "message body" #> $ show_error_message TRUE #> $ error_title "error title" #> $ error_body "error body" #> $ error_symbol "stop" The gamut of possible rules is given in the examples for `[xlsx_validation()](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) `. as.data.frame(xlsx_validation(examples)) #> sheet ref type operator formula1 #> 1 Sheet1 A106 whole between 0 #> 2 Sheet1 A108 list $B$108 #> 3 Sheet1 A110 date between 2017-01-01 00:00:00 #> 4 Sheet1 A111 time between 00:00:00 #> 5 Sheet1 A112 textLength between 0 #> 6 Sheet1 A114 whole notBetween 0 #> 7 Sheet1 A115,A121:A122 whole equal 0 #> 8 Sheet1 A116 whole notEqual 0 #> 9 Sheet1 A117 whole greaterThan 0 #> 10 Sheet1 A119 whole greaterThanOrEqual 0 #> 11 Sheet1 A120 whole lessThanOrEqual 0 #> 12 Sheet1 A118 whole lessThan 0 #> 13 Sheet1 A107 decimal notBetween 0 #> 14 Sheet1 A113 custom A113<=LEN(B113) #> 15 Sheet1 A109 list $B$108 #> formula2 allow_blank show_input_message prompt_title #> 1 9 TRUE TRUE message title #> 2 TRUE TRUE #> 3 2017-01-09 09:00:00 TRUE TRUE #> 4 09:00:00 TRUE TRUE #> 5 9 TRUE TRUE #> 6 9 TRUE TRUE #> 7 TRUE TRUE #> 8 TRUE TRUE #> 9 TRUE TRUE #> 10 TRUE TRUE #> 11 TRUE TRUE #> 12 TRUE TRUE #> 13 9 FALSE FALSE #> 14 TRUE TRUE #> 15 TRUE TRUE #> prompt_body show_error_message error_title error_body error_symbol #> 1 message body TRUE error title error body stop #> 2 TRUE warning #> 3 TRUE stop #> 4 TRUE stop #> 5 TRUE stop #> 6 TRUE stop #> 7 TRUE stop #> 8 TRUE stop #> 9 TRUE stop #> 10 TRUE stop #> 11 TRUE stop #> 12 TRUE stop #> 13 FALSE stop #> 14 TRUE stop #> 15 TRUE information Joining rules to cells[](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html#joining-rules-to-cells) ----------------------------------------------------------------------------------------------------------------------- > There are no built-in functions for joining ranges like `A1:D5,G8` to single cells like `B3`. For now, use the snippets in this section. In future I might develop a dplyr-like join function (this is hard currently because dplyr doesn’t yet join on arbitrary functions, or even the standard inequalities like `>=`). Help and advice would be gratefully accepted! To join rules to cells, a naive method is to use the `sheet` and `ref` columns to match the `sheet` and `address` columns to the output of `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) `. rules <- xlsx_validation(examples) cells <- filter(xlsx_cells(examples), row >= 106, col == 1) rules #> # A tibble: 15 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE #> 5 Sheet1 A112 text… between 0 9 TRUE TRUE #> 6 Sheet1 A114 whole notBetw… 0 9 TRUE TRUE #> 7 Sheet1 A115,… whole equal 0 NA TRUE TRUE #> 8 Sheet1 A116 whole notEqual 0 NA TRUE TRUE #> 9 Sheet1 A117 whole greater… 0 NA TRUE TRUE #> 10 Sheet1 A119 whole greater… 0 NA TRUE TRUE #> 11 Sheet1 A120 whole lessTha… 0 NA TRUE TRUE #> 12 Sheet1 A118 whole lessThan 0 NA TRUE TRUE #> 13 Sheet1 A107 deci… notBetw… 0 9 FALSE FALSE #> 14 Sheet1 A113 cust… NA A113<=L… NA TRUE TRUE #> 15 Sheet1 A109 list NA $B$108 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol cells #> # A tibble: 93 × 24 #> sheet address row col is_blank content data_type error logical numeric #> #> 1 Sheet1 A106 106 1 FALSE 0 numeric NA NA 0 #> 2 Sheet1 A107 107 1 FALSE 0.1 numeric NA NA 0.1 #> 3 Sheet1 A108 108 1 FALSE 137 character NA NA NA #> 4 Sheet1 A109 109 1 FALSE 137 character NA NA NA #> 5 Sheet1 A110 110 1 FALSE 42736 date NA NA NA #> 6 Sheet1 A111 111 1 FALSE 0.354166… date NA NA NA #> 7 Sheet1 A112 112 1 FALSE 149 character NA NA NA #> 8 Sheet1 A113 113 1 FALSE 10 numeric NA NA 10 #> 9 Sheet1 A114 114 1 FALSE -1 numeric NA NA -1 #> 10 Sheet1 A115 115 1 FALSE 0 numeric NA NA 0 #> # ℹ 83 more rows #> # ℹ 14 more variables: date , character , #> # character_formatted , formula , is_array , #> # formula_ref , formula_group , comment , height , #> # width , row_outline_level , col_outline_level , #> # style_format , local_format_id inner_join(rules, cells, by = c("sheet" = "sheet", "ref" = "address")) #> # A tibble: 9 × 36 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE #> 5 Sheet1 A112 textLe… between 0 9 TRUE TRUE #> 6 Sheet1 A114 whole notBetw… 0 9 TRUE TRUE #> 7 Sheet1 A107 decimal notBetw… 0 9 FALSE FALSE #> 8 Sheet1 A113 custom NA A113<=L… NA TRUE TRUE #> 9 Sheet1 A109 list NA $B$108 NA TRUE TRUE #> # ℹ 28 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol , row , col , is_blank , content , #> # data_type , error , logical , numeric , date , #> # character , character_formatted , formula , is_array , #> # formula_ref , formula_group , comment , height , #> # width , row_outline_level , col_outline_level , … Notice that only 9 cells were joined, even though 15 rules were defined. Surely at least 15 cells ought to be joined? The reason why they are not is that the cells for the other 6 rules don’t exist – rules can be defined for cells that have no value, and cells with no value are not returned by `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) `, otherwise all 17179869184 cells in a worksheet must be returned. A more subtle reason for certain cells not to have joined successfully is that the `ref` column of the rules sometimes refers to more than one cell, and can even refer to several, non-contiguous ranges of cells. Specifically, the seventh rule’s `ref` column has `A115,A121:A122`. Special treatment is needed here. Ideally, some kind of join function would be defined that can compare indidual cells with ranges. But I haven’t written one, so what follows is a workaround. First, the two ranges of cells must be `unnested` into `A115` and `A121:122`. Then the range `A121:122` must be ‘unranged’ into `A121` and `A122`. unrange <- function(x) { limits <- cellranger::as.cell_limits(x) rows <- seq(limits$ul[1], limits$lr[1]) cols <- seq(limits$ul[2], limits$lr[2]) rowcol <- expand.grid(rows, cols) cell_addrs <- cellranger::cell_addr(rowcol[[1]], rowcol[[2]]) cellranger::to_string(cell_addrs, fo = "A1", strict = FALSE) } unnest_ref <- function(x, ref) { UseMethod("unnest_ref") } unnest_ref.default <- function(x, ref_col = ref) { stopifnot(is.character(x), length(x) == 1L) refs <- unlist(strsplit(x, ",", fixed = TRUE)) unlist(lapply(refs, unrange)) } unrange("A121:A122") #> [1] "A121" "A122" unnest_ref("A115,A121:A122") #> [1] "A115" "A121" "A122" The `unnest_ref()` function can also be defined for whole data frames, unnesting them by a column of references. unnest_ref.data.frame <- function(x, ref_col) { ref <- rlang::enquo(ref_col) x[[rlang::quo_name(ref)]] <- lapply(x[[rlang::quo_name(ref)]], unnest_ref) tidyr::unnest(x, rlang::UQ(ref)) } (nested_rule <- slice(rules, 7)) #> # A tibble: 1 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A115,A… whole equal 0 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol unnest_ref(nested_rule, ref) #> Warning: Prefixing `UQ()` with the rlang namespace is deprecated as of rlang 0.3.0. #> Please use the non-prefixed form or `!!` instead. #> #> # Bad: rlang::expr(mean(rlang::UQ(var) * 100)) #> #> # Ok: rlang::expr(mean(UQ(var) * 100)) #> #> # Good: rlang::expr(mean(!!var * 100)) #> This warning is displayed once every 8 hours. #> # A tibble: 3 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A115 whole equal 0 NA TRUE TRUE #> 2 Sheet1 A121 whole equal 0 NA TRUE TRUE #> 3 Sheet1 A122 whole equal 0 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol Finally the new data frame of rules can be joined to a data frame of cells in any of the usual ways, via the `sheet` and `ref` columns. Problems with this approach occur with rules that are defined over large ranges of cells: the ‘unnesting’ of those ranges results in very long vectors of individual cell addresses, or (worse) huge data frames of rules. Such cases are commonplace, because rules are often defined for entire columns of a spreadsheet, and a column has 1048576 rows. --- # 2.3 Meaningfully formatted rows | Spreadsheet Munging Strategies Type to search [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Toggle Sidebar") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Search") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Share") FacebookTwitterLinkedInWeiboInstapaper [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Facebook") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Twitter") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "LinkedIn") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Font Settings") AA SerifSans WhiteSepiaNight [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Edit") [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Download") PDF EPUB [](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-rows# "Information about the toolbar") [Spreadsheet Munging Strategies](https://nacnudus.github.io/spreadsheet-munging-strategies/) ============================================================================================= 2.3 Meaningfully formatted rows ------------------------------- ![](https://nacnudus.github.io/spreadsheet-munging-strategies/images/highlights.png) As with [clean, tidy tables](https://nacnudus.github.io/spreadsheet-munging-strategies/clean) , but with a second step to interpret the formatting. Sometimes whole rows in a table are highlighted by formatting them with, say, a bright yellow fill. The highlighting could mean “this observation should be ignored”, or “this product is no longer available”. Different colours could mean different levels of a hierarchy, e.g. green for “pass” and red for “fail”. There are three steps to interpreting this. 1. Import the table, taking only the cell values and ignoring the formatting. 2. Import one column of the table, taking only the formatting and not the cell values. 3. Use `dplyr::bind_cols()` to append the column of formatting to the table of cell values. You can then interpret the formatting however you like. Step 1 is the same as [clean, tidy tables](https://nacnudus.github.io/spreadsheet-munging-strategies/clean) . Step 2 uses `tidyxl::xlsx_cells()` to load the data, `tidyxl::xlsx_formats()`, and several tidyverse functions to link the two and filter for only one column. Why only one column? Because if a whole row is highlighted, then you only need to know the highlighting of one column to know the highlighting of all the others. This is a special case of the following section, [meaningfully formatted cells](https://nacnudus.github.io/spreadsheet-munging-strategies/tidy-formatted-cells) . Here `dplyr::bind_cols()` can be used as a shortcut, because we are joining exactly `n` rows of formatting to `n` rows of data. The following sections is a more general case that can be used instead of this procedure. # Step 1: import the table taking only cell values and ignoring the formatting path <- system.file("extdata", "worked-examples.xlsx", package = "unpivotr") x <- read_excel(path, sheet = "highlights") # Step 2: import one column of the table, taking only the formatting and not the # cell values # `formats` is a pallette of fill colours that can be indexed by the # `local_format_id` of a given cell to get the fill colour of that cell fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb # Import all the cells, filter out the header row, filter for the first column, # and create a new column `fill_colour` of the fill colours, by looking up the # local_format_id of each cell in the `fill_colours` pallette. fills <- xlsx_cells(path, sheet = "highlights") %>% dplyr::filter(row >= 2, col == 1) %>% # Omit the header row mutate(fill_colour = fill_colours[local_format_id]) %>% select(fill_colour) # Step 3: append the `fill` column to the rest of the data bind_cols(x, fills) %>% select(Age, Height, fill_colour) ## # A tibble: 3 x 3 ## Age Height fill_colour ## ## 1 1 2 ## 2 3 4 FFFFFF00 ## 3 5 6 Note that the fill colour is expressed as an RGB value with transparency in the first two letters, e.g. `FFFFFF00` is `FF` (opaque), with `FFFF00` (yellow). Here’s another way using only tidyxl and unpivotr. fill_colours <- xlsx_formats(path)$local$fill$patternFill$fgColor$rgb xlsx_cells(path, sheet = "highlights") %>% mutate(fill_colour = fill_colours[local_format_id]) %>% select(row, col, data_type, character, numeric, fill_colour) %>% behead("up", header) %>% select(-col, -character) %>% spatter(header) %>% select(-row) ## # A tibble: 3 x 3 ## fill_colour Age Height ## ## 1 1 2 ## 2 FFFFFF00 3 4 ## 3 5 6 --- # Tokenise 'Excel' Formulas • lexl [](https://nacnudus.github.io/lexl/#lexl) lexl ============================================== [lexl](https://github.com/nacnudus/lexl) separates Excel formulas into tokens of different types, and gives their depth within a nested formula. Its name is a bad pun on ‘Excel’ and ‘lexer’. Try the [online demo](https://duncan-garmonsway.shinyapps.io/lexl/) or run `[demo_lexl()](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` locally. [](https://nacnudus.github.io/lexl/#installation) Installation -------------------------------------------------------------- You can install lexl from github with: # install.packages("devtools") devtools::install_github("nacnudus/lexl") [](https://nacnudus.github.io/lexl/#example) Example ---------------------------------------------------- library(lexl) x <- lex_xl("MIN(3,MAX(2,A1))") x #> level type token #> 1 0 function MIN #> 2 0 fun_open ( #> 3 1 number 3 #> 4 1 separator , #> 5 1 function MAX #> 6 1 fun_open ( #> 7 2 number 2 #> 8 2 separator , #> 9 2 ref A1 #> 10 1 fun_close ) #> 11 0 fun_close ) plot(x) # Requires the ggraph package ![](https://nacnudus.github.io/lexl/reference/figures/README-unnamed-chunk-1-1.png) [](https://nacnudus.github.io/lexl/#parse-tree) Parse tree ---------------------------------------------------------- Not all parse trees are the same. The one given by `[lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` is intended for analysis, rather than for computation. Examples of the kind of analysis that it might support are: * Detecting constants that have been embedded inside formulas, rather than in cells referred to by formulas. * Revealing which functions and combinations of functions are most common. * Untangling the dependencies between cells in a spreadsheet. [](https://nacnudus.github.io/lexl/#where-to-find-specimen-formulas) Where to find specimen formulas ---------------------------------------------------------------------------------------------------- The [tidyxl](https://nacnudus.github.io/tidyxl) package imports formulas from xlsx (spreadsheet) files. The [Enron corpus](https://figshare.com/articles/Enron_Spreadsheets_and_Emails/1221767) contains thousands of real-life spreadsheets. [](https://nacnudus.github.io/lexl/#inspiration) Inspiration ------------------------------------------------------------ [Research](https://drive.google.com/file/d/0B79P2Uym3JjvMjlaWWtnTWRLQmc/view?usp=sharing) by Felienne Hermans inspired this package, and the related [XLParser](https://github.com/spreadsheetlab/XLParser) project was a great help in creating the grammar. --- # Tidyxl • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#main) ![](https://nacnudus.github.io/tidyxl/articles/tidyxl.html) Tidyxl ====== #### Duncan Garmonsway #### 2023-12-21 Source: [`vignettes/tidyxl.Rmd`](https://github.com/nacnudus/tidyxl/blob/HEAD/vignettes/tidyxl.Rmd) `tidyxl.Rmd` tidyxl[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#tidyxl) ------------------------------------------------------------------------ [tidyxl](https://github.com/nacnudus/tidyxl) imports non-tabular data from Excel files into R. It exposes cell content, position, formatting and comments in a tidy structure for further manipulation, especially by the [unpivotr](https://github.com/nacnudus/unpivotr) package. It supports the xml-based file formats ‘.xlsx’ and ‘.xlsm’ via the embedded [RapidXML](https://rapidxml.sourceforge.net/) C++ library. It does not support the binary file formats ‘.xlsb’ or ‘.xls’. It also provides a function `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` for tokenizing formulas. See the [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html) for details. It is useful for detecting ‘spreadsheet smells’ (poor practice such as embedding constants in formulas, or using deep levels of nesting), and for understanding the dependency structures within spreadsheets. ### Mailing list[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#mailing-list) For bugs and/or issues, create a new issue on [GitHub](https://github.com/nacnudus/tidyxl/issues) For other questions or comments, please subscribe to the [tidyxl-devel mailing list](https://groups.google.com/forum/#!forum/tidyxl-devel) . You must be a member to post messages, but anyone can read the archived discussions. ### Installation[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#installation) devtools::install_github("nacnudus/tidyxl") ### Examples[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#examples) The package includes a spreadsheet, ‘titanic.xlsx’, which contains the following pivot table: ftable(Titanic, row.vars = 1:2) #> Age Child Adult #> Survived No Yes No Yes #> Class Sex #> 1st Male 0 5 118 57 #> Female 0 1 4 140 #> 2nd Male 0 11 154 14 #> Female 0 13 13 80 #> 3rd Male 35 13 387 75 #> Female 17 14 89 76 #> Crew Male 0 0 670 192 #> Female 0 0 3 20 The multi-row column headers make this difficult to import. A popular package for importing spreadsheets coerces the pivot table into a dataframe. It treats the second header row as though it were observations. titanic <- system.file("extdata/titanic.xlsx", package = "tidyxl") readxl::read_excel(titanic) #> New names: #> • `` -> `...1` #> • `` -> `...2` #> • `` -> `...5` #> • `` -> `...7` #> # A tibble: 10 × 7 #> ...1 ...2 Age Child ...5 Adult ...7 #> #> 1 NA NA Survived No Yes No Yes #> 2 Class Sex NA NA NA NA NA #> 3 1st Male NA 0 5 118 57 #> 4 NA Female NA 0 1 4 140 #> 5 2nd Male NA 0 11 154 14 #> 6 NA Female NA 0 13 13 80 #> 7 3rd Male NA 35 13 387 75 #> 8 NA Female NA 17 14 89 76 #> 9 Crew Male NA 0 0 670 192 #> 10 NA Female NA 0 0 3 20 [tidyxl](https://github.com/nacnudus/tidyxl) doesn’t coerce the pivot table into a data frame. Instead, it represents each cell in its own row, where it describes the cell’s address, value and other properties. library(tidyxl) x <- xlsx_cells(titanic) dplyr::glimpse(x) #> Rows: 60 #> Columns: 24 #> $ sheet "Sheet1", "Sheet1", "Sheet1", "Sheet1", "Sheet1", … #> $ address "C1", "D1", "E1", "F1", "G1", "C2", "D2", "E2", "F… #> $ row 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4,… #> $ col 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 1, 2, 1, 2, 4, 5, 6,… #> $ is_blank FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FAL… #> $ content "0", "1", NA, "2", NA, "3", "4", "5", "4", "5", "6… #> $ data_type "character", "character", "blank", "character", "b… #> $ error NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ logical NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ numeric NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… #> $ date NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N… #> $ character "Age", "Child", NA, "Adult", NA, "Survived", "No",… #> $ character_formatted [], [], , […\ #> $ formula NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ is_array FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…\ #> $ formula_ref NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ formula_group NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ comment NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…\ #> $ height 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15…\ #> $ width 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.38, 8.…\ #> $ row_outline_level 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…\ #> $ col_outline_level 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…\ #> $ style_format "Normal", "Normal", "Normal", "Normal", "Normal", …\ #> $ local_format_id 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 2, 3, 3, 1, 1, 1,…\ \ In this structure, the cells can be found by filtering.\ \ x[x$data_type == "character", c("address", "character")]\ #> # A tibble: 22 × 2\ #> address character\ #> \ #> 1 C1 Age \ #> 2 D1 Child \ #> 3 F1 Adult \ #> 4 C2 Survived \ #> 5 D2 No \ #> 6 E2 Yes \ #> 7 F2 No \ #> 8 G2 Yes \ #> 9 A3 Class \ #> 10 B3 Sex \ #> # ℹ 12 more rows\ x[x$row == 4, c("address", "character", "numeric")]\ #> # A tibble: 6 × 3\ #> address character numeric\ #> \ #> 1 A4 1st NA\ #> 2 B4 Male NA\ #> 3 D4 NA 0\ #> 4 E4 NA 5\ #> 5 F4 NA 118\ #> 6 G4 NA 57\ \ Specific sheets can be requested using `xlsx_cells(file, sheet)`, and the names of all sheets in a file are given by `[xlsx_sheet_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html) `.\ \ #### Formatting[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#formatting)\ \ The original spreadsheet has formatting applied to the cells. This can also be retrieved using [tidyxl](https://github.com/nacnudus/tidyxl)\ , with the `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` function.\ \ ![Screenshot of a spreadsheet with meaningful formatting](https://nacnudus.github.io/tidyxl/articles/titanic-screenshot.png)\ \ Formatting is available by using the columns `local_format_id` and `style_format` as indexes into a separate list-of-lists structure. ‘Local’ formatting is the most common kind, applied to individual cells. ‘Style’ formatting is usually applied to blocks of cells, and defines several formats at once. Here is a screenshot of the styles buttons in Excel.\ \ ![Screenshot of a formatting options available in Excel](https://nacnudus.github.io/tidyxl/articles/styles-screenshot.png)\ \ Formatting can be looked up as follows.\ \ # Bold\ formats <- xlsx_formats(titanic)\ formats$local$font$bold\ #> [1] FALSE TRUE FALSE FALSE\ x[x$local_format_id %in% which(formats$local$font$bold),\ c("address", "character")]\ #> # A tibble: 4 × 2\ #> address character\ #> \ #> 1 C1 Age \ #> 2 C2 Survived \ #> 3 A3 Class \ #> 4 B3 Sex\ \ # Yellow fill\ formats$local$fill$patternFill$fgColor$rgb\ #> [1] NA NA NA "FFFFFF00"\ x[x$local_format_id %in%\ which(formats$local$fill$patternFill$fgColor$rgb == "FFFFFF00"),\ c("address", "numeric")]\ #> # A tibble: 2 × 2\ #> address numeric\ #> \ #> 1 F11 3\ #> 2 G11 20\ \ # Styles by name\ formats$style$font$name["Normal"]\ #> Normal \ #> "Calibri"\ head(x[x$style_format == "Normal", c("address", "character")])\ #> # A tibble: 6 × 2\ #> address character\ #> \ #> 1 C1 Age \ #> 2 D1 Child \ #> 3 E1 NA \ #> 4 F1 Adult \ #> 5 G1 NA \ #> 6 C2 Survived\ \ # In-cell formatting is available in the `character_formatted` column as a data\ # frame, one row per substring.\ examples <- system.file("/extdata/examples.xlsx", package = "tidyxl")\ xlsx_cells(examples)$character_formatted[77]\ #> [[1]]\ #> # A tibble: 16 × 14\ #> character bold italic underline strike vertAlign size color_rgb color_theme\ #> \ #> 1 "in-cell" FALSE FALSE NA FALSE NA 0 NA NA\ #> 2 "bold" TRUE FALSE NA FALSE NA 0 FF000000 2\ #> 3 "italic" FALSE TRUE NA FALSE NA 0 FF000000 2\ #> 4 "underli… FALSE FALSE single FALSE NA 0 FF000000 2\ #> 5 "underli… FALSE FALSE double FALSE NA 0 FF000000 2\ #> 6 "singlea… FALSE FALSE singleAc… FALSE NA 0 FF000000 2\ #> 7 "doublea… FALSE FALSE doubleAc… FALSE NA 0 FF000000 2\ #> 8 "striket… FALSE FALSE NA TRUE NA 0 FF000000 2\ #> 9 "subscri… FALSE FALSE NA FALSE subscript 0 FF000000 2\ #> 10 "supersc… FALSE FALSE NA FALSE superscr… 0 FF000000 2\ #> 11 "red" FALSE FALSE NA FALSE NA 0 FFFF0000 NA\ #> 12 "theme" FALSE FALSE NA FALSE NA 0 FFC0504D 6\ #> 13 "tint" FALSE FALSE NA FALSE NA 0 FFC0504D 6\ #> 14 "size" FALSE FALSE NA FALSE NA 0 FFFF0000 NA\ #> 15 "arial" FALSE FALSE NA FALSE NA 0 FFFF0000 NA\ #> 16 "UTF8Sté… FALSE FALSE NA FALSE NA 0 FFFF0000 NA\ #> # ℹ 5 more variables: color_indexed , color_tint , font ,\ #> # family , scheme \ \ To see all the available kinds of formats, use `str(formats)`.\ \ #### Comments[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#comments)\ \ Comments are available alongside cell values.\ \ x[!is.na(x$comment), c("address", "comment")]\ #> # A tibble: 1 × 2\ #> address comment \ #> \ #> 1 G11 All women in the crew worked in the victualling department.\ \ #### Formulas[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#formulas)\ \ Formulas are available, but with a few quirks.\ \ options(width = 120)\ y <- xlsx_cells(examples, "Sheet1")\ y[!is.na(y$formula),\ c("address", "formula", "is_array", "formula_ref", "formula_group",\ "error", "logical", "numeric", "date", "character")]\ #> # A tibble: 32 × 10\ #> address formula is_array formula_ref formula_group error logical numeric date character \ #> \ #> 1 A1 "1/0" FALSE NA NA #DIV/0! NA NA NA NA \ #> 2 A14 "1=1" FALSE NA NA NA TRUE NA NA NA \ #> 3 A15 "A4+1" FALSE NA NA NA NA 1338 NA NA \ #> 4 A16 "DATE(2017,1,18)" FALSE NA NA NA NA NA 2017-01-18 00:00:00 NA \ #> 5 A17 "\"Hello, World!\"" FALSE NA NA NA NA NA NA Hello, Wo…\ #> 6 A19 "$A$18+1" FALSE NA NA NA NA 2 NA NA \ #> 7 B19 "A18+2" FALSE NA NA NA NA 3 NA NA \ #> 8 A20 "$A$18+1" FALSE A20:A21 0 NA NA 2 NA NA \ #> 9 B20 "A19+2" FALSE B20:B21 1 NA NA 4 NA NA \ #> 10 A21 "$A$18+1" FALSE NA 0 NA NA 2 NA NA \ #> # ℹ 22 more rows\ \ The top five cells show that the results of formulas are available as usual in the columns `error`, `logical`, `numeric`, `date`, and `character`.\ \ ##### Shared formulas[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#shared-formulas)\ \ Cells `A20` and `A21` illustrate how formulas are normalised before being written to file, using the `formula_ref` and `formula_group` columns. When there is a group of cells whose formulas only differ by cell reference (e.g. “=A1+1”, “=A2+1”, “=A3+1”, etc.), only one formula in each group is written to the file, so `tidyxl` infers what the formulas in the other cells in the group must be, from their relative positions.\ \ ##### Array formulas[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#array-formulas)\ \ There are two kinds of array formulas: ones that compute over arrays, and ones whose output is an array (of cells).\ \ Both kinds are distinguished in spreadsheet programs by curly braces, e.g. `{SUM(A19:A21*B19:B21)}`. In `tidyxl`, the curly braces are ommitted (as they are from the file itself), and instead the `is_array` column has the value `TRUE`.\ \ The first kind (those that compute over arrays) is illustrated by cell `A22`.\ \ The second kind (those whose value is spread across an array of cells) is illustrated by cells `A23` and `A24`. The formula is only given in the top-left cell (`A23`), which is also the only cell that describes the range of cells containing the result, in the `formula-ref` column. The results themselves are stored in all relevant cells (`A23` and `A24`). Unlike shared formulas, there is no `formula_group` to associate the cells of an array formula’s result. If you need to do identify those cells, use the [cellranger](https://github.com/rsheets/cellranger)\ package and the `formula_ref` column.\ \ ##### Formulas referring to other files[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#formulas-referring-to-other-files)\ \ Cell `A25` contains a formula that refers to another file. The `[1]` is an index into a table of files. The roadmap for [tidyxl](https://github.com/nacnudus/tidyxl)\ includes de-referencing such numbers.\ \ ##### Tokenizing formulas[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#tokenizing-formulas)\ \ The function `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` separates formulas into tokens of different types, and gives their depth within a nested formula. Its name is a bad pun on ‘Excel’ and ‘lexer’. Try the [online demo](https://duncan-garmonsway.shinyapps.io/xlex/)\ or install the more experimental [lexl](https://nacnudus.github.io/lexl/)\ package to run `demo_lexl()` locally.\ \ It is useful for detecting spreadsheet smells, which are poor practices in spreadsheet design, such as deep nests of functions, or embedding constants in formulas.\ \ x <- xlex("MIN(3,MAX(2,A1))")\ x\ #> root \ #> ¦-- MIN function\ #> °-- ( fun_open\ #> ¦-- 3 number\ #> ¦-- , separator\ #> ¦-- MAX function\ #> °-- ( fun_open\ #> ¦-- 2 number\ #> ¦-- , separator\ #> °-- A1 ref\ #> °-- ) fun_close\ #> °-- ) fun_close\ \ See the [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html)\ for more examples and details.\ \ #### Named ranges[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#named-ranges)\ \ Names are imported with `[xlsx_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) `. Also called ‘named formulas’ and ‘defined names’, these are usually used to name particular cells or ranges, making formulas that refer to them more readable. Ones that _are_ ranges are identifed by the `is_range` column (using `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) `), making it easier to match the names to the cells returned by `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` – e.g. by using the [`cellranger`](https://github.com/rsheets/cellranger)\ package.\ \ When the scope of the name is within a particular sheet, rather than global, the sheet name is given.\ \ xlsx_names(examples)\ #> rId sheet name formula comment hidden is_range\ #> 1 1 Sheet1 named_local_formula MAX(Sheet1!$A$129:$A$130)+1 FALSE FALSE\ #> 2 4 E09904.2 sheet_beyond_chart E09904.2!$A$1,E09904.2!$C$1 FALSE TRUE\ #> 3 NA intersection Sheet1!$B:$B Sheet1!$8:$8 FALSE TRUE\ #> 4 NA named_global_formula Sheet1!$A$129-1 FALSE FALSE\ #> 5 NA named_range Sheet1!$A$129 My comment FALSE TRUE\ \ #### Data validation rules[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#data-validation-rules)\ \ Data validation rules are imported with `[xlsx_validation()](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) `. These rules control what values may be entered into a cell, and are often used to create a drop-down list in a cell. Read the [vignette](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html)\ for details.\ \ xlsx_validation(examples)\ #> # A tibble: 15 × 14\ #> sheet ref type operator formula1 formula2 allow_blank show_input_message prompt_title prompt_body\ #> \ #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE message tit… message bo…\ #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE NA NA \ #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE NA NA \ #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE NA NA \ #> 5 Sheet1 A112 textLength between 0 9 TRUE TRUE NA NA \ #> 6 Sheet1 A114 whole notBetween 0 9 TRUE TRUE NA NA \ #> 7 Sheet1 A115,A121:A122 whole equal 0 NA TRUE TRUE NA NA \ #> 8 Sheet1 A116 whole notEqual 0 NA TRUE TRUE NA NA \ #> 9 Sheet1 A117 whole greaterTh… 0 NA TRUE TRUE NA NA \ #> 10 Sheet1 A119 whole greaterTh… 0 NA TRUE TRUE NA NA \ #> 11 Sheet1 A120 whole lessThanO… 0 NA TRUE TRUE NA NA \ #> 12 Sheet1 A118 whole lessThan 0 NA TRUE TRUE NA NA \ #> 13 Sheet1 A107 decimal notBetween 0 9 FALSE FALSE NA NA \ #> 14 Sheet1 A113 custom NA A113<=L… NA TRUE TRUE NA NA \ #> 15 Sheet1 A109 list NA $B$108 NA TRUE TRUE NA NA \ #> # ℹ 4 more variables: show_error_message , error_title , error_body , error_symbol \ \ ### Philosophy[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#philosophy)\ \ Information in in many spreadsheets cannot be easily imported into R. Why?\ \ Most R packages that import spreadsheets have difficulty unless the layout of the spreadsheet conforms to a strict definition of a ‘table’, e.g.:\ \ * observations in rows\ * variables in columns\ * a single header row\ * all information represented by characters, whether textual, logical, or numeric\ \ These rules are designed to eliminate ambiguity in the interpretation of the information. But most spreadsheeting software relaxes these rules in a trade of ambiguity for expression via other media:\ \ * proximity (other than headers, i.e. other than being the first value at the top of a column)\ * formatting (colours and borders)\ \ Humans can usually resolve the ambiguities with contextual knowledge, but computers are limited by their ignorance. Programmers are hampered by:\ \ * their language’s expressiveness\ * loss of information in transfer from spreadsheet to programming library\ \ Information is lost when software discards it in order to force the data into tabular form. Sometimes date formatting is retained, but mostly formatting is lost, and position has to be inferred again.\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ addresses the programmer’s problems by not discarding information. It imports the content, position and formatting of cells, leaving it up to the user to associate the different forms of information, and to re-encode them in tabular form without loss. The [unpivotr](https://github.com/nacnudus/unpivotr)\ package has been developed to assist with that step.\ \ ### Similar projects[](https://nacnudus.github.io/tidyxl/articles/tidyxl.html#similar-projects)\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ was originally derived from [readxl](https://github.com/tidyverse/readxl)\ and still contains some of the same code, hence it inherits the GPL-3 licence. [readxl](https://github.com/tidyverse/readxl)\ is intended for importing tabular data with a single row of column headers, whereas [tidyxl](https://github.com/nacnudus/tidyxl)\ is more general, and less magic.\ \ The [rsheets](https://github.com/rsheets)\ project of several R packages is in the early stages of importing spreadsheet information from Excel and Google Sheets into R, manipulating it, and potentially parsing and processing formulas and writing out to spreadsheet files. In particular, [jailbreaker](https://github.com/rsheets/jailbreakr)\ attempts to extract non-tabular data from spreadsheets into tabular structures automatically via some clever algorithms.\ \ [tidyxl](https://github.com/nacnudus/tidyxl)\ differs from [rsheets](https://github.com/rsheets)\ in scope ([tidyxl](https://github.com/nacnudus/tidyxl)\ will never import charts, for example), and implementation ([tidyxl](https://github.com/nacnudus/tidyxl)\ is implemented mainly in C++ and is quite fast, only a little slower than [readxl](https://github.com/tidyverse/readxl)\ ). [unpivotr](https://github.com/nacnudus/unpivotr)\ is a package related to [tidyxl](https://github.com/nacnudus/tidyxl)\ that provides tools for unpivoting complex and non-tabular data layouts using I not AI (intelligence, not artificial intelligence). In this way it corresponds to [jailbreaker](https://github.com/rsheets/jailbreakr)\ , but with a different philosophy. --- # Changelog • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/news/index.html#main) ![](https://nacnudus.github.io/tidyxl/news/index.html) Changelog ========= Source: [`NEWS.md`](https://github.com/nacnudus/tidyxl/blob/HEAD/NEWS.md) tidyxl 1.0.10[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-1010) ------------------------------------------------------------------------------- * Fixed a bug in the support for formatted strings, which sometimes weren’t being imported ([#92](https://github.com/nacnudus/tidyxl/issues/92) ) tidyxl 1.0.9[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-109) ----------------------------------------------------------------------------- CRAN release: 2023-10-30 * CRAN fixes tidyxl 1.0.8[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-108) ----------------------------------------------------------------------------- CRAN release: 2022-09-01 * tidyxl is now licensed as MIT ([#63](https://github.com/nacnudus/tidyxl/issues/63) ). * Fixed a unicode bug ([#64](https://github.com/nacnudus/tidyxl/issues/64) ) * Fixed two crashes ([#71](https://github.com/nacnudus/tidyxl/issues/71) , [#73](https://github.com/nacnudus/tidyxl/issues/73) ) * Fixed a rare bug in number formats ([#52](https://github.com/nacnudus/tidyxl/issues/52) , [#75](https://github.com/nacnudus/tidyxl/issues/75) , [@da11an](https://github.com/da11an) ) * New feature: Import outline levels of rows and columns ([#77](https://github.com/nacnudus/tidyxl/issues/77) , [#78](https://github.com/nacnudus/tidyxl/issues/78) ) * New feature: Import raw cell value into `content` column ([#80](https://github.com/nacnudus/tidyxl/issues/80) ) * Compatibility with upcoming Rcpp release ([#86](https://github.com/nacnudus/tidyxl/issues/86) , [@Enchufa2](https://github.com/Enchufa2) ) tidyxl 1.0.7[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-107) ----------------------------------------------------------------------------- CRAN release: 2020-11-16 * Update namespace in C++ code for compatibility with the latest version of PEGTL, wrapped in the {piton} package. tidyxl 1.0.6[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-106) ----------------------------------------------------------------------------- CRAN release: 2020-05-09 * Fix compiler warnings. tidyxl 1.0.5[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-105) ----------------------------------------------------------------------------- CRAN release: 2020-04-02 * Fix tests for `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` ([#57](https://github.com/nacnudus/tidyxl/issues/57) ). tidyxl 1.0.4[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-104) ----------------------------------------------------------------------------- CRAN release: 2019-01-02 * Compatibility: imports cell data validation rules from files created by Office 365 ([#46](https://github.com/nacnudus/tidyxl/issues/46) ). tidyxl 1.0.3[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-103) ----------------------------------------------------------------------------- CRAN release: 2018-06-21 * Noticeably faster for large files. * Omission of blank cells with `include_blank_cells = FALSE` had a bug that returned blank cells as an empty row in the `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` data frame. tidyxl 1.0.2[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-102) ----------------------------------------------------------------------------- * Correctly constructs formulas where references are preceded by operators, e.g. `-F10` ([#26](https://github.com/nacnudus/tidyxl/issues/26) [@cablegui](https://github.com/cablegui) ). * No longer misinterprets date formats that use underscores `_` as dates when the underscore is followed by a date-ish character like `M` ([#24](https://github.com/nacnudus/tidyxl/issues/24) ). * Optionally omits blank cells with `include_blank_cells = FALSE` in `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` ([#25](https://github.com/nacnudus/tidyxl/issues/25) ). * Doesn’t crash reading files with certain colour themes ([#34](https://github.com/nacnudus/tidyxl/issues/34) [@dan-fahey](https://github.com/dan-fahey) ). tidyxl 1.0.1[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-101) ----------------------------------------------------------------------------- CRAN release: 2018-03-26 * Filetype checking is based on the [file signature](https://en.wikipedia.org/wiki/List_of_file_signatures) or “magic number”, rather than the filename extension. A new function `[maybe_xlsx()](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html) ` is provided for checking whether a file might be in the xlsx format. It is impossible to be sure from the magic number alone, because the magic numbers are either common to all zip files, or common to other Microsoft Office files (e.g. .doc, .ppt). * Fixed a CRAN warning. tidyxl 1.0.0[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-100) ----------------------------------------------------------------------------- CRAN release: 2017-11-25 ### New features[](https://nacnudus.github.io/tidyxl/news/index.html#new-features-1-0-0) * `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` and `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` replace `[tidy_xlsx()](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) `, which has been deprecated. `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` returns a single data frame of all the cells in scope (the whole workbook, or chosen sheets), rather than a list of separate data frames for each sheet. `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` performs orders of magnitude faster. * `[xlsx_validation()](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) ` imports validation rules from cells that restrict data input, such as cells that require a selection from a drop-down list. See the [vignette](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html) `[vignette("data-validation-rules", package = "tidyxl")](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html) `. * `[xlsx_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) ` imports defined names (aka named ranges/formulas), which can be used to filter for particular ranges of cells by name. Use `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` to filter for ones that are named ranges, and then read [joining rules to cells](https://nacnudus.github.io/tidyxl/articles/data-validation-rules.html#joining-rules-to-cells) for how to join cell ranges to cell addresses. This will become easier in a future release. * `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` checks whether a formula is simply ranges of cells. * `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` tokenises formulas. This is useful for detecting spreadsheet smells like embedded constants and deep nesting. There is a [demo Shiny app](https://duncan-garmonsway.shinyapps.io/xlex/) , and a [vignette](https://nacnudus.github.io/tidyxl/articles/smells.html) `[vignette("smells", package = "tidyxl")](https://nacnudus.github.io/tidyxl/articles/smells.html) `. A vector of Excel function names `excel_functions` can be used to separated built-in functions from custom functions. More experimental features will be implemented in the off-CRAN package [lexl](https://nacnudus.github.io/lexl/) before becoming part of tidyxl. * `xlsx_cells()$character_formatted` is a new column for the in-cell formatting of text ([#5](https://github.com/nacnudus/tidyxl/issues/5) ). This is for when different parts of text in a single cell have been formatted differently from one another. * `[is_date_format()](https://nacnudus.github.io/tidyxl/reference/is_date_format.html) ` checks whether a number format string is a date format. This is useful if a cell formula contains a number formatting string (e.g. `TEXT(45678,"yyyy")`), and you need to know that the constant 45678 is a date in order to recover it at full resolution (rather than parsing the character output “2025” as a year). * `[xlsx_color_theme()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) ` and it’s British alias `[xlsx_colour_theme()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) ` returns the theme colour palette used in a file. This is useful to monitor use of a corporate standard theme. * `xlsx_color_standard` and it’s British alias `xlsx_colour_standard` are data frames of the standard Excel palette (`red`, `blue`, etc.). * Shared formulas are propogated to all the cells that use the same formula definition. Relative cell references are handled, so that the formula `=A1*2` in cell `B1` becomes `=A2*2` in cell `B2` (for more details see issue [#7](https://github.com/nacnudus/tidyxl/issues/7) ). * Formatting of alignment and cell protection is returned ([#20](https://github.com/nacnudus/tidyxl/issues/20) ). ### Breaking changes and deprecations[](https://nacnudus.github.io/tidyxl/news/index.html#breaking-changes-and-deprecations-1-0-0) * `[tidy_xlsx()](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) ` has been deprecated in favour of `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) `, which returns a data frame of all the cells in the workbook (or in the requested sheets), and `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `, which returns a lookup list of cell formats. * In `[tidy_xlsx()](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) ` and one of it’s replacments `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` * the column `content` has been replaced by `is_blank`, a logical value indicating whether the cell contains data. Please replace `!is.na(content)` with `!is_blank` to filter out blank cells (ones with formatting but no value). * the column `formula_type` has been replaced by `is_array`, a logical value indicating whether the cell’s formula is an array formula or not. In Excel array formulas are represented visually by being surrounded by curly braces `[{}](https://rdrr.io/r/base/Paren.html) `. * The order of columns has been changed so that the more useful columns are visible in narrow consoles. * in `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` and `[tidy_xlsx()](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) `, theme colours are given by name rather than by number, e.g. `"accent6"` instead of `4`. ### Minor fixes and improvements[](https://nacnudus.github.io/tidyxl/news/index.html#minor-fixes-and-improvements-1-0-0) * Certain unusual custom number formats that specify colours (e.g. `"[Cyan]0%"`) are no longer mis-detect as dates ([#21](https://github.com/nacnudus/tidyxl/issues/21) ). `[is_date_format()](https://nacnudus.github.io/tidyxl/reference/is_date_format.html) ` tests whether a number format is a date format. * `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` is now thoroughly tested, and several relatively minor bugs fixed. For example, `xlsx_formats(path)$local$fill$patternFill$patternType` consistently returns `NA` and never `"none"` when a pattern fill has not been set, and escape-backslashes are consistently omitted from numFmts. ### New dependency[](https://nacnudus.github.io/tidyxl/news/index.html#new-dependency-1-0-0) `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) `, `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` and the handling of relative references in shared formulas requires a dependency on the [piton](https://cran.r-project.org/package=piton) package, which wraps the [PEGTL](https://github.com/taocpp/PEGTL) C++ parser generator. tidyxl 0.2.3[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-023) ----------------------------------------------------------------------------- CRAN release: 2017-07-08 * Imports dates on or before 1900-02-28 dates correctly, and only warns on the impossible date 1900-02-29, returning NA (following [readxl](https://github.com/tidyverse/readxl/commit/c9a54ae9ce0394808f6d22e8ef1a7a647b2d92bb) ). * Fixes subsecond rounding (following [readxl](https://github.com/tidyverse/readxl/commit/63ef215f57322dd5d7a27799a2a3fe463bd39fc7) (fixes [#14](https://github.com/nacnudus/tidyxl/issues/14) )) * Imports styles correctly from LibreOffice files (interprets ‘true’ and ‘false’ as booleans, as well as the 0 and 1 used by Microsoft Excel, and defaults to ‘true’ when not present, e.g. applyFill) * Fixes a bug that caused some LibreOffice files to crash R, when styles were declared with gaps in the sequence of xfIds. * Imports comments attached to blank cells (fixes [#10](https://github.com/nacnudus/tidyxl/issues/10) ) * Includes the sheet and cell address in warnings of impossible datetimes from 1900-02-29. tidyxl 0.2.1.9000[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-0219000) -------------------------------------------------------------------------------------- * Checks the value of the `date1904` attribute for `"false"` or `"1"` to support files created by the `openxlsx` package ([#8](https://github.com/nacnudus/tidyxl/issues/8) ). * Fixed a bug that only imported the first line of multiline comments ([#9](https://github.com/nacnudus/tidyxl/issues/9) ). * Encodes cell and comment text as UTF-8 ([#12](https://github.com/nacnudus/tidyxl/issues/12) ). * Finds worksheets more reliably in files not created by Excel (part of [#13](https://github.com/nacnudus/tidyxl/issues/13) ). * Falls back to default styles when none defined ([#13](https://github.com/nacnudus/tidyxl/issues/13) ). * Imports dates with greater precision (part of [#14](https://github.com/nacnudus/tidyxl/issues/14) ). * Fixed the order of worksheets ([#15](https://github.com/nacnudus/tidyxl/issues/15) ) tidyxl 0.2.1[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-021) ----------------------------------------------------------------------------- CRAN release: 2017-01-29 * Fixed a major bug: dates were parsed incorrectly because the offsets for the 1900 and 1904 systems were the wrong way around. * Added a warning when dates suffer from the Excel off-by-one bug. * Fixed a bug that misinterpreted some number formats as dates (confused by the ‘d’ in ‘\[Red\]’, which looks like a ‘d’ in ‘d/m/y’.) * Added support for xlsx files created by Gnumeric (a single, unnamed cell formatting style). * Fixed the checkUserInterrupt to work every 1000th cell. * Added many tests. * Removed lots of redundant code. tidyxl 0.2.0[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-020) ----------------------------------------------------------------------------- * Breaking change: Look up style formats by style name (e.g. `"Normal"`) instead of by index integer. All the vectors under `x$formats$style` are named according to the style names. `x$data$sheet$style_format_id` has been renamed to `x$data$sheet$style_format` and its type changed from integer (index into style formats) to character (still an index, but looking up the named vectors by name). There are examples in the README and vignette. * Simplified some variable names in the C++ code. tidyxl 0.1.1[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-011) ----------------------------------------------------------------------------- * Fixed many compilation errors on Travis/AppVeyor/r-hub * Many small non-breaking changes tidyxl 0.1.0[](https://nacnudus.github.io/tidyxl/news/index.html#tidyxl-010) ----------------------------------------------------------------------------- * Added a `NEWS.md` file to track changes to the package. --- # Function reference • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/index.html#main) ![](https://nacnudus.github.io/tidyxl/reference/index.html) Function reference ================== All functions[](https://nacnudus.github.io/tidyxl/reference/index.html#all-functions) -------------------------------------------------------------------------------------- `[excel_functions](https://nacnudus.github.io/tidyxl/reference/excel_functions.html) ` Names of all Excel functions `[is_date_format()](https://nacnudus.github.io/tidyxl/reference/is_date_format.html) ` Test that Excel number formats are date formats `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` Test that Excel formulas are ranges `[maybe_xlsx()](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html) ` Determine file format `[tidy_xlsx()](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) ` Import xlsx (Excel) cell contents into a tidy structure. `[tidyxl](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html) ` `[tidyxl-package](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html) ` tidyxl: Import xlsx (Excel) spreadsheet data and formatting into tidy structures. `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` Parse xlsx (Excel) formulas into tokens `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` Import xlsx (Excel) cell contents into a tidy structure. `[xlsx_color_standard](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html) ` `[xlsx_colour_standard](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html) ` Names and RGB values of Excel standard colours `[xlsx_color_theme()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) ` `[xlsx_colour_theme()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) ` Import theme color definitions from xlsx (Excel) files `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` Import xlsx (Excel) formatting definitions. `[xlsx_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) ` Import named formulas from xlsx (Excel) files `[xlsx_sheet_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html) ` List sheets in an xlsx (Excel) file `[xlsx_validation()](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) ` Import data validation rules of cells in xlsx (Excel) files --- # Parse xlsx (Excel) formulas into tokens — xlex • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlex.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlex.html) Parse xlsx (Excel) formulas into tokens ======================================= Source: [`R/xlex.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlex.R) `xlex.Rd` `xlex` takes an Excel formula and separates it into tokens. The name is a bad pun on 'Excel' and 'lexer'. It returns a dataframe, one row per token, giving the token itself, its type (e.g. `number`, or `error`), and its level. The level is a number to show the depth of a token within nested function calls. The token `A2` in the formula `IF(A1=1,A2,MAX(A3,A4))` is at level 1. Tokens `A3` and `A4` are at level 2. The token `IF` is at level 0, which is the outermost level. The output isn't enough to enable computation or validation of formulas, but it is enough to investigate the structure of formulas and spreadsheets. It has been tested on millions of formulas in the Enron corpus. Usage[](https://nacnudus.github.io/tidyxl/reference/xlex.html#ref-usage) ------------------------------------------------------------------------- xlex(x) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlex.html#arguments) ----------------------------------------------------------------------------- x Character vector of length 1, giving the formula. Value[](https://nacnudus.github.io/tidyxl/reference/xlex.html#value) --------------------------------------------------------------------- A data frame (a tibble, if you use the tidyverse) one row per token, giving the token itself, its type (e.g. `number`, or `error`), and its level. A class attribute `xlex` is added, so that the `[base::print()](https://rdrr.io/r/base/print.html) ` generic can be specialised to print the tree prettily. Details[](https://nacnudus.github.io/tidyxl/reference/xlex.html#details) ------------------------------------------------------------------------- The different types of tokens are: * `ref` A cell reference/address e.g. `A1` or `$B2:C$14`. * `sheet`A sheet name, e.g. `Sheet1!` or `'My Sheet'!`. If the sheet is from a different file, then the file is included in this token -- usually it has been normalized to the form `[0]`. * `name` A named range, or more properly a named formula. * `function` An Excel or user-defined function, e.g. `MAX` or `_xll.MY_CUSTOM_FUNCTION`. A complete list of official Excel functions is available in the vector `[excel_functions](https://nacnudus.github.io/tidyxl/reference/excel_functions.html) `. * `error` An error, e.g. `#N/A` or `#REF!`. * `bool` `TRUE` or `FALSE` -- note that there are also functions `TRUE()` and `FALSE()`. * `number` All forms of numbers, e.g. `1`, `1.1`, `-1`, `1.2E3`. * `text` Strings inside double quotes, e.g. `"Hello, World!"`. * `operator` The usual infix operators, `+`, `-`, `*`, `/`, `^`, `<`, `<=`, `<>`, etc. and also the range operator `:` when it is used with ranges that aren't cell addresses, e.g. `INDEX(something):A1`. The union operator `,` is the same symbol that is used to separate function arguments and array columns, so it is only tagged `operator` when it is inside parentheses that are not function parentheses or array curly braces (see the examples). * `paren_open` An open parenthesis `(` indicating an increase in the level of nesting, but not directly enclosing function arguments. * `paren_close` As `open`, but reducing the level of nesting. * `open_array` An open curly brace '{' indicating the start of an array of constants, and an increase in the level of nesting. * `close_array` As `open_array`, but ending the array of constants * `fun_open` An open parenthesis `(` immediately after a function name, directly enclosing the function arguments. * `fun_close` As `fun_open` but immediately after the function arguments. * `separator` A comma `,` separating function arguments or array columns, or a semicolon `;` separating array rows. * `DDE` A call to a Dynamic Data Exchange server, usually normalized to the form `[1]!'DDE_parameter=1'`, but the full form is `'ABCD'|'EFGH'!'IJKL'`. * `space` Some old files haven't stripped formulas of meaningless spaces. They are returned as `space` tokens so that the original formula can always be reconstructed by concatenating all tokens. * `other` If you see this, then something has gone wrong -- please report it at https://github.com/nacnudus/tidyxl/issues with a reproducible example (e.g. using the reprex package). Every part of the original formula is returned as a token, so the original formula can be reconstructed by concatenating the tokens. If that doesn't work, please report it at https://github.com/nacnudus/tidyxl/issues with a reproducible example (e.g. using the reprex package). The XLParser project was a great help in creating the grammar. https://github.com/spreadsheetlab/XLParser. Examples[](https://nacnudus.github.io/tidyxl/reference/xlex.html#ref-examples) ------------------------------------------------------------------------------- # All explicit cell references/addresses are returned as a single 'ref' token. xlex("A1") #> root #> °-- A1 ref xlex("A$1") #> root #> °-- A$1 ref xlex("$A1") #> root #> °-- $A1 ref xlex("$A$1") #> root #> °-- $A$1 ref xlex("A1:B2") #> root #> °-- A1:B2 ref xlex("1:1") # Whole row #> root #> °-- 1:1 ref xlex("A:B") # Whole column #> root #> °-- A:B ref # If one part of an address is a name or a function, then the colon ':' is # regarded as a 'range operator', so is tagged 'operator'. xlex("A1:SOME.NAME") #> root #> ¦-- A1 ref #> ¦-- : operator #> °-- SOME.NAME name xlex("SOME_FUNCTION():B2") #> root #> ¦-- SOME_FUNCTION function #> ¦-- ( fun_open #> ¦-- ) fun_close #> ¦-- : operator #> °-- B2 ref xlex("SOME_FUNCTION():SOME.NAME") #> root #> ¦-- SOME_FUNCTION function #> ¦-- ( fun_open #> ¦-- ) fun_close #> ¦-- : operator #> °-- SOME.NAME name # Sheet names are recognised by the terminal exclamation mark '!'. xlex("Sheet1!A1") #> root #> ¦-- Sheet1! sheet #> °-- A1 ref xlex("'Sheet 1'!A1") # Quoted names may contain some punctuation #> root #> ¦-- 'Sheet 1'! sheet #> °-- A1 ref xlex("'It''s a sheet'!A1") # Quotes are escaped by doubling #> root #> ¦-- 'It''s a sheet'! sheet #> °-- A1 ref # Sheets can be ranged together in so-called 'three-dimensional formulas'. # Both sheets are returned in a single 'sheet' token. xlex("Sheet1:Sheet2!A1") #> root #> ¦-- Sheet1:Sheet2! sheet #> °-- A1 ref xlex("'Sheet 1:Sheet 2'!A1") # Quotes surround both (rather than each) sheet #> root #> ¦-- 'Sheet 1:Sheet 2'! sheet #> °-- A1 ref # Sheets from other files are prefixed by the filename, which Excel # normalizes the filenames into indexes. Either way, xlex() includes the # file/index in the 'sheet' token. xlex("[1]Sheet1!A1") #> root #> ¦-- [1]Sheet1! sheet #> °-- A1 ref xlex("'[1]Sheet 1'!A1") # Quotes surround both the file index and the sheet #> root #> ¦-- '[1]Sheet 1'! sheet #> °-- A1 ref xlex("'C:\\My Documents\\[file.xlsx]Sheet1'!A1") #> root #> ¦-- 'C:\My Documents\[file.xlsx]Sheet1'! sheet #> °-- A1 ref # Function names are recognised by the terminal open-parenthesis '('. There # is no distinction between custom functions and built-in Excel functions. # The open-parenthesis is tagged 'fun_open', and the corresponding # close-parenthesis at the end of the arguments is tagged 'fun_close'. xlex("MAX(1,2)") #> root #> ¦-- MAX function #> °-- ( fun_open #> ¦-- 1 number #> ¦-- , separator #> °-- 2 number #> °-- ) fun_close xlex("_xll.MY_CUSTOM_FUNCTION()") #> root #> ¦-- _xll.MY_CUSTOM_FUNCTION function #> ¦-- ( fun_open #> °-- ) fun_close # Named ranges (properly called 'named formulas') are a last resort after # attempting to match a function (ending in an open parenthesis '(') or a # sheet (ending in an exclamation mark '!') xlex("MY_NAMED_RANGE") #> root #> °-- MY_NAMED_RANGE name # Some cell addresses/references, functions and names can look alike, but # xlex() should always make the right choice. xlex("XFD1") # A cell in the maximum column in Excel #> root #> °-- XFD1 ref xlex("XFE1") # Beyond the maximum column, must be a named range/formula #> root #> °-- XFE1 name xlex("A1048576") # A cell in the maximum row in Excel #> root #> °-- A1048576 ref xlex("A1048577") # Beyond the maximum row, must be a named range/formula #> root #> °-- A1048577 name xlex("LOG10") # A cell address #> root #> °-- LOG10 ref xlex("LOG10()") # A log function #> root #> ¦-- LOG10 function #> ¦-- ( fun_open #> °-- ) fun_close xlex("LOG:LOG") # The whole column 'LOG' #> root #> °-- LOG:LOG ref xlex("LOG") # Not a cell address, must be a named range/formula #> root #> °-- LOG name xlex("LOG()") # Another log function #> root #> ¦-- LOG function #> ¦-- ( fun_open #> °-- ) fun_close xlex("A1.2!A1") # A sheet called 'A1.2' #> root #> ¦-- A1.2! sheet #> °-- A1 ref # Text is surrounded by double-quotes. xlex("\"Some text\"") #> root #> °-- "Some text" text xlex("\"Some \"\"text\"\"\"") # Double-quotes within text are escaped by #> root #> °-- "Some ""text""" text # Numbers are signed where it makes sense, and can be scientific xlex("1") #> root #> °-- 1 number xlex("1.2") #> root #> °-- 1.2 number xlex("-1") #> root #> °-- -1 number xlex("-1-1") #> root #> ¦-- -1 number #> ¦-- - operator #> °-- 1 number xlex("-1+-1") #> root #> ¦-- -1 number #> ¦-- + operator #> °-- -1 number xlex("MAX(-1-1)") #> root #> ¦-- MAX function #> °-- ( fun_open #> ¦-- - operator #> ¦-- 1 number #> ¦-- - operator #> °-- 1 number #> °-- ) fun_close xlex("-1.2E-3") #> root #> °-- -1.2E-3 number # Booleans can be constants or functions, and names can look like booleans, # but xlex() should always make the right choice. xlex("TRUE") #> root #> °-- TRUE bool xlex("TRUEISH") #> root #> °-- TRUEISH name xlex("TRUE!A1") #> root #> ¦-- TRUE! sheet #> °-- A1 ref xlex("TRUE()") #> root #> ¦-- TRUE function #> ¦-- ( fun_open #> °-- ) fun_close # Errors are tagged 'error' xlex("#DIV/0!") #> root #> °-- #DIV/0! error xlex("#N/A") #> root #> °-- #N/A error xlex("#NAME?") #> root #> °-- #NAME? error xlex("#NULL!") #> root #> °-- #NULL! error xlex("#NUM!") #> root #> °-- #NUM! error xlex("#REF!") #> root #> °-- #REF! error xlex("#VALUE!") #> root #> °-- #VALUE! error # Operators with more than one character are treated as single tokens xlex("1<>2") #> root #> ¦-- 1 number #> ¦-- <> operator #> °-- 2 number xlex("1<=2") #> root #> ¦-- 1 number #> ¦-- <= operator #> °-- 2 number xlex("1<2") #> root #> ¦-- 1 number #> ¦-- < operator #> °-- 2 number xlex("1=2") #> root #> ¦-- 1 number #> ¦-- = operator #> °-- 2 number xlex("1&2") #> root #> ¦-- 1 number #> ¦-- & operator #> °-- 2 number xlex("1 2") #> root #> ¦-- 1 number #> ¦-- operator #> °-- 2 number xlex("(1,2)") #> root #> °-- ( paren_open #> ¦-- 1 number #> ¦-- , operator #> °-- 2 number #> °-- ) paren_close xlex("1%") # postfix operator #> root #> ¦-- 1 number #> °-- % operator # The union operator is a comma ',', which is the same symbol that is used # to separate function arguments or array columns. It is tagged 'operator' # only when it is inside parentheses that are not function parentheses or # array curly braces. The curly braces are tagged 'array_open' and # 'array_close'. tidyxl::xlex("A1,B2") # invalid formula, defaults to 'union' to avoid a crash #> root #> ¦-- A1 ref #> ¦-- , operator #> °-- B2 ref tidyxl::xlex("(A1,B2)") #> root #> °-- ( paren_open #> ¦-- A1 ref #> ¦-- , operator #> °-- B2 ref #> °-- ) paren_close tidyxl::xlex("MAX(A1,B2)") #> root #> ¦-- MAX function #> °-- ( fun_open #> ¦-- A1 ref #> ¦-- , separator #> °-- B2 ref #> °-- ) fun_close tidyxl::xlex("SMALL((A1,B2),1)") #> root #> ¦-- SMALL function #> °-- ( fun_open #> °-- ( paren_open #> ¦-- A1 ref #> ¦-- , operator #> °-- B2 ref #> ¦-- ) paren_close #> ¦-- , separator #> °-- 1 number #> °-- ) fun_close # Function arguments are separated by commas ',', which are tagged # 'separator'. xlex("MAX(1,2)") #> root #> ¦-- MAX function #> °-- ( fun_open #> ¦-- 1 number #> ¦-- , separator #> °-- 2 number #> °-- ) fun_close # Nested functions are marked by an increase in the 'level'. The level # increases inside parentheses, rather than at the parentheses. Curly # braces, for arrays, have the same behaviour, as do subexpressions inside # ordinary parenthesis, tagged 'paren_open' and 'paren_close'. To see the # levels explicitly (rather than by the pretty printing), print as a normal # data frame or tibble by specifying `pretty = FALSE`. # class with as.data.frame. xlex("MAX(MIN(1,2),3)") #> root #> ¦-- MAX function #> °-- ( fun_open #> ¦-- MIN function #> °-- ( fun_open #> ¦-- 1 number #> ¦-- , separator #> °-- 2 number #> ¦-- ) fun_close #> ¦-- , separator #> °-- 3 number #> °-- ) fun_close xlex("{1,2;3,4}") #> root #> °-- { open_array #> ¦-- 1 number #> ¦-- , separator #> ¦-- 2 number #> ¦-- ; separator #> ¦-- 3 number #> ¦-- , separator #> °-- 4 number #> °-- } close_array xlex("1*(2+3)") #> root #> ¦-- 1 number #> ¦-- * operator #> °-- ( paren_open #> ¦-- 2 number #> ¦-- + operator #> °-- 3 number #> °-- ) paren_close print(xlex("1*(2+3)"), pretty = FALSE) #> # A tibble: 7 × 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator * #> 3 0 paren_open ( #> 4 1 number 2 #> 5 1 operator + #> 6 1 number 3 #> 7 0 paren_close ) # Arrays are marked by opening and closing curly braces, with comma ',' # between columns, and semicolons ';' between rows Commas and semicolons are # both tagged 'separator'. Arrays contain only constants, which are # booleans, numbers, text, and errors. xlex("MAX({1,2;3,4})") #> root #> ¦-- MAX function #> °-- ( fun_open #> °-- { open_array #> ¦-- 1 number #> ¦-- , separator #> ¦-- 2 number #> ¦-- ; separator #> ¦-- 3 number #> ¦-- , separator #> °-- 4 number #> °-- } close_array #> °-- ) fun_close xlex("=MAX({-1E-2,TRUE;#N/A,\"Hello, World!\"})") #> root #> ¦-- = operator #> ¦-- MAX function #> °-- ( fun_open #> °-- { open_array #> ¦-- -1E-2 number #> ¦-- , separator #> ¦-- TRUE bool #> ¦-- ; separator #> ¦-- #N/A error #> ¦-- , separator #> °-- "Hello, World!" text #> °-- } close_array #> °-- ) fun_close # Structured references are surrounded by square brackets. Subexpressions # may also be surrounded by square brackets, but xlex() returns the whole # expression in a single 'structured_ref' token. xlex("[@col2]") #> root #> °-- [@col2] structured_ref xlex("SUM([col22])") #> root #> ¦-- SUM function #> °-- ( fun_open #> °-- [col22] structured_ref #> °-- ) fun_close xlex("Table1[col1]") #> root #> °-- Table1[col1] structured_ref xlex("Table1[[col1]:[col2]]") #> root #> °-- Table1[[col1]:[col2]] structured_ref xlex("Table1[#Headers]") #> root #> °-- Table1[#Headers] structured_ref xlex("Table1[[#Headers],[col1]]") #> root #> °-- Table1[[#Headers],[col1]] structured_ref xlex("Table1[[#Headers],[col1]:[col2]]") #> root #> °-- Table1[[#Headers],[col1]:[col2]] structured_ref # DDE calls (Dynamic Data Exchange) are normalized by Excel into indexes. # Either way, xlex() includes everything in one token. xlex("[1]!'DDE_parameter=1'") #> root #> °-- [1]!'DDE_parameter=1' DDE xlex("'Quote'|'NYSE'!ZAXX") #> root #> °-- 'Quote'|'NYSE'!ZAXX DDE # Meaningless spaces that appear in some old files are returned as 'space' # tokens, so that the original formula can still be recovered by # concatenating all the tokens. Spaces between function names and their open # parenthesis have not been observed, so are not permitted. xlex(" MAX( A1 ) ") #> root #> ¦-- operator #> ¦-- MAX function #> °-- ( fun_open #> ¦-- operator #> ¦-- A1 ref #> °-- operator #> ¦-- ) fun_close #> °-- operator # print.xlex() invisibly returns the original argument, so that it can be # used in magrittr pipelines. str(print(xlex("ROUND(A1*2"))) #> root #> ¦-- ROUND function #> °-- ( fun_open #> ¦-- A1 ref #> ¦-- * operator #> °-- 2 number #> xlex [5 × 3] (S3: xlex/tbl_df/tbl/data.frame) #> $ level: int [1:5] 0 0 1 1 1 #> $ type : chr [1:5] "function" "fun_open" "ref" "operator" ... #> $ token: chr [1:5] "ROUND" "(" "A1" "*" ... --- # Import data validation rules of cells in xlsx (Excel) files — xlsx_validation • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) Import data validation rules of cells in xlsx (Excel) files =========================================================== Source: [`R/xlsx_validation.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_validation.R) `xlsx_validation.Rd` `xlsx_validation()` returns the data validation rules applied to cells in xlsx (Excel) files. Data validation rules control what constants can be entered into a cell, e.g. any whole number between 0 and 9, or one of several values from another part of the spreadsheet. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html#ref-usage) ------------------------------------------------------------------------------------ xlsx_validation(path, sheets = NA) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html#arguments) ---------------------------------------------------------------------------------------- path Path to the xlsx file. sheets Sheets to read. Either a character vector (the names of the sheets), an integer vector (the positions of the sheets), or NA (default, all sheets). Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html#value) -------------------------------------------------------------------------------- A data frame with the following columns. * `sheet` The worksheet that a validation rule cell is from. * `ref` Comma-delimited cell addresses to which the rules apply, e.g. `A106` or A115,A121:A122\`. * `type Data type of input, one of` whole`,` decimal`,` list`,` date`,` time`,` textLength`,` custom`, and` whole\`. * `operator` Unless `type` is `list` or `custom`, then `operator` is one of `between`, `notBetween`, `equal`, `notEqual`, `greaterThan`, `lessThan`, `greaterThanOrEqual`, `lessthanOrEqual`. * `formula1` If `type` is `list`, then a range of cells whose values are allowed by the rule. If `type` is `custom`, then a formula to determine allowable values. Otherwise, a cell address or constant, coerced to character. Dates and times are formatted like "2017-01-27 13:30:45". Times without dates are formatted like "13:30:45". * `formula2` If `operator` is `between` or `notBetween`, then a cell address or constant as with formula1, otherwise NA. * `allow_blank` Boolean, whether or not the rule allows blanks. * `show_input_message` Boolean, whether or not the rule shows a message when the user begins entering a value. * `prompt_title` Text to appear in the title bar of a popup message box when the user begins entering a value. * `prompt_body` Text to appear in a popup message box when the user begins entering a value. When `NA`, then some default text is shown. * `show_error_message` Boolean, whether or not the rule shows a message when the user has entered a forbidden value. When `NA`, then some default text is shown. * `error_title` Text to appear in the title bar of a popup message box when the user enters a forbidden value. When `NA`, then some default text is shown. * `error_body` Text to appear in a popup message box when the user enters a forbidden value. When `NA`, then some default text is shown. * `error_symbol` Name of a symbol to appear in the popup error message when the user enters a forbidden value. Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html#ref-examples) ------------------------------------------------------------------------------------------ examples <- system.file("extdata/examples.xlsx", package = "tidyxl") xlsx_validation(examples) #> # A tibble: 15 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE #> 5 Sheet1 A112 text… between 0 9 TRUE TRUE #> 6 Sheet1 A114 whole notBetw… 0 9 TRUE TRUE #> 7 Sheet1 A115,… whole equal 0 NA TRUE TRUE #> 8 Sheet1 A116 whole notEqual 0 NA TRUE TRUE #> 9 Sheet1 A117 whole greater… 0 NA TRUE TRUE #> 10 Sheet1 A119 whole greater… 0 NA TRUE TRUE #> 11 Sheet1 A120 whole lessTha… 0 NA TRUE TRUE #> 12 Sheet1 A118 whole lessThan 0 NA TRUE TRUE #> 13 Sheet1 A107 deci… notBetw… 0 9 FALSE FALSE #> 14 Sheet1 A113 cust… NA A113<=L… NA TRUE TRUE #> 15 Sheet1 A109 list NA $B$108 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol xlsx_validation(examples, 1) #> # A tibble: 15 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE #> 5 Sheet1 A112 text… between 0 9 TRUE TRUE #> 6 Sheet1 A114 whole notBetw… 0 9 TRUE TRUE #> 7 Sheet1 A115,… whole equal 0 NA TRUE TRUE #> 8 Sheet1 A116 whole notEqual 0 NA TRUE TRUE #> 9 Sheet1 A117 whole greater… 0 NA TRUE TRUE #> 10 Sheet1 A119 whole greater… 0 NA TRUE TRUE #> 11 Sheet1 A120 whole lessTha… 0 NA TRUE TRUE #> 12 Sheet1 A118 whole lessThan 0 NA TRUE TRUE #> 13 Sheet1 A107 deci… notBetw… 0 9 FALSE FALSE #> 14 Sheet1 A113 cust… NA A113<=L… NA TRUE TRUE #> 15 Sheet1 A109 list NA $B$108 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol xlsx_validation(examples, "Sheet1") #> # A tibble: 15 × 14 #> sheet ref type operator formula1 formula2 allow_blank show_input_message #> #> 1 Sheet1 A106 whole between 0 9 TRUE TRUE #> 2 Sheet1 A108 list NA $B$108 NA TRUE TRUE #> 3 Sheet1 A110 date between 2017-01… 2017-01… TRUE TRUE #> 4 Sheet1 A111 time between 00:00:00 09:00:00 TRUE TRUE #> 5 Sheet1 A112 text… between 0 9 TRUE TRUE #> 6 Sheet1 A114 whole notBetw… 0 9 TRUE TRUE #> 7 Sheet1 A115,… whole equal 0 NA TRUE TRUE #> 8 Sheet1 A116 whole notEqual 0 NA TRUE TRUE #> 9 Sheet1 A117 whole greater… 0 NA TRUE TRUE #> 10 Sheet1 A119 whole greater… 0 NA TRUE TRUE #> 11 Sheet1 A120 whole lessTha… 0 NA TRUE TRUE #> 12 Sheet1 A118 whole lessThan 0 NA TRUE TRUE #> 13 Sheet1 A107 deci… notBetw… 0 9 FALSE FALSE #> 14 Sheet1 A113 cust… NA A113<=L… NA TRUE TRUE #> 15 Sheet1 A109 list NA $B$108 NA TRUE TRUE #> # ℹ 6 more variables: prompt_title , prompt_body , #> # show_error_message , error_title , error_body , #> # error_symbol --- # Import xlsx (Excel) cell contents into a tidy structure. — tidy_xlsx • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#main) ![](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html) Import xlsx (Excel) cell contents into a tidy structure. ======================================================== Source: [`R/tidy_xlsx.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/tidy_xlsx.R) `tidy_xlsx.Rd` `tidy_xlsx()` is deprecated. Please use `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` or `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` instead. `tidy_xlsx()` imports data from spreadsheets without coercing it into a rectangle. Each cell is represented by a row in a data frame, giving the cell's address, contents, formula, height, width, and keys to look up the cell's formatting in an adjacent data structure within the list returned by this function. Usage[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#ref-usage) ------------------------------------------------------------------------------ tidy_xlsx(path, sheets = NA) Arguments[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#arguments) ---------------------------------------------------------------------------------- path Path to the xlsx file. sheets Sheets to read. Either a character vector (the names of the sheets), an integer vector (the positions of the sheets), or NA (default, all sheets). Value[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#value) -------------------------------------------------------------------------- A list of the data within each sheet (`$data`), and the formatting applied to each cell (`$formats`). Each sheet's data is returned as a data frames, one per sheet, by the sheet name. For example, the data in a sheet named 'My Worksheet' is in x$data$`My Worksheet`. Each data frame has the following columns: * `address` The cell address in A1 notation. * `row` The row number of a cell address (integer). * `col` The column number of a cell address (integer). * `is_blank` Whether or not the cell has a value * `data_type` The type of a cell, referring to the following columns: error, logical, numeric, date, character, blank. * `error` The error value of a cell. * `logical` The boolean value of a cell. * `numeric` The numeric value of a cell. * `date` The date value of a cell. * `character` The string value of a cell. * `character_formatted` A data frame of substrings and their individual formatting. * `formula` The formula in a cell (see 'Details'). * `is_array` Whether or not the formula is an array formula. * `formula_ref` The address of a range of cells group to which an array formula or shared formula applies (see 'Details'). * `formula_group` The formula group to which the cell belongs (see 'Details'). * `comment` The text of a comment attached to a cell. * `height` The height of a cell's row, in Excel's units. * `width` The width of a cell's column, in Excel's units. * `style_format` An index into a table of style formats `x$formats$style` (see 'Details'). * `local_format_id` An index into a table of local cell formats `x$formats$local` (see 'Details'). ### Formula[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#formula) When a cell has a formula, the value in the 'content' column is the result of the formula the last time it was evaluated. Certain groups of cells may share a formula that differs only by addresses referred to in the formula; such groups are identified by an index, the 'formula\_group'. The xlsx (Excel) file format only records the formula against one cell in any group. `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` propagates such formulas to the other cells in a group, making the necessary changes to relative addresses in the formula. Array formulas may also apply to a group of cells, identified by an address 'formula\_ref', but xlsx (Excel) file format only records the formula against one cell in the group. `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` propagates such formulas to the other cells in a group. Unlike shared formulas, no changes to addresses in array formulas are necessary. Formulas that refer to other workbooks currently do not name the workbooks directly, instead via indices such as `[1]`. It is planned to dereference these. ### Formatting[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#formatting) Cell formatting is returned in `x$formats`. There are two types or scopes of formatting: 'style' formatting, such as Excel's built-in styles 'normal', 'bad', etc., and 'local' formatting, which overrides particular elements of the style, e.g. by making it bold. Both types of are returned in `x$formats$style` and `x$formats$local`, with identical structures. To look up the local formatting of a given cell, take the cell's 'local\_format\_id' value (`x$data$Sheet1[1, "local_format_id"]`), and use it as an index into the format structure. E.g. to look up the font size, `x$formats$local$font$size[local_format_id]`. To see all available formats, type `str(x$formats$local)`. Colours may be recorded in any of three ways: a hexadecimal RGB string with or without alpha, an 'indexed' colour, and an index into a 'theme'. `tidy_xlsx` dereferences 'indexed' and 'theme' colours to their hexadecimal RGB string representation, and standardises all RGB strings to have an alpha channel in the first two characters. The 'index' and the 'theme' name are still provided. To filter by an RGB string, you could look up the RGB values in a spreadsheet program (e.g. Excel, LibreOffice, Gnumeric), and use the `[grDevices::rgb()](https://rdrr.io/r/grDevices/rgb.html) ` function to convert these to a hexadecimal string. Put the alpha value in first, e.g. A <- 1; R <- 0.5; G <- 0; B <- 0 rgb(A, R, G, B) # [1] "#FF800000" Strings can be formatted within a cell, so that a single cell can contain substrings with different formatting. This in-cell formatting is available in the column `character_formatted`, which is a list-column of data frames. Each row of each data frame describes a substring and its formatting. For cells without a character value, `character_formatted` is `NULL`, so for further processing you might need to filter out the `NULL`s first. Details[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#details) ------------------------------------------------------------------------------ A cell has two 'values': its content, and sometimes also a formula. It also has formatting applied at the 'style' level, which can be locally overridden. ### Content[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#content) Depending on the cell, the content may be a numeric value such as 365 or 365.25, it may represent a date/datetime in one of Excel's date/datetime systems, or it may be an index into an internal table of strings. `tidy_xlsx()` attempts to infer the correct data type of each cell, returning its value in the appropriate column (error, logical, numeric, date, character). In case this cleverness is unhelpful, the unparsed value and type information is available in the 'content' and 'type' columns. ### Formula[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#formula-1) When a cell has a formula, the value in the 'content' column is the result of the formula the last time it was evaluated. Certain groups of cells may share a formula that differs only by addresses referred to in the formula; such groups are identified by an index, the 'formula\_group'. The xlsx (Excel) file format only records the formula against one cell in any group, but `tidy_xlsx()` propagates the formula to all the cells in the group, making the necessary changes to relative addresses in the formula. Array formulas may also apply to a group of cells, identified by an address 'formula\_ref', but xlsx (Excel) file format only records the formula against one cell in the group. Unlike ordinary formulas, `tidy_xlsx()` does not propagate these to the other cells in the group. Formulas that refer to other workbooks currently do not name the workbooks directly, instead via indices such as `[1]`. It is planned to dereference these. ### Formatting[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#formatting-1) Cell formatting is returned in `x$formats`. There are two types of formatting: 'style' formatting, such as Excel's built-in styles 'normal', 'bad', etc., and 'local' formatting, which overrides the style. These are returned in `x$formats$style` and `x$formats$local`, with identical structures. To look up the local formatting of a given cell, take the cell's 'local\_format\_id' value (`x$data$Sheet1[1, "local_format_id"]`), and use it as an index into the format structure. E.g. to look up the font size, `x$formats$local$font$size[local_format_id]`. To see all available formats, type `str(x$formats$local)`. Examples[](https://nacnudus.github.io/tidyxl/reference/tidy_xlsx.html#ref-examples) ------------------------------------------------------------------------------------ if (FALSE) { examples <- system.file("extdata/examples.xlsx", package = "tidyxl") # All sheets str(tidy_xlsx(examples)$data) # Specific sheet either by position or by name str(tidy_xlsx(examples, 2)$data) str(tidy_xlsx(examples, "Sheet1")$data) # Data (cell values) x <- tidy_xlsx(examples) str(x$data$Sheet1) # Formatting str(x$formats$local) # The formats of particular cells can be retrieved like this: Sheet1 <- x$data$Sheet1 x$formats$style$font$bold[Sheet1$style_format] x$formats$local$font$bold[Sheet1$local_format_id] # To filter for cells of a particular format, first filter the formats to get # the relevant indices, and then filter the cells by those indices. bold_indices <- which(x$formats$local$font$bold) Sheet1[Sheet1$local_format_id %in% bold_indices, ] # In-cell formatting is available in the `character_formatted` column as a # data frame, one row per substring. tidy_xlsx(examples)$data$Sheet1$character_formatted[77] } --- # Import xlsx (Excel) cell contents into a tidy structure. — xlsx_cells • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) Import xlsx (Excel) cell contents into a tidy structure. ======================================================== Source: [`R/xlsx_cells.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_cells.R) `xlsx_cells.Rd` `xlsx_cells()` imports data from spreadsheets without coercing it into a rectangle. Each cell is represented by a row in a data frame, giving the cell's address, contents, formula, height, width, and keys to look up the cell's formatting in the return value of `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#ref-usage) ------------------------------------------------------------------------------- xlsx_cells( path, sheets = NA, check_filetype = TRUE, include_blank_cells = TRUE ) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#arguments) ----------------------------------------------------------------------------------- path Path to the xlsx file. sheets Sheets to read. Either a character vector (the names of the sheets), an integer vector (the positions of the sheets), or NA (default, all sheets). check\_filetype Logical. Whether to check that the filetype is xlsx (or xlsm) by looking at the file itself, rather than using the filename extension. include\_blank\_cells Logical. Whether to include cells that have no value or formula (but might have formatting or comments). Useful when a whole column of cells has been formatted, but most are empty. Try setting this to `FALSE` if a spreadsheet seems too large to load. Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#value) --------------------------------------------------------------------------- A data frame with the following columns. * `sheet` The worksheet that the cell is from. * `address` The cell address in A1 notation. * `row` The row number of a cell address (integer). * `col` The column number of a cell address (integer). * `is_blank` Whether or not the cell has a value * `content` Raw cell value before type conversion, useful for debugging. * `data_type` The type of a cell, referring to the following columns: error, logical, numeric, date, character, blank. * `error` The error value of a cell. * `logical` The boolean value of a cell. * `numeric` The numeric value of a cell. * `date` The date value of a cell. * `character` The string value of a cell. * `formula` The formula in a cell (see 'Details'). * `is_array` Whether or not the formula is an array formula. * `formula_ref` The address of a range of cells group to which an array formula or shared formula applies (see 'Details'). * `formula_group` The formula group to which the cell belongs (see 'Details'). * `comment` The text of a comment attached to a cell. * `height` The height of a cell's row, in Excel's units. * `width` The width of a cell's column, in Excel's units. * `row_outline_level` The outline level of a cells's row. * `col_outline_level` The outline level of a cells's column. * `style_format` An index into a table of style formats `x$formats$style` (see 'Details'). * `local_format_id` An index into a table of local cell formats `x$formats$local` (see 'Details'). Cell formatting is returned in `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `. There are two types or scopes of formatting: 'style' formatting, such as Excel's built-in styles 'normal', 'bad', etc., and 'local' formatting, which overrides particular elements of the style, e.g. by making it bold. Both types are returned, in the `$style` and `$local` sublists of `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `, with identical structures. To look up the local formatting of a given cell, take the cell's 'local\_format\_id' value (`my_cells$data$Sheet1[1, "local_format_id"]`), and use it as an index into the format structure. E.g. to look up the font size, `my_formats$local$font$size[local_format_id]`. To see all available formats, type `str(my_formats$local)`. Details[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#details) ------------------------------------------------------------------------------- A cell has two 'values': its content, and sometimes also a formula. It also has formatting applied at the 'style' level, which can be locally overridden. ### Content[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#content) Depending on the cell, the content may be a numeric value such as 365 or 365.25, it may represent a date/datetime in one of Excel's date/datetime systems, or it may be an index into an internal table of strings. `xlsx_cells()` attempts to infer the correct data type of each cell, returning its value in the appropriate column (error, logical, numeric, date, character). In case this cleverness is unhelpful, the unparsed value and type information is available in the 'content' and 'data\_type' columns. ### Formula[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#formula) When a cell has a formula, the value in the 'content' column is the result of the formula the last time it was evaluated. Certain groups of cells may share a formula that differs only by addresses referred to in the formula; such groups are identified by an index, the 'formula\_group'. The xlsx (Excel) file format only records the formula against one cell in any group. `xlsx_cells()` propagates such formulas to the other cells in a group, making the necessary changes to relative addresses in the formula. Array formulas may also apply to a group of cells, identified by an address 'formula\_ref', but xlsx (Excel) file format only records the formula against one cell in the group. `xlsx_cells()` propagates such formulas to the other cells in a group. Unlike shared formulas, no changes to addresses in array formulas are necessary. Formulas that refer to other workbooks currently do not name the workbooks directly, instead via indices such as `[1]`. It is planned to dereference these. ### Formatting[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#formatting) Cell formatting is returned by `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `. There are two types of formatting: 'style' formatting, such as Excel's built-in styles 'normal', 'bad', etc., and 'local' formatting, which overrides the style. These are returned in the `$style` and `$local` sublists of `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) `, with identical structures. To look up the local formatting of a given cell, take the cell's `local_format_id` value (`my_cells$Sheet1[1, "local_format_id"]`), and use it as an index into the format structure. E.g. to look up the font size, `my_formats$local$font$size[local_format_id]`. To see all available formats, type `str(my_formats$local)`. Strings can be formatted within a cell, so that a single cell can contain substrings with different formatting. This in-cell formatting is available in the column `character_formatted`, which is a list-column of data frames. Each row of each data frame describes a substring and its formatting. For cells without a character value, `character_formatted` is `NULL`, so for further processing you might need to filter out the `NULL`s first. Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html#ref-examples) ------------------------------------------------------------------------------------- examples <- system.file("extdata/examples.xlsx", package = "tidyxl") # All sheets str(xlsx_cells(examples)) #> tibble [586 × 24] (S3: tbl_df/tbl/data.frame) #> $ sheet : chr [1:586] "Sheet1" "Sheet1" "Sheet1" "Sheet1" ... #> $ address : chr [1:586] "A1" "B1" "A2" "B2" ... #> $ row : int [1:586] 1 1 2 2 3 3 4 4 5 5 ... #> $ col : int [1:586] 1 2 1 2 1 2 1 2 1 2 ... #> $ is_blank : logi [1:586] FALSE FALSE FALSE FALSE FALSE FALSE ... #> $ content : chr [1:586] "#DIV/0!" "56" "1" "54" ... #> $ data_type : chr [1:586] "error" "character" "logical" "character" ... #> $ error : chr [1:586] "#DIV/0!" NA NA NA ... #> $ logical : logi [1:586] NA NA TRUE NA FALSE NA ... #> $ numeric : num [1:586] NA NA NA NA NA ... #> $ date : POSIXct[1:586], format: NA NA ... #> $ character : chr [1:586] NA "Error" NA "Boolean (true)" ... #> $ character_formatted:List of 586 #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Error" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Boolean (true)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Boolean (false)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "integer" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "integer with higher-precision number formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "default date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Different built-in date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "custom date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "character" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "character" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "comment" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "comment with formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Currency format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Percent format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Scientific format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to logical" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to a number" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to date" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to a string" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "some formulas that are stored in groups" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "single-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "multi-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "multi-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula that refers to another file" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal' style but 'normal' has been modified from Excel's default to use Arial font" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "italic" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local italics applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local bold applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local underline applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local double underline applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bad" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'bad' style applied to this cell" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "badboldrightupindent" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'bad' style modified with bold font, right-and-up-aligned with an indent." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "custom style with custom date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "auto-font" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Automatic font colour" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "standardred" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'indexed' or standard red text" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "themeblue" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Blue text from the theme" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bluepattern" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Pattern but no background, blue from theme" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "stylealign" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Custom style 'stylealign' where the style is aligned to the centre." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [16 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:16] "in-cell" "bold" "italic" "underline" ... #> .. ..$ bold : logi [1:16] FALSE TRUE FALSE FALSE FALSE FALSE ... #> .. ..$ italic : logi [1:16] FALSE FALSE TRUE FALSE FALSE FALSE ... #> .. ..$ underline : chr [1:16] NA NA NA "single" ... #> .. ..$ strike : logi [1:16] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ vertAlign : chr [1:16] NA NA NA NA ... #> .. ..$ size : num [1:16] 0 0 0 0 0 0 0 0 0 0 ... #> .. ..$ color_rgb : chr [1:16] NA "FF000000" "FF000000" "FF000000" ... #> .. ..$ color_theme : int [1:16] NA 2 2 2 2 2 2 2 2 2 ... #> .. ..$ color_indexed: int [1:16] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ color_tint : num [1:16] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ font : chr [1:16] "Calibri" "Calibri" "Calibri" "Calibri" ... #> .. ..$ family : int [1:16] 2 2 2 2 2 2 2 2 2 2 ... #> .. ..$ scheme : chr [1:16] "minor" "minor" "minor" "minor" ... #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "in-cellbolditalicunderlineunderlinedoublesingleaccountingdoubleaccountingstrikethroughsuperscriptsubscriptredth"| __truncated__ #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "yellowfillunderline12arialstrikethroughsuperscript" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "More fancy inline-string formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bottomborderslant" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Slanted text" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "merged" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Cell merged into cell below" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "unprotected" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is not 'protected'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "unhidden" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is not 'hidden'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "locked" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is 'locked'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "themebackground1" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Text is formatted the colour 'Background1' from the theme, i.e. white." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [2 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:2] "in-cell-bold" "-not-bold" #> .. ..$ bold : logi [1:2] TRUE FALSE #> .. ..$ italic : logi [1:2] FALSE FALSE #> .. ..$ underline : chr [1:2] NA NA #> .. ..$ strike : logi [1:2] FALSE FALSE #> .. ..$ vertAlign : chr [1:2] NA NA #> .. ..$ size : num [1:2] 0 0 #> .. ..$ color_rgb : chr [1:2] "FF000000" NA #> .. ..$ color_theme : int [1:2] 2 NA #> .. ..$ color_indexed: int [1:2] NA NA #> .. ..$ color_tint : num [1:2] NA NA #> .. ..$ font : chr [1:2] "Calibri" "Arial" #> .. ..$ family : int [1:2] 2 2 #> .. ..$ scheme : chr [1:2] "minor" NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Bold applied in inline string (not at cell level)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [2 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:2] "in-cell-bold" "-not-bold" #> .. ..$ bold : logi [1:2] NA FALSE #> .. ..$ italic : logi [1:2] NA FALSE #> .. ..$ underline : chr [1:2] NA NA #> .. ..$ strike : logi [1:2] NA FALSE #> .. ..$ vertAlign : chr [1:2] NA NA #> .. ..$ size : num [1:2] NA 0 #> .. ..$ color_rgb : chr [1:2] NA NA #> .. ..$ color_theme : int [1:2] NA NA #> .. ..$ color_indexed: int [1:2] NA NA #> .. ..$ color_tint : num [1:2] NA NA #> .. ..$ font : chr [1:2] NA "Arial" #> .. ..$ family : int [1:2] NA 2 #> .. ..$ scheme : chr [1:2] NA NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Bold applied at cell level" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "diagonal-up" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Diagonal-up border" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> .. [list output truncated] #> $ formula : chr [1:586] "1/0" NA NA NA ... #> $ is_array : logi [1:586] FALSE FALSE FALSE FALSE FALSE FALSE ... #> $ formula_ref : chr [1:586] NA NA NA NA ... #> $ formula_group : int [1:586] NA NA NA NA NA NA NA NA NA NA ... #> $ comment : chr [1:586] NA NA NA NA ... #> $ height : num [1:586] 14.2 14.2 14.2 14.2 14.2 ... #> $ width : num [1:586] 19.4 68 19.4 68 19.4 ... #> $ row_outline_level : num [1:586] 1 1 1 1 1 1 1 1 1 1 ... #> $ col_outline_level : num [1:586] 1 1 1 1 1 1 1 1 1 1 ... #> $ style_format : chr [1:586] "Normal" "Normal" "Normal" "Normal" ... #> $ local_format_id : int [1:586] 1 1 1 1 1 1 1 1 5 1 ... # Specific sheet either by position or by name str(xlsx_cells(examples, 2)) #> tibble [2 × 24] (S3: tbl_df/tbl/data.frame) #> $ sheet : chr [1:2] "1~`!@#$%^&()_-+={}|;\"'<,>." "1~`!@#$%^&()_-+={}|;\"'<,>." #> $ address : chr [1:2] "B2" "B6" #> $ row : int [1:2] 2 6 #> $ col : int [1:2] 2 2 #> $ is_blank : logi [1:2] FALSE FALSE #> $ content : chr [1:2] "29" "30" #> $ data_type : chr [1:2] "character" "character" #> $ error : chr [1:2] NA NA #> $ logical : logi [1:2] NA NA #> $ numeric : num [1:2] NA NA #> $ date : POSIXct[1:2], format: NA NA #> $ character : chr [1:2] "empty first row and col" "This worksheet has a nasty name but can be imported nevertheless" #> $ character_formatted:List of 2 #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "empty first row and col" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "This worksheet has a nasty name but can be imported nevertheless" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> $ formula : chr [1:2] NA NA #> $ is_array : logi [1:2] FALSE FALSE #> $ formula_ref : chr [1:2] NA NA #> $ formula_group : int [1:2] NA NA #> $ comment : chr [1:2] "nacnudus:\nComment3" NA #> $ height : num [1:2] 14.2 14.2 #> $ width : num [1:2] 8.38 8.38 #> $ row_outline_level : num [1:2] 1 1 #> $ col_outline_level : num [1:2] 1 1 #> $ style_format : chr [1:2] "Normal" "Normal" #> $ local_format_id : int [1:2] 1 1 str(xlsx_cells(examples, "Sheet1")) #> tibble [395 × 24] (S3: tbl_df/tbl/data.frame) #> $ sheet : chr [1:395] "Sheet1" "Sheet1" "Sheet1" "Sheet1" ... #> $ address : chr [1:395] "A1" "B1" "A2" "B2" ... #> $ row : int [1:395] 1 1 2 2 3 3 4 4 5 5 ... #> $ col : int [1:395] 1 2 1 2 1 2 1 2 1 2 ... #> $ is_blank : logi [1:395] FALSE FALSE FALSE FALSE FALSE FALSE ... #> $ content : chr [1:395] "#DIV/0!" "56" "1" "54" ... #> $ data_type : chr [1:395] "error" "character" "logical" "character" ... #> $ error : chr [1:395] "#DIV/0!" NA NA NA ... #> $ logical : logi [1:395] NA NA TRUE NA FALSE NA ... #> $ numeric : num [1:395] NA NA NA NA NA ... #> $ date : POSIXct[1:395], format: NA NA ... #> $ character : chr [1:395] NA "Error" NA "Boolean (true)" ... #> $ character_formatted:List of 395 #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Error" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Boolean (true)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Boolean (false)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "integer" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "integer with higher-precision number formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "default date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Different built-in date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "custom date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "character" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "character" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "comment" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "comment with formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Currency format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Percent format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Scientific format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to logical" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to a number" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to date" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula evaluating to a string" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "some formulas that are stored in groups" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "single-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "multi-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "multi-cell array formula" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "formula that refers to another file" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal' style but 'normal' has been modified from Excel's default to use Arial font" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "italic" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local italics applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local bold applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local underline applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "normal" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'normal' style but with local double underline applied to this cell." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bad" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'bad' style applied to this cell" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "badboldrightupindent" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'bad' style modified with bold font, right-and-up-aligned with an indent." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "custom style with custom date format" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "auto-font" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Automatic font colour" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "standardred" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "'indexed' or standard red text" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "themeblue" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Blue text from the theme" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bluepattern" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Pattern but no background, blue from theme" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "stylealign" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Custom style 'stylealign' where the style is aligned to the centre." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [16 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:16] "in-cell" "bold" "italic" "underline" ... #> .. ..$ bold : logi [1:16] FALSE TRUE FALSE FALSE FALSE FALSE ... #> .. ..$ italic : logi [1:16] FALSE FALSE TRUE FALSE FALSE FALSE ... #> .. ..$ underline : chr [1:16] NA NA NA "single" ... #> .. ..$ strike : logi [1:16] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ vertAlign : chr [1:16] NA NA NA NA ... #> .. ..$ size : num [1:16] 0 0 0 0 0 0 0 0 0 0 ... #> .. ..$ color_rgb : chr [1:16] NA "FF000000" "FF000000" "FF000000" ... #> .. ..$ color_theme : int [1:16] NA 2 2 2 2 2 2 2 2 2 ... #> .. ..$ color_indexed: int [1:16] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ color_tint : num [1:16] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ font : chr [1:16] "Calibri" "Calibri" "Calibri" "Calibri" ... #> .. ..$ family : int [1:16] 2 2 2 2 2 2 2 2 2 2 ... #> .. ..$ scheme : chr [1:16] "minor" "minor" "minor" "minor" ... #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "in-cellbolditalicunderlineunderlinedoublesingleaccountingdoubleaccountingstrikethroughsuperscriptsubscriptredth"| __truncated__ #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "yellowfillunderline12arialstrikethroughsuperscript" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "More fancy inline-string formatting" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "bottomborderslant" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Slanted text" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "merged" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Cell merged into cell below" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : NULL #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "unprotected" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is not 'protected'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "unhidden" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is not 'hidden'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "locked" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "A cell that is 'locked'" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "themebackground1" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Text is formatted the colour 'Background1' from the theme, i.e. white." #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [2 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:2] "in-cell-bold" "-not-bold" #> .. ..$ bold : logi [1:2] TRUE FALSE #> .. ..$ italic : logi [1:2] FALSE FALSE #> .. ..$ underline : chr [1:2] NA NA #> .. ..$ strike : logi [1:2] FALSE FALSE #> .. ..$ vertAlign : chr [1:2] NA NA #> .. ..$ size : num [1:2] 0 0 #> .. ..$ color_rgb : chr [1:2] "FF000000" NA #> .. ..$ color_theme : int [1:2] 2 NA #> .. ..$ color_indexed: int [1:2] NA NA #> .. ..$ color_tint : num [1:2] NA NA #> .. ..$ font : chr [1:2] "Calibri" "Arial" #> .. ..$ family : int [1:2] 2 2 #> .. ..$ scheme : chr [1:2] "minor" NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Bold applied in inline string (not at cell level)" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [2 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr [1:2] "in-cell-bold" "-not-bold" #> .. ..$ bold : logi [1:2] NA FALSE #> .. ..$ italic : logi [1:2] NA FALSE #> .. ..$ underline : chr [1:2] NA NA #> .. ..$ strike : logi [1:2] NA FALSE #> .. ..$ vertAlign : chr [1:2] NA NA #> .. ..$ size : num [1:2] NA 0 #> .. ..$ color_rgb : chr [1:2] NA NA #> .. ..$ color_theme : int [1:2] NA NA #> .. ..$ color_indexed: int [1:2] NA NA #> .. ..$ color_tint : num [1:2] NA NA #> .. ..$ font : chr [1:2] NA "Arial" #> .. ..$ family : int [1:2] NA 2 #> .. ..$ scheme : chr [1:2] NA NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Bold applied at cell level" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "diagonal-up" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> ..$ : tibble [1 × 14] (S3: tbl_df/tbl/data.frame) #> .. ..$ character : chr "Diagonal-up border" #> .. ..$ bold : logi NA #> .. ..$ italic : logi NA #> .. ..$ underline : chr NA #> .. ..$ strike : logi NA #> .. ..$ vertAlign : chr NA #> .. ..$ size : num NA #> .. ..$ color_rgb : chr NA #> .. ..$ color_theme : int NA #> .. ..$ color_indexed: int NA #> .. ..$ color_tint : num NA #> .. ..$ font : chr NA #> .. ..$ family : int NA #> .. ..$ scheme : chr NA #> .. [list output truncated] #> $ formula : chr [1:395] "1/0" NA NA NA ... #> $ is_array : logi [1:395] FALSE FALSE FALSE FALSE FALSE FALSE ... #> $ formula_ref : chr [1:395] NA NA NA NA ... #> $ formula_group : int [1:395] NA NA NA NA NA NA NA NA NA NA ... #> $ comment : chr [1:395] NA NA NA NA ... #> $ height : num [1:395] 14.2 14.2 14.2 14.2 14.2 ... #> $ width : num [1:395] 19.4 68 19.4 68 19.4 ... #> $ row_outline_level : num [1:395] 1 1 1 1 1 1 1 1 1 1 ... #> $ col_outline_level : num [1:395] 1 1 1 1 1 1 1 1 1 1 ... #> $ style_format : chr [1:395] "Normal" "Normal" "Normal" "Normal" ... #> $ local_format_id : int [1:395] 1 1 1 1 1 1 1 1 5 1 ... # The formats of particular cells can be retrieved like this: Sheet1 <- xlsx_cells(examples, "Sheet1") formats <- xlsx_formats(examples) formats$local$font$bold[Sheet1$local_format_id] #> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [49] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE #> [61] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE #> [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [229] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [241] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [253] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [265] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [277] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [289] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [301] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [313] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [325] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [337] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [349] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [361] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [373] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [385] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE formats$style$font$bold[Sheet1$style_format] #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Percent Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Bad Normal Bad Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> styledate Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal styledate Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal 2 Normal Normal 2 #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal Normal 2 Normal Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal Normal 2 Normal Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Hyperlink Normal 2 Hyperlink Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal 2 Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal #> FALSE FALSE FALSE # To filter for cells of a particular format, first filter the formats to get # the relevant indices, and then filter the cells by those indices. bold_indices <- which(formats$local$font$bold) Sheet1[Sheet1$local_format_id %in% bold_indices, ] #> # A tibble: 3 × 24 #> sheet address row col is_blank content data_type error logical numeric #> #> 1 Sheet1 A28 28 1 FALSE 0 character NA NA NA #> 2 Sheet1 A32 32 1 FALSE 6 character NA NA NA #> 3 Sheet1 A49 49 1 FALSE 12 character NA NA NA #> # ℹ 14 more variables: date , character , #> # character_formatted , formula , is_array , #> # formula_ref , formula_group , comment , height , #> # width , row_outline_level , col_outline_level , #> # style_format , local_format_id # In-cell formatting is available in the `character_formatted` column as a # data frame, one row per substring. xlsx_cells(examples)$character_formatted[77] #> [[1]] #> # A tibble: 16 × 14 #> character bold italic underline strike vertAlign size color_rgb color_theme #> #> 1 "in-cell" FALSE FALSE NA FALSE NA 0 NA NA #> 2 "bold" TRUE FALSE NA FALSE NA 0 FF000000 2 #> 3 "italic" FALSE TRUE NA FALSE NA 0 FF000000 2 #> 4 "underli… FALSE FALSE single FALSE NA 0 FF000000 2 #> 5 "underli… FALSE FALSE double FALSE NA 0 FF000000 2 #> 6 "singlea… FALSE FALSE singleAc… FALSE NA 0 FF000000 2 #> 7 "doublea… FALSE FALSE doubleAc… FALSE NA 0 FF000000 2 #> 8 "striket… FALSE FALSE NA TRUE NA 0 FF000000 2 #> 9 "subscri… FALSE FALSE NA FALSE subscript 0 FF000000 2 #> 10 "supersc… FALSE FALSE NA FALSE superscr… 0 FF000000 2 #> 11 "red" FALSE FALSE NA FALSE NA 0 FFFF0000 NA #> 12 "theme" FALSE FALSE NA FALSE NA 0 FFC0504D 6 #> 13 "tint" FALSE FALSE NA FALSE NA 0 FFC0504D 6 #> 14 "size" FALSE FALSE NA FALSE NA 0 FFFF0000 NA #> 15 "arial" FALSE FALSE NA FALSE NA 0 FFFF0000 NA #> 16 "UTF8Sté… FALSE FALSE NA FALSE NA 0 FFFF0000 NA #> # ℹ 5 more variables: color_indexed , color_tint , font , #> # family , scheme #> --- # Function reference • lexl Reference version 0.1.0 ======================= | | | | --- | --- | | [](https://nacnudus.github.io/lexl/reference/index.html#section-all-functions)
All functions
-------------------------------------------------------------------------------------------- | | | `[demo_lexl](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` | Run a shiny app to demonstrate lexl() | | `[excel_functions](https://nacnudus.github.io/lexl/reference/excel_functions.html) ` | Names of all Excel functions | | `[is_range](https://nacnudus.github.io/lexl/reference/is_range.html) ` | Test that Excel formulas are ranges | | `[lex_xl](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` | Parse Excel formulas into tokens | | `[plot](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` `[lexl_edges](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` `[lexl_vertices](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` `[lexl_igraph](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` | Plot the parse tree of a formula | | `[lexl](https://nacnudus.github.io/lexl/reference/lexl.html) ` | lexl: Parse Excel formulas into tokens. | --- # Developer Notes • lexl Developer Notes =============== #### 2017-10-10 [](https://nacnudus.github.io/lexl/articles/dev-notes.html#what-does-it-do) What does it do? -------------------------------------------------------------------------------------------- `lexl` tokenises Excel formulas into some kind of parse tree, represented by a data frame with three columns: the tokens themselves, the type, and the level within the nested structure. [](https://nacnudus.github.io/lexl/articles/dev-notes.html#how-does-tokenise-formulas) How does tokenise formulas? ------------------------------------------------------------------------------------------------------------------ The bulk of the work is done by a parser generator, according to a grammer defined in `src/token_grammar.h`. ### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#what-is-a-parser-generator) What is a parser generator? A parser generator is a kind of supercharged REGEX engine, which allows actions to be carried out upon each successful match of a particular group within the overall pattern. I don’t know much about parser generators, so the rest of this For example, in REGEX you might define a group by enclosing it within parentheses, so that `Na(na)* Batman` would match `Nanana Batman` as well as `Nananananananana Batman`. With a parser generator, the pattern between the parentheses is called a ‘rule’, and there is nothing that isn’t a rule – even string literals must be set up as rules. Any action defined for a given rule/group/pattern/what-you-will is carried out whenever the rule is successfully matched. For example, you could define an action that prints `"Hey Jude?"`, to be carried out when the rule `(na)` is matched. Given the Batman string above, it that action would be performed several times. Rules can be disabled, and there are some tricks you can pull to have them act differently depending on the current state, but really they’re pretty inflexible. The most annoying problem is backtracking. ### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#backtracking) Backtracking This can happen when a rule is composed of subrules. The parser attempts to match the whole rule, and does not fail until it has already matched some of the subrules. The parser will ‘wind the tape back’ (go back to the place in the string where it started to match the overall rule), but any actions defined on the subrules will already have been performed. For example, if the overall rule is `Na(na)* Batman`, and the string to be matched is `Nananananananana Robin`, then `"Hey Jude?"` would still be printed several times before the overall rule failed to match. `lex_xl` avoids this problem by disabling certain rules. The trade-off is that it the resulting parse tree isn’t very detailed. If it needs to become more detailed, then I must either become wickedly clever, or re-implement the grammer using `boost::spirit` (which can do something about backtracking, albeit slow), or parse the affected tokens a second time, using another grammer. [](https://nacnudus.github.io/lexl/articles/dev-notes.html#todo) TODO --------------------------------------------------------------------- ### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#pretty-print-abstract-syntax-tree-in-console) Pretty print abstract syntax tree in console A minimum thing would be like: print(lex_xl("MIN(3,MAX(2,A1))")) \- root \- MIN \- ( \- 3 \- , \- MAX \- ( \- 2 \- , \- A1 \- ) \- ) Or without the parentheses print(lex_xl("MIN(3,MAX(2,A1))")) \- root \- MIN \- 3 \- , \- MAX \- 2 \- , \- A1 Or in pseudo-`plyr::ast()` [style](http://adv-r.had.co.nz/Expressions.html) (via Jenny Bryan), thinking of something clever for operators. Excel doesn’t have inline function definitions, and function arguments are positional, not named, so pairlists can be ignored. print(lex_xl("MIN(3,MAX(2,A1))")) \- root \- () \- `MIN \- 3 \- () \- `MAX \- 2 \- `A1 Operators would have to be handled using associativity. Fortunately, Excel operators are consistently left-associative (so in Excel `2^2^3` is 64, whereas in R and most other languages it is 256), which makes the logic easier. #### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#api) API * As an S3 override of `plot()`? * As a separate function `lex_tree()`? I think it’s important that users realise it’s just a data frame. How does data.tree do it? ### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#evaluate-formulas) Evaluate formulas This might well be possible using `shiny`. Joe Cheng kindly wrote me an demo of general reactivity. library(shiny) a <- reactiveVal(0) b <- reactive(-a()) c <- observe(print(b())) shiny:::flushReact() workbook <- new.env() # For each formula cell workbook[[cell_id]] <- reactive({ # formula workbook[["other_cell"]]() }) # For each value cell workbook[[cell_id]] <- reactiveVal(initialValue) References to external files (`[0]Sheet1!A1`) would have to be resolved to find their value. Fortunately these values are written into the local file. ### [](https://nacnudus.github.io/lexl/articles/dev-notes.html#test-shiny) Test Shiny Use [shinytest](https://rstudio.github.io/shinytest/articles/shinytest.html) [](https://nacnudus.github.io/lexl/articles/dev-notes.html#continuous-integration) Continuous Integration --------------------------------------------------------------------------------------------------------- I gave up trying to build it in all Travis and AppVeyor environments, invariably because of problems with `ggraph`, so now it is only built in the same environments as `ggraph`’s own continuous integration. [](https://nacnudus.github.io/lexl/articles/dev-notes.html#not-on-cran-yet) Not on CRAN yet ------------------------------------------------------------------------------------------- CRAN thought the package was too small to be distinct from [tidyxl](https://nacnudus.github.io/tidyxl) , which is probably true at the moment. When the package has grow a bit, I’ll resubmit --- # All news • lexl Change log All releases ======================= [](https://nacnudus.github.io/lexl/news/index.html#lexl-0-1-0) lexl 0.1.0 ========================================================================= * CRAN release [](https://nacnudus.github.io/lexl/news/index.html#lexl-0-0-1) lexl 0.0.1 ========================================================================= * Removed Excel formula parsing from [tidyxl](https://nacnudus.github.io/tidyxl) into this new package, renaming it from `xlex` to `lexl`. * Added a `NEWS.md` file to track changes to the package. --- # License • lexl License ======= YEAR: 2017 COPYRIGHT HOLDER: Duncan Garmonsway --- # Run a shiny app to demonstrate lexl() — demo_lexl • lexl Run a shiny app to demonstrate lexl() ===================================== Requires the `shiny`, `igraph` and `ggraph` packages, which you can install with `install.packages(c("shiny", "igraph", "ggraph"))`. Runs a shiny app to demonstrate tokenizing an Excel formula with `[lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` and plotting the parse tree with `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` demo\_lexl() [](https://nacnudus.github.io/lexl/reference/demo_lexl.html#see-also) See also ------------------------------------------------------------------------------ `[lexl::lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) `, `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) `, `lexl::lexl_igraph()` [](https://nacnudus.github.io/lexl/reference/demo_lexl.html#examples) Examples ------------------------------------------------------------------------------ if(interactive()) { demo\_lexl() } --- # Tokenise Excel Formulas • lexl Tokenise Excel Formulas ======================= #### Duncan Garmonsway #### 2017-10-10 [lexl](https://github.com/nacnudus/lexl) separates Excel formulas into tokens of different types, and gives their depth within a nested formula. Its name is a bad pun on ‘Excel’ and ‘lexer’. Try the [online demo](https://duncan-garmonsway.shinyapps.io/lexl/) or run `[demo_lexl()](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` locally. [](https://nacnudus.github.io/lexl/articles/lex-xl.html#example) Example ------------------------------------------------------------------------ library(lexl) x <- lex_xl("MIN(3,MAX(2,A1))") x #> level type token #> 1 0 function MIN #> 2 0 fun_open ( #> 3 1 number 3 #> 4 1 separator , #> 5 1 function MAX #> 6 1 fun_open ( #> 7 2 number 2 #> 8 2 separator , #> 9 2 ref A1 #> 10 1 fun_close ) #> 11 0 fun_close ) plot(x) # Requires the ggraph package ![](https://nacnudus.github.io/lexl/articles/lex-xl_files/figure-html/unnamed-chunk-1-1.png) [](https://nacnudus.github.io/lexl/articles/lex-xl.html#parse-tree) Parse tree ------------------------------------------------------------------------------ Not all parse trees are the same. The one given by `[lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` is intended for analysis, rather than for computation. Examples of the kind of analysis that it might support are: * Detecting constants that have been embedded inside formulas, rather than in cells referred to by formulas. * Revealing which functions and combinations of functions are most common. * Untangling the dependencies between cells in a spreadsheet. [](https://nacnudus.github.io/lexl/articles/lex-xl.html#where-to-find-specimen-formulas) Where to find specimen formulas ------------------------------------------------------------------------------------------------------------------------ The [tidyxl](https://nacnudus.github.io/tidyxl) package imports formulas from xlsx (spreadsheet) files. The [Enron corpus](https://figshare.com/articles/Enron_Spreadsheets_and_Emails/1221767) contains thousands of real-life spreadsheets. [](https://nacnudus.github.io/lexl/articles/lex-xl.html#inspiration) Inspiration -------------------------------------------------------------------------------- [Research](https://drive.google.com/file/d/0B79P2Uym3JjvMjlaWWtnTWRLQmc/view?usp=sharing) by Felienne Hermans inspired this package, and the related [XLParser](https://github.com/spreadsheetlab/XLParser) project was a great help in creating the grammar. --- # Parse Excel formulas into tokens — lex_xl • lexl Parse Excel formulas into tokens ================================ `lex_xl` takes an Excel formula and separates it into tokens. It returns a dataframe, one row per token, giving the token itself, its type (e.g. `number`, or `error`), and its level. The level is a number to show the depth of a token within nested function calls. The token `A2` in the formula `IF(A1=1,A2,MAX(A3,A4))` is at level 1. Tokens `A3` and `A4` are at level 2. The token `IF` is at level 0, which is the outermost level. The output isn't enough to enable computation or validation of formulas, but it is enough to investigate the structure of formulas and spreadsheets. It has been tested on millions of formulas in the Enron corpus. lex\_xl(x) [](https://nacnudus.github.io/lexl/reference/lex_xl.html#arguments) Arguments ----------------------------------------------------------------------------- | | | | --- | --- | | x | Character vector of length 1, giving the formula. | [](https://nacnudus.github.io/lexl/reference/lex_xl.html#value) Value --------------------------------------------------------------------- A data frame (a tibble, if you use the tidyverse) one row per token, giving the token itself, its type (e.g. `number`, or `error`), and its level. [](https://nacnudus.github.io/lexl/reference/lex_xl.html#details) Details ------------------------------------------------------------------------- The different types of tokens are: * `ref` A cell reference/address e.g. `A1` or `$B2:C$14`. * `sheet`A sheet name, e.g. `Sheet1!` or `'My Sheet'!`. If the sheet is from a different file, then the file is included in this token -- usually it has been normalized to the form `[0]`. * `name` A named range, or more properly a named formula. * `function` An Excel or user-defined function, e.g. `MAX` or `_xll.MY_CUSTOM_FUNCTION`. A complete list of official Excel functions is available in the vector `[excel_functions](https://nacnudus.github.io/lexl/reference/excel_functions.html) `. * `error` An error, e.g. `#N/A` or `#REF!`. * `bool` `TRUE` or `FALSE` -- note that there are also functions `TRUE()` and `FALSE()`. * `number` All forms of numbers, e.g. `1`, `1.1`, `-1`, `1.2E3`. * `text` Strings inside double quotes, e.g. `"Hello, World!"`. * `operator` The usual infix operators, `+`, `-`, `*`, `/`, `^`, `<`, `<=`, `<>`, etc. and also the range operator `:` when it is used with ranges that aren't cell addresses, e.g. `INDEX(something):A1`. The union operator `,` is the same symbol that is used to separate function arguments and array columns, so it is only tagged `operator` when it is inside parentheses that are not function parentheses or array curly braces (see the examples). * `paren_open` An open parenthesis `(` indicating an increase in the level of nesting, but not directly enclosing function arguments. * `paren_close` As `open`, but reducing the level of nesting. * `open_array` An open curly brace '{' indicating the start of an array of constants, and an increase in the level of nesting. * `close_array` As `open_array`, but ending the array of constants * `fun_open` An open parenthesis `(` immediately after a function name, directly enclosing the function arguments. * `fun_close` As `fun_open` but immediately after the function arguments. * `separator` A comma `,` separating function arguments or array columns, or a semicolon `;` separating array rows. * `DDE` A call to a Dynamic Data Exchange server, usually normalized to the form `[1]!'DDE_parameter=1'`, but the full form is `'ABCD'|'EFGH'!'IJKL'`. * `space` Some old files haven't stripped formulas of meaningless spaces. They are returned as `space` tokens so that the original formula can always be reconstructed by concatenating all tokens. * `other` If you see this, then something has gone wrong -- please report it at https://github.com/nacnudus/lexl/issues with a reproducible example (e.g. using the reprex package). Every part of the original formula is returned as a token, so the original formula can be reconstructed by concatenating the tokens. If that doesn't work, please report it at https://github.com/nacnudus/lexl/issues with a reproducible example (e.g. using the reprex package). The XLParser project was a great help in creating the grammar. https://github.com/spreadsheetlab/XLParser. [](https://nacnudus.github.io/lexl/reference/lex_xl.html#see-also) See also --------------------------------------------------------------------------- `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) `, `lexl::lexl_igraph()`, `[lexl::demo_lexl()](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` [](https://nacnudus.github.io/lexl/reference/lex_xl.html#examples) Examples --------------------------------------------------------------------------- \# All explicit cell references/addresses are returned as a single 'ref' \# token. lex\_xl("A1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref A1 lex\_xl("A$1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref A$1 lex\_xl("$A1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref $A1 lex\_xl("$A$1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref $A$1 lex\_xl("A1:B2") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref A1:B2 lex\_xl("1:1") \# Whole row #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref 1:1 lex\_xl("A:B") \# Whole column #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref A:B \# If one part of an address is a name or a function, then the colon ':' is \# regarded as a 'range operator', so is tagged 'operator'. lex\_xl("A1:SOME.NAME") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 ref A1 #> 2 0 operator : #> 3 0 name SOME.NAME lex\_xl("SOME\_FUNCTION():B2") #> # A tibble: 5 x 3 #> level type token #> #> 1 0 function SOME\_FUNCTION #> 2 0 fun\_open ( #> 3 0 fun\_close ) #> 4 0 operator : #> 5 0 ref B2 lex\_xl("SOME\_FUNCTION():SOME.NAME") #> # A tibble: 5 x 3 #> level type token #> #> 1 0 function SOME\_FUNCTION #> 2 0 fun\_open ( #> 3 0 fun\_close ) #> 4 0 operator : #> 5 0 name SOME.NAME \# Sheet names are recognised by the terminal exclamation mark '!'. lex\_xl("Sheet1!A1") #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet Sheet1! #> 2 0 ref A1 lex\_xl("'Sheet 1'!A1") \# Quoted names may contain some punctuation #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet 'Sheet 1'! #> 2 0 ref A1 lex\_xl("'It''s a sheet'!A1") \# Quotes are escaped by doubling #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet 'It''s a sheet'! #> 2 0 ref A1 \# Sheets can be ranged together in so-called 'three-dimensional formulas'. \# Both sheets are returned in a single 'sheet' token. lex\_xl("Sheet1:Sheet2!A1") #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet Sheet1:Sheet2! #> 2 0 ref A1 lex\_xl("'Sheet 1:Sheet 2'!A1") \# Quotes surround both sheets (not each) #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet 'Sheet 1:Sheet 2'! #> 2 0 ref A1 \# Sheets from other files are prefixed by the filename, which Excel \# normalizes the filenames into indexes. Either way, lex\_xl() includes the \# file/index in the 'sheet' token. lex\_xl("\[1\]Sheet1!A1") #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet \[1\]Sheet1! #> 2 0 ref A1 lex\_xl("'\[1\]Sheet 1'!A1") \# Quotes surround both the file index and the sheet #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet '\[1\]Sheet 1'! #> 2 0 ref A1 lex\_xl("'C:\\\\My Documents\\\\\[file.xlsx\]Sheet1'!A1") #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet "'C:\\\\My Documents\\\\\[file.xlsx\]Sheet1'!" #> 2 0 ref A1 \# Function names are recognised by the terminal open-parenthesis '('. There \# is no distinction between custom functions and built-in Excel functions. \# The open-parenthesis is tagged 'fun\_open', and the corresponding \# close-parenthesis at the end of the arguments is tagged 'fun\_close'. lex\_xl("MAX(1,2)") #> # A tibble: 6 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 number 1 #> 4 1 separator , #> 5 1 number 2 #> 6 0 fun\_close ) lex\_xl("\_xll.MY\_CUSTOM\_FUNCTION()") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 function \_xll.MY\_CUSTOM\_FUNCTION #> 2 0 fun\_open ( #> 3 0 fun\_close ) \# Named ranges (properly called 'named formulas') are a last resort after \# attempting to match a function (ending in an open parenthesis '(') or a \# sheet (ending in an exclamation mark '!') lex\_xl("MY\_NAMED\_RANGE") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 name MY\_NAMED\_RANGE \# Some cell addresses/references, functions and names can look alike, but \# lex\_xl() should always make the right choice. lex\_xl("XFD1") \# A cell in the maximum column in Excel #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref XFD1 lex\_xl("XFE1") \# Beyond the maximum column, must be a named range/formula #> # A tibble: 1 x 3 #> level type token #> #> 1 0 name XFE1 lex\_xl("A1048576") \# A cell in the maximum row in Excel #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref A1048576 lex\_xl("A1048577") \# Beyond the maximum row, must be a named range/formula #> # A tibble: 1 x 3 #> level type token #> #> 1 0 name A1048577 lex\_xl("LOG10") \# A cell address #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref LOG10 lex\_xl("LOG10()") \# A log function #> # A tibble: 3 x 3 #> level type token #> #> 1 0 function LOG10 #> 2 0 fun\_open ( #> 3 0 fun\_close ) lex\_xl("LOG:LOG") \# The whole column 'LOG' #> # A tibble: 1 x 3 #> level type token #> #> 1 0 ref LOG:LOG lex\_xl("LOG") \# Not a cell address, must be a named range/formula #> # A tibble: 1 x 3 #> level type token #> #> 1 0 name LOG lex\_xl("LOG()") \# Another log function #> # A tibble: 3 x 3 #> level type token #> #> 1 0 function LOG #> 2 0 fun\_open ( #> 3 0 fun\_close ) lex\_xl("A1.2!A1") \# A sheet called 'A1.2' #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet A1.2! #> 2 0 ref A1 \# Text is surrounded by double-quotes. lex\_xl("\\"Some text\\"") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 text "\\"Some text\\"" lex\_xl("\\"Some \\"\\"text\\"\\"\\"") \# Double-quotes within text are escaped by #> # A tibble: 1 x 3 #> level type token #> #> 1 0 text "\\"Some \\"\\"text\\"\\"\\"" \# Numbers are signed where it makes sense, and can be scientific lex\_xl("1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 number 1 lex\_xl("1.2") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 number 1.2 lex\_xl("-1") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 number -1 lex\_xl("-1-1") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number -1 #> 2 0 operator - #> 3 0 number 1 lex\_xl("-1+-1") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number -1 #> 2 0 operator + #> 3 0 number -1 lex\_xl("MAX(-1-1)") #> # A tibble: 7 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 operator - #> 4 1 number 1 #> 5 1 operator - #> 6 1 number 1 #> 7 0 fun\_close ) lex\_xl("-1.2E-3") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 number -1.2E-3 \# Booleans can be constants or functions, and names can look like booleans, \# but lex\_xl() should always make the right choice. lex\_xl("TRUE") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 bool TRUE lex\_xl("TRUEISH") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 name TRUEISH lex\_xl("TRUE!A1") #> # A tibble: 2 x 3 #> level type token #> #> 1 0 sheet TRUE! #> 2 0 ref A1 lex\_xl("TRUE()") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 function TRUE #> 2 0 fun\_open ( #> 3 0 fun\_close ) \# Errors are tagged 'error' lex\_xl("#DIV/0!") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #DIV/0! lex\_xl("#N/A") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #N/A lex\_xl("#NAME?") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #NAME? lex\_xl("#NULL!") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #NULL! lex\_xl("#NUM!") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #NUM! lex\_xl("#REF!") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #REF! lex\_xl("#VALUE!") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 error #VALUE! \# Operators with more than one character are treated as single tokens lex\_xl("1<>2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator <> #> 3 0 number 2 lex\_xl("1<=2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator <= #> 3 0 number 2 lex\_xl("1<2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator < #> 3 0 number 2 lex\_xl("1=2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator = #> 3 0 number 2 lex\_xl("1&2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator & #> 3 0 number 2 lex\_xl("1 2") #> # A tibble: 3 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator #> 3 0 number 2 lex\_xl("(1,2)") #> # A tibble: 5 x 3 #> level type token #> #> 1 0 paren\_open ( #> 2 1 number 1 #> 3 1 operator , #> 4 1 number 2 #> 5 0 paren\_close ) lex\_xl("1%") \# postfix operator #> # A tibble: 2 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator % \# The union operator is a comma ',', which is the same symbol that is used \# to separate function arguments or array columns. It is tagged 'operator' \# only when it is inside parentheses that are not function parentheses or \# array curly braces. The curly braces are tagged 'array\_open' and \# 'array\_close'. lex\_xl("A1,B2") \# invalid formula, defaults to 'union' to avoid a crash #> # A tibble: 3 x 3 #> level type token #> #> 1 0 ref A1 #> 2 0 operator , #> 3 0 ref B2 lex\_xl("(A1,B2)") #> # A tibble: 5 x 3 #> level type token #> #> 1 0 paren\_open ( #> 2 1 ref A1 #> 3 1 operator , #> 4 1 ref B2 #> 5 0 paren\_close ) lex\_xl("MAX(A1,B2)") #> # A tibble: 6 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 ref A1 #> 4 1 separator , #> 5 1 ref B2 #> 6 0 fun\_close ) lex\_xl("SMALL((A1,B2),1)") #> # A tibble: 10 x 3 #> level type token #> #> 1 0 function SMALL #> 2 0 fun\_open ( #> 3 1 paren\_open ( #> 4 2 ref A1 #> 5 2 operator , #> 6 2 ref B2 #> 7 1 paren\_close ) #> 8 1 separator , #> 9 1 number 1 #> 10 0 fun\_close ) \# Function arguments are separated by commas ',', which are tagged \# 'separator'. lex\_xl("MAX(1,2)") #> # A tibble: 6 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 number 1 #> 4 1 separator , #> 5 1 number 2 #> 6 0 fun\_close ) \# Nested functions are marked by an increase in the 'level'. The level \# increases inside parentheses, rather than at the parentheses. Curly \# braces, for arrays, have the same behaviour, as do subexpressions inside \# ordinary parenthesis, tagged 'paren\_open' and 'paren\_close'. lex\_xl("MAX(MIN(1,2),3)") #> # A tibble: 11 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 function MIN #> 4 1 fun\_open ( #> 5 2 number 1 #> 6 2 separator , #> 7 2 number 2 #> 8 1 fun\_close ) #> 9 1 separator , #> 10 1 number 3 #> 11 0 fun\_close ) lex\_xl("{1,2;3,4}") #> # A tibble: 9 x 3 #> level type token #> #> 1 0 open\_array { #> 2 1 number 1 #> 3 1 separator , #> 4 1 number 2 #> 5 1 separator ; #> 6 1 number 3 #> 7 1 separator , #> 8 1 number 4 #> 9 0 close\_array } lex\_xl("1\*(2+3)") #> # A tibble: 7 x 3 #> level type token #> #> 1 0 number 1 #> 2 0 operator \* #> 3 0 paren\_open ( #> 4 1 number 2 #> 5 1 operator + #> 6 1 number 3 #> 7 0 paren\_close ) \# Arrays are marked by opening and closing curly braces, with comma ',' \# between columns, and semicolons ';' between rows Commas and semicolons are \# both tagged 'separator'. Arrays contain only constants, which are \# booleans, numbers, text, and errors. lex\_xl("MAX({1,2;3,4})") #> # A tibble: 12 x 3 #> level type token #> #> 1 0 function MAX #> 2 0 fun\_open ( #> 3 1 open\_array { #> 4 2 number 1 #> 5 2 separator , #> 6 2 number 2 #> 7 2 separator ; #> 8 2 number 3 #> 9 2 separator , #> 10 2 number 4 #> 11 1 close\_array } #> 12 0 fun\_close ) lex\_xl("=MAX({-1E-2,TRUE;#N/A,\\"Hello, World!\\"})") #> # A tibble: 13 x 3 #> level type token #> #> 1 0 operator = #> 2 0 function MAX #> 3 0 fun\_open ( #> 4 1 open\_array { #> 5 2 number -1E-2 #> 6 2 separator , #> 7 2 bool TRUE #> 8 2 separator ; #> 9 2 error #N/A #> 10 2 separator , #> 11 2 text "\\"Hello, World!\\"" #> 12 1 close\_array } #> 13 0 fun\_close ) \# Structured references are surrounded by square brackets. Subexpressions \# may also be surrounded by square brackets, but lex\_xl() returns the whole \# expression in a single 'structured\_ref' token. lex\_xl("\[@col2\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref \[@col2\] lex\_xl("SUM(\[col22\])") #> # A tibble: 4 x 3 #> level type token #> #> 1 0 function SUM #> 2 0 fun\_open ( #> 3 1 structured\_ref \[col22\] #> 4 0 fun\_close ) lex\_xl("Table1\[col1\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref Table1\[col1\] lex\_xl("Table1\[\[col1\]:\[col2\]\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref Table1\[\[col1\]:\[col2\]\] lex\_xl("Table1\[#Headers\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref Table1\[#Headers\] lex\_xl("Table1\[\[#Headers\],\[col1\]\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref Table1\[\[#Headers\],\[col1\]\] lex\_xl("Table1\[\[#Headers\],\[col1\]:\[col2\]\]") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 structured\_ref Table1\[\[#Headers\],\[col1\]:\[col2\]\] \# DDE calls (Dynamic Data Exchange) are normalized by Excel into indexes. \# Either way, lex\_xl() includes everything in one token. lex\_xl("\[1\]!'DDE\_parameter=1'") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 DDE \[1\]!'DDE\_parameter=1' lex\_xl("'Quote'|'NYSE'!ZAXX") #> # A tibble: 1 x 3 #> level type token #> #> 1 0 DDE 'Quote'|'NYSE'!ZAXX \# Meaningless spaces that appear in some old files are returned as 'space' \# tokens, so that the original formula can still be recovered by \# concatenating all the tokens. Spaces between function names and their open \# parenthesis have not been observed, so are not permitted. lex\_xl(" MAX( A1 ) ") #> # A tibble: 8 x 3 #> level type token #> #> 1 0 operator #> 2 0 function MAX #> 3 0 fun\_open ( #> 4 1 operator #> 5 1 ref A1 #> 6 1 operator #> 7 0 fun\_close ) #> 8 0 operator --- # List sheets in an xlsx (Excel) file — xlsx_sheet_names • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html) List sheets in an xlsx (Excel) file =================================== Source: [`R/xlsx_sheet_names.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_sheet_names.R) `xlsx_sheet_names.Rd` `xlsx_sheets()` returns the names of the sheets in a workbook, as a character vector. They are in the same order as they appear in the spreadsheet when it is opened with a spreadsheet application like Excel or LibreOffice. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html#ref-usage) ------------------------------------------------------------------------------------- xlsx_sheet_names(path, check_filetype = TRUE) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html#arguments) ----------------------------------------------------------------------------------------- path Path to the xlsx file. check\_filetype Logical. Whether to check that the filetype is xlsx (or xlsm) by looking at the file itself, rather than using the filename extension. Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html#value) --------------------------------------------------------------------------------- A character vector of the names of the worksheets in the file. Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html#ref-examples) ------------------------------------------------------------------------------------------- examples <- system.file("extdata/examples.xlsx", package = "tidyxl") xlsx_sheet_names(examples) #> [1] "Sheet1" "1~`!@#$%^&()_-+={}|;\"'<,>." #> [3] "E09904.2" "gradientFill" --- # Determine file format — maybe_xlsx • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#main) ![](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html) Determine file format ===================== Source: [`R/maybe_xlsx.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/maybe_xlsx.R) `maybe_xlsx.Rd` Whether a file may be be xlsx, xlsm, xltx or xltm (rather than xls or xlt), based on the [file signature](https://en.wikipedia.org/wiki/List_of_file_signatures) or "magic number", rather than the filename extension. Usage[](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#ref-usage) ------------------------------------------------------------------------------- maybe_xlsx(path) Arguments[](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#arguments) ----------------------------------------------------------------------------------- path File path Value[](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#value) --------------------------------------------------------------------------- Logicial Details[](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#details) ------------------------------------------------------------------------------- Only 'maybe', not 'is', because the xlsx magic number is common to all zip files, not specific to xlsx files. The inverse, 'is\_xls' isn't possible either, because the xls magic number is common to other Microsoft Office files such as .doc and .ppt. This uses some logic from Jenny Bryan's commit to the [readxl](https://github.com/tidyverse/readxl/commit/ff071a4758da3677568362daff52e419c4e0cdfe) package. Examples[](https://nacnudus.github.io/tidyxl/reference/maybe_xlsx.html#ref-examples) ------------------------------------------------------------------------------------- examples_xlsx <- system.file("extdata/examples.xlsx", package = "tidyxl") examples_xlsm <- system.file("extdata/examples.xlsm", package = "tidyxl") examples_xltx <- system.file("extdata/examples.xltx", package = "tidyxl") examples_xltm <- system.file("extdata/examples.xltm", package = "tidyxl") examples_xlsb <- system.file("extdata/examples.xlsb", package = "tidyxl") examples_xls <- system.file("extdata/examples.xls", package = "tidyxl") maybe_xlsx(examples_xlsx) #> [1] TRUE maybe_xlsx(examples_xlsm) #> [1] TRUE maybe_xlsx(examples_xltx) #> [1] TRUE maybe_xlsx(examples_xltm) #> [1] TRUE maybe_xlsx(examples_xlsb) #> [1] TRUE maybe_xlsx(examples_xls) #> [1] FALSE --- # Names of all Excel functions — excel_functions • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/excel_functions.html#main) ![](https://nacnudus.github.io/tidyxl/reference/excel_functions.html) Names of all Excel functions ============================ Source: [`R/data.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/data.R) `excel_functions.Rd` A dataset containing the names of all functions available in Excel. This is useful for identifying user-defined functions in formulas tokenized by `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) `. Usage[](https://nacnudus.github.io/tidyxl/reference/excel_functions.html#ref-usage) ------------------------------------------------------------------------------------ excel_functions Format[](https://nacnudus.github.io/tidyxl/reference/excel_functions.html#format) ---------------------------------------------------------------------------------- A character vector of length 600. Source[](https://nacnudus.github.io/tidyxl/reference/excel_functions.html#source) ---------------------------------------------------------------------------------- Pages 26--27 of Microsoft's document "Excel (.xlsx) extensions to the office openxml spreadsheetml file format p.24" [https://learn.microsoft.com/en-us/openspecs/office\_standards/ms-xlsx/2c5dee00-eff2-4b22-92b6-0738acd4475e](https://learn.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/2c5dee00-eff2-4b22-92b6-0738acd4475e) , revision 8.0 2017-06-20. Details[](https://nacnudus.github.io/tidyxl/reference/excel_functions.html#details) ------------------------------------------------------------------------------------ Note that this includes future function names that are already reserved. --- # Import xlsx (Excel) formatting definitions. — xlsx_formats • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) Import xlsx (Excel) formatting definitions. =========================================== Source: [`R/xlsx_formats.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_formats.R) `xlsx_formats.Rd` `xlsx_formats()` imports formatting definitions from spreadsheets. The structure is a nested list, e.g. `bold` is a vector within the list `font`, which is within the list `local`, which is within the list returned by `xlsx_formats()`. You can look up a cell's formatting by indexing the bottom-level vectors. See 'Details' for examples. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#ref-usage) --------------------------------------------------------------------------------- xlsx_formats(path, check_filetype = TRUE) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#arguments) ------------------------------------------------------------------------------------- path Path to the xlsx file. check\_filetype Logical. Whether to check that the filetype is xlsx (or xlsm) by looking at the file itself, rather than using the filename extension. Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#value) ----------------------------------------------------------------------------- A nested list of vectors, beginning at the top level with `$style` and `$local`, then drilling down to the vectors that hold the definitions. E.g. `my_formats$local$font$size`. Details[](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#details) --------------------------------------------------------------------------------- There are two types of formatting: 'style' formatting, such as Excel's built-in styles 'normal', 'bad', etc., and 'local' formatting, which overrides the style. These are returned in the `$style` and `$local` sublists of `xlsx_formats()`, with identical structures. To look up the local formatting of a given cell, take the cell's `local_format_id` value (`my_cells$Sheet1[1, "local_format_id"]`), and use it as an index into the format structure. E.g. to look up the font size, `my_formats$local$font$size[local_format_id]`. To see all available formats, type `str(my_formats$local)`. Colours may be recorded in any of three ways: a hexadecimal RGB string with or without alpha, an 'indexed' colour, and an index into a 'theme'. `xlsx_formats()` dereferences 'indexed' and 'theme' colours to their hexadecimal RGB string representation, and standardises all RGB strings to have an alpha channel in the first two characters. The 'index' and the 'theme' name are still provided. To filter by an RGB string, you could look up the RGB values in a spreadsheet program (e.g. Excel, LibreOffice, Gnumeric), and use the `[grDevices::rgb()](https://rdrr.io/r/grDevices/rgb.html) ` function to convert these to a hexadecimal string. A <- 1; R <- 0.5; G <- 0; B <- 0 rgb(A, R, G, B) # [1] "#FF800000" Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html#ref-examples) --------------------------------------------------------------------------------------- examples <- system.file("extdata/examples.xlsx", package = "tidyxl") str(xlsx_formats(examples)) #> List of 2 #> $ local:List of 6 #> ..$ numFmt : chr [1:173] "General" "d-mmm-yy" "[$-1409]d mmmm yyyy;@" "yyyy mmmm dddd" ... #> ..$ font :List of 10 #> .. ..$ bold : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ italic : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ underline: chr [1:173] NA NA NA NA ... #> .. ..$ strike : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ vertAlign: chr [1:173] NA NA NA NA ... #> .. ..$ size : num [1:173] 11 11 11 11 11 11 11 11 11 11 ... #> .. ..$ color :List of 4 #> .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ name : chr [1:173] "Arial" "Arial" "Arial" "Arial" ... #> .. ..$ family : int [1:173] 2 2 2 2 2 2 2 2 2 2 ... #> .. ..$ scheme : chr [1:173] NA NA NA NA ... #> ..$ fill :List of 2 #> .. ..$ patternFill :List of 3 #> .. .. ..$ fgColor :List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ bgColor :List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ patternType: chr [1:173] NA NA NA NA ... #> .. ..$ gradientFill:List of 8 #> .. .. ..$ type : chr [1:173] NA NA NA NA ... #> .. .. ..$ degree: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ left : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ right : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ top : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ bottom: num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ stop1 :List of 2 #> .. .. .. ..$ position: num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ color :List of 4 #> .. .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. ..$ stop2 :List of 2 #> .. .. .. ..$ position: num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ color :List of 4 #> .. .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> ..$ border :List of 12 #> .. ..$ diagonalDown: logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ diagonalUp : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ outline : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ left :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ right :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ start :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ end :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ top :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ bottom :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ diagonal :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ vertical :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. ..$ horizontal :List of 2 #> .. .. ..$ style: chr [1:173] NA NA NA NA ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ theme : chr [1:173] NA NA NA NA ... #> .. .. .. ..$ indexed: int [1:173] NA NA NA NA NA NA NA NA NA NA ... #> .. .. .. ..$ tint : num [1:173] NA NA NA NA NA NA NA NA NA NA ... #> ..$ alignment :List of 8 #> .. ..$ horizontal : chr [1:173] "general" "general" "general" "general" ... #> .. ..$ vertical : chr [1:173] "bottom" "bottom" "bottom" "bottom" ... #> .. ..$ wrapText : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ readingOrder : chr [1:173] "context" "context" "context" "context" ... #> .. ..$ indent : int [1:173] 0 0 0 0 0 0 0 0 0 1 ... #> .. ..$ justifyLastLine: logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ shrinkToFit : logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> .. ..$ textRotation : int [1:173] 0 0 0 0 0 0 0 0 0 0 ... #> ..$ protection:List of 2 #> .. ..$ locked: logi [1:173] TRUE TRUE TRUE TRUE TRUE TRUE ... #> .. ..$ hidden: logi [1:173] FALSE FALSE FALSE FALSE FALSE FALSE ... #> $ style:List of 6 #> ..$ numFmt : Named chr [1:6] "General" "General" "yyyy-mm-dd hh:mm:ss" "0%" ... #> .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> ..$ font :List of 10 #> .. ..$ bold : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ italic : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ underline: Named chr [1:6] NA NA NA NA ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ strike : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ vertAlign: Named chr [1:6] NA NA NA NA ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ size : Named num [1:6] 11 11 11 11 11 11 #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ color :List of 4 #> .. .. ..$ rgb : Named chr [1:6] NA "FF9C0006" "FF000000" NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ theme : Named chr [1:6] NA NA "text1" NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ name : Named chr [1:6] "Arial" "Calibri" "Calibri" "Arial" ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ family : Named int [1:6] 2 2 2 2 2 2 #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ scheme : Named chr [1:6] NA "minor" "minor" NA ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> ..$ fill :List of 2 #> .. ..$ patternFill :List of 3 #> .. .. ..$ fgColor :List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA "FFFFC7CE" NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ bgColor :List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ patternType: Named chr [1:6] NA "solid" NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ gradientFill:List of 8 #> .. .. ..$ type : Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ degree: Named int [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ left : Named num [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ right : Named num [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ top : Named num [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ bottom: Named num [1:6] NA NA NA NA NA NA #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ stop1 :List of 2 #> .. .. .. ..$ position: Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ color :List of 4 #> .. .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ stop2 :List of 2 #> .. .. .. ..$ position: Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ color :List of 4 #> .. .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> ..$ border :List of 12 #> .. ..$ diagonalDown: Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ diagonalUp : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ outline : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ left :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ right :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ start :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ end :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ top :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ bottom :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ diagonal :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ vertical :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ horizontal :List of 2 #> .. .. ..$ style: Named chr [1:6] NA NA NA NA ... #> .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. ..$ color:List of 4 #> .. .. .. ..$ rgb : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ theme : Named chr [1:6] NA NA NA NA ... #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ indexed: Named int [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. .. .. ..$ tint : Named num [1:6] NA NA NA NA NA NA #> .. .. .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> ..$ alignment :List of 8 #> .. ..$ horizontal : Named chr [1:6] "general" "general" "center" "general" ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ vertical : Named chr [1:6] "bottom" "bottom" "bottom" "bottom" ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ wrapText : Named logi [1:6] FALSE FALSE TRUE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ readingOrder : Named chr [1:6] "context" "context" "left-to-right" "context" ... #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ indent : Named int [1:6] 0 0 0 0 0 0 #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ justifyLastLine: Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ shrinkToFit : Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ textRotation : Named int [1:6] 0 0 0 0 0 0 #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> ..$ protection:List of 2 #> .. ..$ locked: Named logi [1:6] TRUE TRUE FALSE TRUE TRUE TRUE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... #> .. ..$ hidden: Named logi [1:6] FALSE FALSE FALSE FALSE FALSE FALSE #> .. .. ..- attr(*, "names")= chr [1:6] "Normal" "Bad" "styledate" "Percent" ... # The formats of particular cells can be retrieved like this: cells <- xlsx_cells(examples) formats <- xlsx_formats(examples) formats$local$font$bold[cells$local_format_id] #> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [49] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE #> [61] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE #> [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [229] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [241] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [253] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [265] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [277] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [289] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [301] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [313] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [325] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [337] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [349] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [361] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [373] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [385] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [397] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [409] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [421] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [433] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [445] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [457] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [469] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [481] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [493] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [505] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [517] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [529] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [541] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [553] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [565] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [577] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE formats$style$font$bold[cells$style_format] #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Percent Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Bad Normal Bad Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> styledate Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal styledate Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal 2 Normal Normal 2 #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal Normal 2 Normal Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Normal Normal 2 Normal Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal 2 Hyperlink Normal 2 Hyperlink Normal 2 Normal Normal 2 Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal 2 Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal Normal Normal Normal Normal Normal Normal #> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> Normal Normal #> FALSE FALSE # To filter for cells of a particular format, first filter the formats to get # the relevant indices, and then filter the cells by those indices. bold_indices <- which(formats$local$font$bold) cells[cells$local_format_id %in% bold_indices, ] #> # A tibble: 3 × 24 #> sheet address row col is_blank content data_type error logical numeric #> #> 1 Sheet1 A28 28 1 FALSE 0 character NA NA NA #> 2 Sheet1 A32 32 1 FALSE 6 character NA NA NA #> 3 Sheet1 A49 49 1 FALSE 12 character NA NA NA #> # ℹ 14 more variables: date , character , #> # character_formatted , formula , is_array , #> # formula_ref , formula_group , comment , height , #> # width , row_outline_level , col_outline_level , #> # style_format , local_format_id --- # Test that Excel number formats are date formats — is_date_format • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/is_date_format.html#main) ![](https://nacnudus.github.io/tidyxl/reference/is_date_format.html) Test that Excel number formats are date formats =============================================== Source: [`R/is_date_format.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/is_date_format.R) `is_date_format.Rd` `is_date_format()` tests whether an Excel number format string would a resolve to a date in Excel. For example, the number format string `"yyyy-mm-dd"` would resolve to a date, whereas the string `"0.0\\%"` would not. This is useful if a cell formula contains a number formatting string (e.g. `TEXT(45678,"yyyy")`), and you need to know that the constant 45678 is a date in order to recover it at full resolution (rather than parsing the character output "2025" as a year). It is used internally to convert the value of a cell to the correct data type. Usage[](https://nacnudus.github.io/tidyxl/reference/is_date_format.html#ref-usage) ----------------------------------------------------------------------------------- is_date_format(x) Arguments[](https://nacnudus.github.io/tidyxl/reference/is_date_format.html#arguments) --------------------------------------------------------------------------------------- x character vector of number format strings Examples[](https://nacnudus.github.io/tidyxl/reference/is_date_format.html#ref-examples) ----------------------------------------------------------------------------------------- is_date_format(c("yyyy-mm-dd", "0.0%", "h:m:s", "£#,##0;[Red]-£#,##0")) #> [1] TRUE FALSE TRUE FALSE --- # Import theme color definitions from xlsx (Excel) files — xlsx_color_theme • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) Import theme color definitions from xlsx (Excel) files ====================================================== Source: [`R/xlsx_color_theme.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_color_theme.R) `xlsx_color_theme.Rd` `xlsx_color_theme()` (alias `xlsx_colour_theme()` returns the names and RGB values of theme colours defined in xlsx (Excel) files. For example, `"accent6"` is the name of a theme colour in Excel, which could resolve to any RGB colour defined by the author of the file. Themes are often defined to comply with corporate standards. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html#ref-usage) ------------------------------------------------------------------------------------- xlsx_color_theme(path, check_filetype = TRUE) xlsx_colour_theme(path, check_filetype = TRUE) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html#arguments) ----------------------------------------------------------------------------------------- path Path to the xlsx file. check\_filetype Logical. Whether to check that the filetype is xlsx (or xlsm) by looking at the file itself, rather than using the filename extension. Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html#value) --------------------------------------------------------------------------------- A data frame, one row per colour, with the following columns. * `name` The name of the theme. * `rgb` The RGB colour that has been set for the theme in this file. Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html#ref-examples) ------------------------------------------------------------------------------------------- examples <- system.file("extdata/examples.xlsx", package = "tidyxl") xlsx_color_theme(examples) #> # A tibble: 12 × 2 #> name rgb #> #> 1 background1 FFFFFFFF #> 2 text1 FF000000 #> 3 background2 FFEEECE1 #> 4 text2 FF1F497D #> 5 accent1 FF4F81BD #> 6 accent2 FFC0504D #> 7 accent3 FF9BBB59 #> 8 accent4 FF8064A2 #> 9 accent5 FF4BACC6 #> 10 accent6 FFF79646 #> 11 hyperlink FF0000FF #> 12 followed-hyperlink FF800080 xlsx_colour_theme(examples) #> # A tibble: 12 × 2 #> name rgb #> #> 1 background1 FFFFFFFF #> 2 text1 FF000000 #> 3 background2 FFEEECE1 #> 4 text2 FF1F497D #> 5 accent1 FF4F81BD #> 6 accent2 FFC0504D #> 7 accent3 FF9BBB59 #> 8 accent4 FF8064A2 #> 9 accent5 FF4BACC6 #> 10 accent6 FFF79646 #> 11 hyperlink FF0000FF #> 12 followed-hyperlink FF800080 --- # Test that Excel formulas are ranges — is_range • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/is_range.html#main) ![](https://nacnudus.github.io/tidyxl/reference/is_range.html) Test that Excel formulas are ranges =================================== Source: [`R/is_range.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/is_range.R) `is_range.Rd` `is_range()` tests whether or not an Excel formula is a range. A formula like `A1` is a range, whereas a formula like `MAX(A1,2)` is not. Formulas are not evaluated, so it returns `FALSE` for formulas that would eventually resolve to arrange (e.g. `INDEX(A1:A10,2)`) but that are not immediately a range. Usage[](https://nacnudus.github.io/tidyxl/reference/is_range.html#ref-usage) ----------------------------------------------------------------------------- is_range(x) Arguments[](https://nacnudus.github.io/tidyxl/reference/is_range.html#arguments) --------------------------------------------------------------------------------- x character vector of formulas Examples[](https://nacnudus.github.io/tidyxl/reference/is_range.html#ref-examples) ----------------------------------------------------------------------------------- x <- c("A1", "Sheet1!A1", "[0]Sheet1!A1", "A1,A2", "A:A 3:3", "MAX(A1,2)") is_range(x) #> [1] TRUE TRUE TRUE TRUE TRUE FALSE --- # Import named formulas from xlsx (Excel) files — xlsx_names • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) Import named formulas from xlsx (Excel) files ============================================= Source: [`R/xlsx_names.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/xlsx_names.R) `xlsx_names.Rd` `xlsx_names()` returns the names and definitions of named formulas (aka named ranges) in xlsx (Excel) files. Most names refer to ranges of cells, but they can also be defined as formulas. `xlsx_names()` tells you whether or not they are a range, using `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` to work this out. Names are scoped either globally (used only once in the file), or locally to each sheet (can be reused with different definitions in different sheets). For sheet-scoped names, `xlsx_names()` provides the name of the sheet. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html#ref-usage) ------------------------------------------------------------------------------- xlsx_names(path, check_filetype = TRUE) Arguments[](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html#arguments) ----------------------------------------------------------------------------------- path Path to the xlsx file. check\_filetype Logical. Whether to check that the filetype is xlsx (or xlsm) by looking at the file itself, rather than using the filename extension. Value[](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html#value) --------------------------------------------------------------------------- A data frame, one row per name, with the following columns. * `sheet` If the name is defined only for a specific sheet, the name of the sheet. Otherwise `NA` for names defined globally. * `name` * `formula` Usually a range of cells, but sometimes a whole formula, e.g. `MAX(A2,1)`. * `comment` A description given by the spreadsheet author. * `hidden` Whether or not the name is visible to the user in spreadsheet applications. Hidden names are usually ones that were created automatically by the spreadsheet application. * `is_range` Whether or not the `formula` is a range of cells. This is handy for joining to the set of cells referred to by a name. In this context, commas between cell addresses are always regarded as union operators -- this differs from `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) `, see that help file for details. Examples[](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html#ref-examples) ------------------------------------------------------------------------------------- examples <- system.file("extdata/examples.xlsx", package = "tidyxl") xlsx_names(examples) #> rId sheet name formula comment #> 1 1 Sheet1 named_local_formula MAX(Sheet1!$A$129:$A$130)+1 #> 2 4 E09904.2 sheet_beyond_chart E09904.2!$A$1,E09904.2!$C$1 #> 3 NA intersection Sheet1!$B:$B Sheet1!$8:$8 #> 4 NA named_global_formula Sheet1!$A$129-1 #> 5 NA named_range Sheet1!$A$129 My comment #> hidden is_range #> 1 FALSE FALSE #> 2 FALSE TRUE #> 3 FALSE TRUE #> 4 FALSE FALSE #> 5 FALSE TRUE --- # tidyxl: Import xlsx (Excel) spreadsheet data and formatting into tidy structures. — tidyxl-package • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html#main) ![](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html) tidyxl: Import xlsx (Excel) spreadsheet data and formatting into tidy structures. ================================================================================= Source: [`R/tidyxl-package.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/tidyxl-package.R) `tidyxl-package.Rd` Tidyxl imports data from spreadsheets without coercing it into a rectangle, and retains information encoded in cell formatting (e.g. font/fill/border). This data structure is compatible with the 'unpivotr' package for recognising information expressed by relative cell positions and cell formatting, and re-expressing it in a tidy way. Details[](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html#details) ----------------------------------------------------------------------------------- * `[xlsx_cells()](https://nacnudus.github.io/tidyxl/reference/xlsx_cells.html) ` Import cells from an xlsx file. * `[xlsx_formats()](https://nacnudus.github.io/tidyxl/reference/xlsx_formats.html) ` Import formatting from an xlsx file. * `[xlsx_sheet_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_sheet_names.html) ` List the names of sheets in an xlsx file. * `[xlsx_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) ` Import names and definitions of named ranges (aka 'named formulas', 'defined names') from an xlsx file. * `[is_range()](https://nacnudus.github.io/tidyxl/reference/is_range.html) ` Test whether a 'name' from `[xlsx_names()](https://nacnudus.github.io/tidyxl/reference/xlsx_names.html) ` refers to a range or not. * `[xlsx_validation()](https://nacnudus.github.io/tidyxl/reference/xlsx_validation.html) ` Import cell input validation rules (e.g. 'must be from this drop-down list') from an xlsx file. * `[xlsx_colour_standard()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html) ` A data frame of standard colour names and their RGB values. * `[xlsx_colour_theme()](https://nacnudus.github.io/tidyxl/reference/xlsx_color_theme.html) ` Imports a data frame of theme colour names and their RGB values from an xlsx file. * `[xlex()](https://nacnudus.github.io/tidyxl/reference/xlex.html) ` Tokenise (lex) an Excel formula. See also[](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html#see-also) ------------------------------------------------------------------------------------- Useful links: * [https://github.com/nacnudus/tidyxl](https://github.com/nacnudus/tidyxl) * [https://nacnudus.github.io/tidyxl/](https://nacnudus.github.io/tidyxl/) * Report bugs at [https://github.com/nacnudus/tidyxl/issues](https://github.com/nacnudus/tidyxl/issues) Author[](https://nacnudus.github.io/tidyxl/reference/tidyxl-package.html#author) --------------------------------------------------------------------------------- **Maintainer**: Duncan Garmonsway [nacnudus@gmail.com](mailto:nacnudus@gmail.com) Other contributors: * Hadley Wickham (Author of included readxl fragments) \[contributor\] * Jenny Bryan (Author of included readxl fragments) \[contributor\] * RStudio (Copyright holder of included readxl fragments) \[copyright holder\] * Marcin Kalicinski (Author of included RapidXML code) \[contributor, copyright holder\] --- # Names and RGB values of Excel standard colours — xlsx_color_standard • tidyxl [Skip to contents](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html#main) ![](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html) Names and RGB values of Excel standard colours ============================================== Source: [`R/data.R`](https://github.com/nacnudus/tidyxl/blob/HEAD/R/data.R) `xlsx_color_standard.Rd` A dataset containing the names and RGB colour values of Excel's standard palette. Usage[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html#ref-usage) ---------------------------------------------------------------------------------------- xlsx_color_standard xlsx_colour_standard Format[](https://nacnudus.github.io/tidyxl/reference/xlsx_color_standard.html#format) -------------------------------------------------------------------------------------- A data frame with 10 rows and 2 variables: * `name` Name of the colour * `rgb` RGB value of the colour An object of class `tbl_df` (inherits from `tbl`, `data.frame`) with 10 rows and 2 columns. --- # Names of all Excel functions — excel_functions • lexl Names of all Excel functions ============================ A dataset containing the names of all functions available in Excel. This is useful for identifying user-defined functions in formulas tokenized by `[lexl::lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) `. excel\_functions [](https://nacnudus.github.io/lexl/reference/excel_functions.html#format) Format -------------------------------------------------------------------------------- A character vector of length 600. [](https://nacnudus.github.io/lexl/reference/excel_functions.html#source) Source -------------------------------------------------------------------------------- Pages 26--27 of Microsoft's document "Excel (.xlsx) extensions to the office openxml spreadsheetml file format p.24" [https://msdn.microsoft.com/en-us/library/dd922181(v=office.12).aspx](https://msdn.microsoft.com/en-us/library/dd922181(v=office.12).aspx) , revision 8.0 2017-06-20. [](https://nacnudus.github.io/lexl/reference/excel_functions.html#details) Details ---------------------------------------------------------------------------------- Note that this includes future function names that are already reserved. --- # Test that Excel formulas are ranges — is_range • lexl Test that Excel formulas are ranges =================================== `is_range()` tests whether or not an Excel formula is a range. A formula like `A1` is a range, whereas a formula like `MAX(A1,2)` is not. Formulas are not evaluated, so it returns `FALSE` for formulas that would eventually resolve to arrange (e.g. `INDEX(A1:A10,2)`) but that are not immediately a range. is\_range(x) [](https://nacnudus.github.io/lexl/reference/is_range.html#arguments) Arguments ------------------------------------------------------------------------------- | | | | --- | --- | | x | character vector of Excel formulas | [](https://nacnudus.github.io/lexl/reference/is_range.html#examples) Examples ----------------------------------------------------------------------------- x <- c("A1", "Sheet1!A1", "\[0\]Sheet1!A1", "A1,A2", "A:A 3:3", "MAX(A1,2)") is\_range(x) #> \[1\] TRUE TRUE TRUE TRUE TRUE FALSE --- # Plot the parse tree of a formula — lexl_igraph • lexl Plot the parse tree of a formula ================================ Requires the `igraph` and `ggraph` packages, which you can install with `install.packages(c("igraph", "ggraph"))`. Draws a simple tree plot of the parse tree, showing parent-child relationships, tokens and types. The utility functions `lexl_edges()`, `lexl_vertices()`, and `lexl_igraph()` compute each stage in plotting the final graph. \# S3 method for lexl plot(x, ...) lexl\_edges(x) lexl\_vertices(x) lexl\_igraph(x) [](https://nacnudus.github.io/lexl/reference/lexl_igraph.html#arguments) Arguments ---------------------------------------------------------------------------------- | | | | --- | --- | | x | parse tree from `[lexl()](https://nacnudus.github.io/lexl/reference/lexl.html) ` | | ... | ignored, for consistency with the base `plot()` function | [](https://nacnudus.github.io/lexl/reference/lexl_igraph.html#see-also) See also -------------------------------------------------------------------------------- `[lexl::lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) `, `[lexl::demo_lexl()](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` [](https://nacnudus.github.io/lexl/reference/lexl_igraph.html#examples) Examples -------------------------------------------------------------------------------- x <- [lex\_xl](https://nacnudus.github.io/lexl/reference/lex_xl.html) ("MIN(3,MAX(2,A1))") lexl\_edges(x) #> from to #> 1 0 1 #> 2 0 2 #> 3 0 11 #> 4 2 3 #> 5 2 4 #> 6 2 5 #> 7 2 6 #> 8 2 10 #> 9 6 7 #> 10 6 8 #> 11 6 9 lexl\_vertices(x) #> id label type token #> 1 0 root root root #> 2 1 MIN\\nfunction function MIN #> 3 2 (\\nfun\_open fun\_open ( #> 4 3 3\\nnumber number 3 #> 5 4 ,\\nseparator separator , #> 6 5 MAX\\nfunction function MAX #> 7 6 (\\nfun\_open fun\_open ( #> 8 7 2\\nnumber number 2 #> 9 8 ,\\nseparator separator , #> 10 9 A1\\nref ref A1 #> 11 10 )\\nfun\_close fun\_close ) #> 12 11 )\\nfun\_close fun\_close ) if (interactive() && require(igraph, quietly \= TRUE)) { lexl\_igraph(x) } if (interactive() && require(igraph, quietly \= TRUE) && require(ggraph, quietly \= TRUE)) { plot(x) } --- # lexl: Parse Excel formulas into tokens. — lexl • lexl lexl: Parse Excel formulas into tokens. ======================================= `lex_xl` takes an Excel formula and separates it into tokens. It returns a dataframe, one row per token, giving the token itself, its type (e.g. `number`, or `error`), and its level. [](https://nacnudus.github.io/lexl/reference/lexl.html#details) Details ----------------------------------------------------------------------- The parse tree is designed for analysis rather than computation, and may not support computation. A simple `plot()` function is provided that depends on the suggested package `ggraph` (which pulls in many other dependencies that can be tricky to install). [](https://nacnudus.github.io/lexl/reference/lexl.html#functions) Functions --------------------------------------------------------------------------- * `[lexl::lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` Tokenise (lex) an Excel formula. * `[lexl::is_range()](https://nacnudus.github.io/lexl/reference/is_range.html) ` Test whether an Excel formula refers to a range of cells * `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) `, `lexl::lexl_igraph()` Draw a simple tree plot of a parse tree from `[lexl::lex_xl()](https://nacnudus.github.io/lexl/reference/lex_xl.html) ` * `[lexl::lexl_edges()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` Utility function used by `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` and `lexl::lexl_igraph()` * `[lexl::lexl_vertices()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` Utility function used by `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` and `lexl::lexl_igraph()` * `lexl::lexl_igraph()` Utility function used by `[plot.lexl()](https://nacnudus.github.io/lexl/reference/lexl_igraph.html) ` and `lexl::lexl_igraph()` * `[lexl::demo_lexl()](https://nacnudus.github.io/lexl/reference/demo_lexl.html) ` Shiny app to tokenise formulas and view their parse tree ---