Table: A Step-by-Step Guide

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  eval = requireNamespace("GTAPViz", quietly = TRUE)
)
devtools::load_all()
try(devtools::load_all(".."), silent = TRUE)  # go up one level from /vignettes/

input_path <- system.file("extdata/in", package = "GTAPViz")
sl4.plot.data <- readRDS(file.path(input_path, "sl4.plot.data.rds"))
har.plot.data <- readRDS(file.path(input_path, "har.plot.data.rds"))
macro.data    <- readRDS(file.path(input_path, "macro.data.rds"))

This vignette illustrates how to set up table configurations and generate outputs.
For a complete list of available tables with sample images, see the table catalog.

Structured Table

This is an example of how to create a structured pivot table suitable for inclusion in academic presentations.

Important:
The `report_table` function requires **a data list**—not a data frame. - This list structure matches the format of `sl4.plot.data` or `har.plot.data`, without delisting individual elements. - This differs from the plotting functions, which use a **data frame** as input. - Multiple tables can be generated in a single setup using a data list. For example, you can generate report tables for **all data frames within `sl4.plot.data`** in one code block.
report_table(
  data_list = sl4.plot.data,
  pivot_col = list(REG = "Variable",
                   'COMM*REG' = "Commodity"),
  group_by =  list(
    REG = c("Experiment", "Region"),
    'COMM*REG' = c("Experiment", "Region")),
  rename_cols = list("Experiment" = "Scenario"),

  total_column = FALSE,
  decimal = 4,
  subtotal_level = FALSE,
  repeat_label = FALSE,
  include_units = TRUE,

  var_name_by_description = TRUE,
  add_var_info = TRUE,
  add_group_line = FALSE,

  separate_sheet_by = "Unit",
  export_table = FALSE,
  output_path = NULL,
  separate_file = FALSE,
  workbook_name = "Comparison Table"
)

đź’ˇ Tip

`var_name_by_description` replaces variable names with their full descriptions to enhance clarity and give the output a more academic appearance.

Decomposition Table {#sec:decomp-table}

The same report_table function can be used to generate decomposition tables, including a total column, as shown in the example below:

report_table(
  data_list = har.plot.data,
  pivot_col = list(A = "COLUMN", 
                   E1 = "PRICES"),
  group_by = list(
    A = list("Experiment", "Region"), 
    E1 = list("Experiment", "Commodity", "PRICES")
  ),
  rename_cols = list("Experiment" = "Scenario"),

  total_column = TRUE,
  decimal = 6,
  subtotal_level = FALSE,
  repeat_label = FALSE,
  include_units = TRUE,

  var_name_by_description = FALSE,
  add_var_info = FALSE,
  add_group_line = FALSE,

  separate_sheet_by = "Region",
  export_table = FALSE,
  output_path = NULL,
  separate_file = FALSE,
  workbook_name = "Decomposition Table"
)

Interactive Pivot Tables {#sec:pivot-table}

Unlike static tables generated by report_table(), pivot tables remain fully interactive in the Excel output. However, they work with a single data frame at a time (just like the plotting functions).

This function offers:

pivot_table_with_filter(
  data = sl4.plot.data[["COMM*REG"]],
  filter = c("Variable", "Unit"),    # Allow filtering by variable type and unit
  rows = c("Region", "Commodity"),      # Regions and sectors as row fields
  cols = c("Experiment"),            # Experiments as column fields
  data_fields = "Value",             # Values to be aggregated
  raw_sheet_name = "Raw_Data",       # Sheet name for raw data
  pivot_sheet_name = "Sector_Pivot", # Sheet name for pivot table
  export_table = FALSE,
  output_path = NULL,
  workbook_name = "Sectoral_Impact_Analysis.xlsx"
)

Sample Data

Sample data used in this vignette is obtained from the GTAPv7 model and utilizes data from the GTAP 11 database. For more details, refer to the GTAP Database Archive.

```{=html}

```



Try the GTAPViz package in your browser

Any scripts or data that you put into this service are public.

GTAPViz documentation built on June 8, 2025, 11:43 a.m.