knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(tidycells)

tidycells

Read Tabular Data from Diverse Sources and Easily Make Them Tidy

# following given low res raster image
# as https://tinyverse.netlify.com/badge/tidycells pointing to 
# https://img.shields.io/badge/dependencies-9/43-orange.png?style=flat not 
# https://img.shields.io/badge/dependencies-9/43-orange.svg
# [![Dependency status](https://tinyverse.netlify.com/badge/tidycells)](https://CRAN.R-project.org/package=tidycells)

# try to convert url from .png to .svg
# but it will correspond to last CRAN version only
# x <- curlGetHeaders("https://tinyverse.netlify.com/badge/tidycells")
# tg <- which(stringr::str_detect(x, "img.shields.io")) %>% min()

depi <- as.vector(read.dcf('DESCRIPTION')[, 'Imports'])
imps <- stringr::str_split(depi, ",")[[1]] %>% stringr::str_extract("[a-zA-Z0-9]+")

deps <- as.vector(read.dcf('DESCRIPTION')[, 'Suggests'])
sugg <- stringr::str_split(deps, ",")[[1]] %>% stringr::str_extract("[a-zA-Z0-9]+")

x <- installed.packages()
# for inclusion of base and recommended pkgs
usr_pkgs <- x %>% tibble::as_tibble() %>% dplyr::mutate(pkg = row.names(x)) %>% dplyr::filter(is.na(Priority)) %>% dplyr::pull(pkg)

# excluding only base
# usr_pkgs <- x %>% tibble::as_tibble() %>% dplyr::mutate(pkg = row.names(x)) %>% dplyr::filter(Priority == "base") %>% dplyr::pull(pkg)

imps <- intersect(imps, usr_pkgs)
sugg <- intersect(sugg, usr_pkgs)

dep_str <- paste0(length(imps),"/",(length(imps)+length(sugg)))

CRAN status CRAN checks Travis build status AppVeyor build status R-hub checks Project Status: Active – The project has reached a stable, usable state and is being actively developed. Codecov Coverage Status Coveralls Coverage Status Lifecycle Dependency status license See DevNotes

CircleCI build status R build status

Author

Indranil Gayen

TL;DR

Given a file_name which is a path of a file that contains table(s). Run this read_cells() in the R-console to see whether support is present for the file type. If support is present, just run

read_cells(file_name)

Note

A Word of Warning :

Many functions in this package are heuristic-algorithm based. Thus, outcomes may be unexpected. I recommend you to try read_cells on the target file. If the outcome is what you are expecting, it is fine. If not try again with read_cells(file_name, at_level = "compose"). If after that also the output is not as expected then other functions are required to be used. At that time start again with read_cells(file_name, at_level = "make_cells") and proceed to further functions.

Introduction

The package provides utilities to read, cells from complex tabular data and heuristic detection based 'structural assignment' of those cells to a columnar or tidy format.

Read functionality has the ability to read (in a unified manner) structured, partially structured or unstructured tabular data (usually spreadsheets for public data dissemination and aimed for common human understanding) from various types of documents. The tabular information is read as cells. The 'structure assignment' functionality has both supervised and unsupervised way of assigning cells data to columnar/tidy format. Multiple disconnected blocks of tables in a single sheet are also handled appropriately.

These tools are suitable for unattended conversation of (maybe a pile of) messy tables (like government data) into a consumable format(usable for further analysis and data wrangling).

Installation

Install the CRAN version:

install.packages("tidycells")

To install the development version from GitHub you'll need remotes package in R (comes with devtools). Assuming you have remotes you can install this package in R with the following command:

# devtools::install_github is actually remotes::install_github
remotes::install_github("r-rudra/tidycells")

To start with tidycells, I invite you to see vignette("tidycells-intro") or check out tidycells-website (to see vignette you need to install the package with vignette. That can be done in above command (remotes::install_github) by specifying build_vignettes = TRUE. Note that, it might be time consuming. CRAN version comes with prebuilt-vignette).

Quick Overview

Let's take a quick look at an example data as given in

system.file("extdata", "marks.xlsx", package = "tidycells", mustWork = TRUE)

The data looks like (in excel)

knitr::include_graphics("vignettes/ext/marks.png")

Let's try tidycells functions in this data

Read at once

# you should have tidyxl installed
system.file("extdata", "marks.xlsx", package = "tidycells", mustWork = TRUE) %>% 
  read_cells()
