This manual provides an explanation of the file 3.TableGens.R

❗ Important:
  • You must run 1.ProjectSetup with plot_data <- TRUE before running this script.
  • You can simply copy and paste any table code from this manuscript and replace the original code in 3.TableGens.R.

Table Types

If you are unsure, please refer to: Table Catalogs

You may define a custom output folder to create separate directories for each plot.

# Define new output location (or leave it as deafult)
output.folder <- output.folder

1 Report Table

1.1 Input (data_list)

For this table, a data list is required, this data definition should match the output in sl4.plot.data and har.plot.data.

  • This is different to the data used in plotting, which is a data frame.

  • Multiple tables based on a single data list can be created in one setup (e.g., all data frames within sl4.plot.data can be generated in a single code block).

data_list <- sl4.plot.data   

# However, if you used "group_data_by_dims", you may need to de-list one level as follows:
# data_list <- sl4.plot.data[["Sector"]]

1.2 Key Table Parameters

(1) pivot_col

Define the pivot column for each dataframe within the data list. For example,

  • REG and COMM*REG are dataframes within sl4.plot.data,

  • Variable and Commodity are the columns to be pivoted in each respective dataframe

pivot_col = list( # DO NOT CHANGE THIS LIST LINE; MAKE YOUR CHANGES BELOW
  REG = "Variable", 
  COMM*REG = "Commodity"
) # DO NOT REMOVE THIS BRACKET


(2) group_by

Define the main group columns (used as rows in the structured table) for each dataframe. The order matters: the first listed will be shown as the first (leftmost) column

group_by = list( # DO NOT CHANGE THIS LIST LINE; MAKE YOUR CHANGES BELOW
  REG = list("Experiment", "Region"),
  COMM*REG = list("Experiment", "Variable", "Region")
) # DO NOT REMOVE THIS BRACKET


(3) configs
rename_cols       = list("Experiment" = "Scenario"),  # Rename "Experiment" to "Scenario"
total_column      = FALSE,  # Add row totals (e.g., for decomposition)
decimal           = 4,      # Number of decimal places
subtotal_level    = FALSE,  # Include subtotal rows from CGE if available
repeat_label      = FALSE,  # Repeat group labels in all rows
include_units     = TRUE,   # Append unit to column names (e.g., (%), (million USD))

var_name_by_description = TRUE,  # Use variable description instead of GTAP code
add_var_info            = TRUE,  # Append code or description in parentheses
add_group_line          = FALSE, # Add line between groups based on first column

separate_sheet_by = "Unit",          # Split sheets by this column
export_table      = TRUE,            # Export to Excel
output_path       = output.folder,   # Output directory
separate_file     = FALSE,           # Split sheets into separate workbooks
workbook_name     = "Comparison Table 1D"  # Workbook file name


1.3 Report Table Codes

💻 Report Table Codes

A default code provided in `3.TableGens.R`.


# Your data list
data_list <- sl4.plot.data

report_table(
  data_list = data_list,
  pivot_col = list( # See manual or run `?report_table` for more details
    REG = "Variable", 
    'COMM*REG' = "Commodity"
  ),
  group_by = list( # See manual or run `?report_table` for more details
    REG = list("Experiment", "Region"),
    'COMM*REG' = list("Experiment", "Variable", "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 = TRUE,
  output_path = output.folder,
  separate_file = FALSE,
  workbook_name = "Comparison Table Default"
)

A one-dimensional structure groups data by a single dimension, such as REG, COMM, or ACTS.


# Your data list
data_list <- sl4.plot.data

report_table(
  data_list = data_list,
  pivot_col = list( # See manual or run `?report_table` for more details
  REG = "Variable"
  ),
  group_by = list( # See manual or run `?report_table` for more details
    REG = list("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 = TRUE,
  output_path = output.folder,
  separate_file = FALSE,
  workbook_name = "Comparison Table 1D"
)

A two-dimensional structure groups data by two dimensions, such as REG*COMM or REG*ACTS.


# Your data list
data_list <- sl4.plot.data

report_table(
  data_list = data_list,
  pivot_col = list( # See manual or run `?report_table` for more details
    COMM*REG = "Commodity"
  ),
  
  group_by = list( # See manual or run `?report_table` for more details
    COMM*REG = list("Experiment", "Variable", "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 = TRUE,
  output_path = output.folder,
  separate_file = FALSE,
  workbook_name = "Comparison Table 2D"
)

A multi-dimensional structure groups data by more than two dimensions, such as COMM*REG*REG for bilateral trade at the sector level.


# Your data list
data_list <- bilateral_data

report_table( 
  data_list = bilateral_data,
  pivot_col = list( # See manual or run `?report_table` for more details
  qxs = "Commodity"
  ),
  
  group_by = list( # See manual or run `?report_table` for more details
    qxs = list("Experiment", "Source", "Destination")
    ),
    
  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 = TRUE,
  
  separate_sheet_by = "Unit",
  export_table = TRUE,
  output_path = output.folder,
  separate_file = FALSE,
  workbook_name = "Comparison Table 3D"
)

2 Pivot Table with Filter

This pivot table requires a data frame similar to the one used for plotting. Therefore, you can create only one table per setup—unlike structured tables, which support multiple tables within a single setup.

💻 Pivot Table with Filter Codes

A default code provided in `3.TableGens.R`.


# Your data frame
data_pivot_table <- sl4.plot.data[["REG"]] 

pivot_table_with_filter( # DO NOT CHANGE THIS FUCNTION NAME; MAKE YOUR CHANGES BELOW
  data = data_pivot_table,
  filter = c("Variable", "Unit"),    # Allow filtering by variable type and unit
  rows = c("Region", "Sector"),      # 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
  dims = "A3",                      # Starting cell for pivot table
  export = TRUE,
  output_path = output.folder,
  workbook_name = "Sectoral_Impact_Analysis.xlsx"
)

# Your data frame
data_pivot_table <- bilateral_data[["qxs"]] 

pivot_table_with_filter( # DO NOT CHANGE THIS FUCNTION NAME; MAKE YOUR CHANGES BELOW
  data = data_pivot_table,
  filter = c("Experiment", "Commodity", "Unit"),    # Allow filtering by variable type and unit
  rows = c("Source"),      # Regions and sectors as row fields
  cols = c("Destination"),           # 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
  dims = "A5",                      # Starting cell for pivot table
  export = TRUE,
  output_path = output.folder,
  workbook_name = "Sectoral_Impact_Analysis.xlsx"
)