if(rlang::is_installed("tidyxl")){
  system.file("extdata", "marks.xlsx", package = "tidycells", mustWork = TRUE) %>% 
    read_cells() %>% 
    knitr::kable()
}

The function read_cells is a set of ordered operations connected together. The flowchart of read_cells:

knitr::include_graphics("vignettes/ext/read_cells.svg")

Let's understand step by step procedures followed by read_cells.

# if you have tidyxl installed
d <- system.file("extdata", "marks.xlsx", package = "tidycells", mustWork = TRUE) %>% 
  read_cells(at_level = "make_cells") %>% 
  .[[1]]

Or

# or you may do
d <- system.file("extdata", "marks_cells.rds", package = "tidycells", mustWork = TRUE) %>% 
  readRDS()

Then

d <- numeric_values_classifier(d)
da <- analyze_cells(d)

After this you need to run compose_cells (with argument print_attribute_overview = TRUE)

dc <- compose_cells(da, print_attribute_overview = TRUE)
knitr::include_graphics("vignettes/ext/compose_cells_cli1.png")
dc <- compose_cells(da)

If you want a well-aligned columns then you may like to do

# bit tricky and tedious unless you do print_attribute_overview = TRUE in above line
dcfine <- dc %>% 
  dplyr::mutate(name = dplyr::case_when(
    data_block == 1 ~ major_row_left_2_1,
    data_block == 2 ~ major_col_bottom_1_1,
    data_block == 3 ~ major_row_left_1_1
  ),
  sex = dplyr::case_when(
    data_block == 1 ~ major_row_left_1_1,
    data_block == 2 ~ major_col_bottom_2_1,
    data_block == 3 ~ minor_row_right_1_1
  ),
  school = dplyr::case_when(
    data_block == 1 ~ minor_col_top_1_1,
    data_block == 2 ~ minor_corner_topLeft_1_1,
    data_block == 3 ~ minor_col_top_1_1
  )) %>% 
  dplyr::select(school,sex, name, value)

head(dcfine) looks like

knitr::kable(head(dcfine), align = c(rep("l", 3), "c"))

This is still not good right! You had to manually pick some weird column-names and spent some time and energy (when it was evident from data which columns should be aligned with whom).

The collate_columns functions does exactly this for you. So instead of manually picking column-names after compose cells you can simply run

# collate_columns(dc) should be same with 
# direct read_cells() result except table_tag column
collate_columns(dc) %>% 
  head()
knitr::kable(head(collate_columns(dc)), align = c(rep("l", 5), "c"))

Looks like staged example! Yes, you are right this is not always perfect (same is true for analyze_cells also). However, if the data is somehow helpful in demystifying underlying columns structure (like this one), then this will be useful.

These functions read_cells (all functionalities combined), analyze_cells, collate_columns are here to ease your pain in data wrangling and reading from various sources. It may not be full-proof solution to all types of tabular data. It is always recommended to perform these tasks manually whenever expected results are not coming.

Plots and Interactive Modules

The package provides ggplot based plots and shiny based interactive visualisations for understanding how the heuristic is functioning and also provides object (like cell-df or cell-analysis) editing capabilities.

The shiny package is required for interactive modules. Most of the features are self-explanatory and guided.

Check out interactive documentation of any of these functions listed below. All of these functions are available as RStudio Addins.

Here are screenshots of each interactive widgets.

  1. Plot tune (part of all modules)
  2. visual_crop() for data crop and deletion of sections
knitr::include_graphics("vignettes/ext/v12.png")
  1. visual_va_classify() for interactive VA classification
  2. visual_data_block_inspection() this shows how the heuristic has performed the analysis after analyze_cells
knitr::include_graphics("vignettes/ext/v34.png")
  1. visual_orientation_modification() for modification to heuristic based results
  2. visual_traceback() this is for observing how the original data is composed to form the final output. (compose_cells is called internally)
knitr::include_graphics("vignettes/ext/v56.png")

For each of these modules, there is a dynamic plot option available from plotly. If you have that package, the corresponding tab will be activated. Since all of these modules are entirely optional the dependency is kept at tidycells 'suggests' level only.

Reference and Related Projects

Acknowledgement

This package incomplete without following packages (apart from the unpivotr which is the core package on which tidycells depends largely, as mentioned above). Each of these packages are in suggests fields of tidycells. (The read_cells basically, performs unification on several functions from various packages to give you support for different file types. These are listed below.)



r-rudra/tidycells documentation built on July 19, 2022, 5:10 a.m.