GTAPViz FAQs
This document provides a list of frequently asked questions (FAQs) and their corresponding solutions related to the GTAPViz package. Users can identify relevant issues and run the suggested solutions by copying and pasting the code into their R script and run!
Package Help
Many helpful functions—such as how to rename values or columns—are
not covered in this brief README.
For complete documentation, please refer to the links provided in the
badges below.
Package Versions
Install GTAPViz for the first time
# Install the GTAPViz package (note: case-sensitive)
install.packages("GTAPViz")
# Check the installed version of GTAPViz
library(GTAPViz)
packageVersion("GTAPViz")
Undefined Functions
Common Issue:
- When running the code, an error indicates unused arguments or parameters, or an old version of the package is detected.
Solution 1
Show details for Solution 1
Try force-reinstalling the updated version of
GTAPViz.
Restart your R session (close and reopen R), or go to Session > Restart R in RStudio.
Reinstall the package using the following code:
# Remove the package (if installed)
if ("GTAPViz" %in% installed.packages()) {
remove.packages("GTAPViz")
}
# Clear from memory (if loaded)
if ("GTAPViz" %in% loadedNamespaces()) {
detach("package:GTAPViz", unload = TRUE, character.only = TRUE)
}
# Reinstall the latest version from CRAN
install.packages("GTAPViz")
# Check the version of GTAPViz
library(GTAPViz)
packageVersion("GTAPViz")
Solution 2
Show details for Solution 2
If Solution 1 does not work, the CRAN version of
GTAPViz may be outdated.
Try removing the currently installed CRAN version and reinstalling the
latest development version from GitHub:
# Remove the package (if installed)
if ("GTAPViz" %in% installed.packages()) {
remove.packages("GTAPViz")
}
# Clear from memory (if loaded)
if ("GTAPViz" %in% loadedNamespaces()) {
detach("package:GTAPViz", unload = TRUE, character.only = TRUE)
}
# Install devtools if not already installed
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
# Force reinstallation of the latest version from GitHub
devtools::install_github("Bodysbobb/GTAPViz", force = TRUE)
require(GTAPViz)
# Check the version of GTAPViz
packageVersion("GTAPViz")
Get Empty Outputs
Common Issue:
- Returns empty outputs when running the code.
Solution 1
Show details for Solution 1
Check the “OutputMapping.xlsx” file in the
/mapfolder. Make sure that all three sheets are well defined, and verify that the specified “Variables”, “Regions”, and “Sectors” exist in the input files.Check the project directory to ensure it includes the
/infolder, and confirm that all input file names listed in theexperimentfile are correctly defined and match the actual input filenames.Check the variables (and other names) in the input files (
.sl4and.har). Ensure that they exactly match the names defined in the “OutputMapping.xlsx” file (note: names are case-sensitive).
Solution 2
Show details for Solution 2
If you have tried all steps in Solution 1 but are still getting empty outputs, try running the following code to clear all R objects in the environment and attempt to re-extract all variables (this may take some time):
# Define
project.folder <- "D:/One Drive/OneDrive - purdue.edu/GTAPViz Data/users"
experiment <- c("IncreaseTar10", "IncreaseTar50", "ReduceTar20")
# DO NOT CHANGE BELOW THIS LINE
## Checking Folders
input.folder <- paste0(project.folder, "/in")
map.folder <- paste0(project.folder, "/map")
# Function to check and report folder existence
check_folder <- function(folder.name, folder.path) {
if (!dir.exists(folder.path)) {
message(paste0("❌ '", folder.name, "' does NOT exist at: ", folder.path))
}
}
# Check each folder
check_folder("input.folder", input.folder)
check_folder("map.folder", map.folder)
# Check input files
if (dir.exists(input.folder)) {
files <- list.files(input.folder, pattern = "\\.(sl4|har)$", ignore.case = TRUE, full.names = FALSE)
if (length(files) > 0) {
cat("Available files:", paste(files, collapse = ","), "\n")
}
}
# Extract Data
auto_gtap_data(
experiment = experiment,
process_sl4_vars = NULL,
process_har_vars = NULL,
mapping_info = "GTAPv7",
sl4_mapping_info = NULL,
har_mapping_info = NULL,
sl4_convert_unit = NULL,
har_convert_unit = NULL,
region_select = NULL,
sector_select = NULL,
subtotal_level = FALSE,
input_path = input.folder,
map_path = map.folder,
sl4_extract_method = "get_data_by_var",
har_extract_method = "get_data_by_var",
subtotal_level = TRUE,
plot_data = TRUE,
output_formats = list(
"csv" = "no",
"stata" = "no",
"rds" = "no",
"txt" = "no"))
Solution 3
Show details for Solution 3
Once the solution 2 is complete, check whether
sl4.plot.dataandhar.plot.dataappear in your R environment.
If they do, it means the extraction was successful using default settings. In that case, the root of the problem may be a mismatch when filtering data by “Variable”, “Region”, or “Sector” in the OutputMapping.xlsx file.Other potential issues may include:
- The data extraction method used in your code. Try modifying it as
follows:
sl4_extract_method = "get_data_by_var"har_extract_method = "get_data_by_var"
- Your model includes
subtotal_effects, but this was not enabled. Add this line to your code:subtotal_level = TRUE
- The data extraction method used in your code. Try modifying it as
follows:
If the output is still empty, try reinstalling the package.
After reinstalling, if the issue persists, try importing the raw data directly using the
HARpluspackage.
For more details, see: https://bodysbobb.github.io/HARplus/articles/introduction.html
Plot Dimensions
Common Issue:
The plot output is not aesthetically pleasing—too dense, too small, or too large.
The plot dimensions do not match expectations.
Solution 1
Show details for Solution 1
- Adjust the
widthandheightparameters in the following code.
my_export_config <- create_export_config(
width = 10, # Width in inches
height = 6, # Height in inches
dpi = 300, # Resolution in dots per inch
units = "in" # Units for width and height
)
- Pass the customized
my_export_configto your plot function, while keeping all other settings (...) unchanged, as shown in the example below:
comparison_plot(...,
export_config = my_export_config
)
Plot Order and Rearrangement
Common Issue:
- Want to change the display order in plots, such as showing
EXP2beforeEXP1, orSEAsiabeforeOceania.
Solution 1
Show details for Solution 1
To customize the sorting, use the following command:
# Using the function
sort_sl4_plot <- sort_plot_data(
sl4.plot.data,
sort_columns = list(
Experiment = c("EXP2", "EXP1"), # Column Name = Sorting Order
Region = c("SEAsia", "Oceania")
),
sort_by_value_desc = NULL
)
💡 Tip
This will:
<ul>
<li>Sort by multiple columns simultaneously for precise output control</li>
<li>Apply consistent sorting across all data frames within a data list</li>
</ul>
Renaming Values
Common Issue:
Need to rename values in a specific column before plotting—for example, replacing
"USA"with"United States"or"CHN"with"China"in theREGcolumn across all inputs simultaneously.Need to rename components of decomposition labels—for example, modifying values in the
COLUMNcolumn for welfare decomposition outputs.
Example 1
Show details for Example 1
Option 1: Create a mapping in an Excel file. Column
names ("OldName", "NewName") and structure
must match exactly as shown:
| OldName | NewName |
|---|---|
| USA | United States |
| CHN | China |
Then import it into R as follows:
library(readxl)
# Your mapping file
rename.region <- readxl::read_xlsx("C:/Your/Folder/RenameMapping.xlsx", sheet = "Sheet1")
sl4.plot.data_rename <- rename_value(sl4.plot.data,
column_name = "REG", # Specify the column containing values to rename
mapping.file = rename.region)
Option 2: Create the mapping directly in an R
script:
# Creating Mapping File
rename.region <- data.frame(
ColumnName = "REG",
OldName = c("USA", "CHN"),
NewName = c("United States", "China"),
stringsAsFactors = FALSE
)
sl4.plot.data_rename <- rename_value(sl4.plot.data, mapping.file = rename.region)
Example 2
Show details for Example 2
Option 1: Create a mapping in an Excel file. Column
names ("OldName", "NewName") and structure
must match exactly as shown:
| OldName | NewName |
|---|---|
| alloc_A1 | Alloc Eff. |
| ENDWB1 | Endwb |
| tech_C1 | Tech Chg. |
| pop_D1 | Pop |
| pref_G1 | Perf |
| tot_E1 | ToT |
| IS_F1 | I-S |
Then import it into R as follows:
library(readxl)
# Your mapping file
rename.welfare.comp <- readxl::read_xlsx("C:/Your/Folder/RenameWelfareMapping.xlsx", sheet = "Sheet1")
har.plot.data_rename <- rename_value(har.plot.data,
column_name = "COLUMN", # Specify the column containing values to rename
mapping.file = rename.welfare.comp)
Option 2: Create the mapping directly in an R
script:
# Rename Value if needed
wefare.decomp.rename <- data.frame(
ColumnName = "COLUMN",
OldName = c("alloc_A1", "ENDWB1", "tech_C1", "pop_D1", "pref_G1", "tot_E1", "IS_F1"),
NewName = c("Alloc Eff.", "Endwb", "Tech Chg.", "Pop", "Perf", "ToT", "I-S"),
stringsAsFactors = FALSE
)
welfare_decomp <- rename_value(har.plot.data,
mapping.file = wefare.decomp.rename)
Missing Plot Outputs
Solution 1
Show details for Solution 1
Ensure that the export parameters are set to TRUE in the
plot function:
comparison_plot(...,
export_picture = TRUE,
export_as_pdf = "merged") # (merged will combine all plots into one PDF file)
Solution 2
Show details for Solution 2
- Make sure you have defined the correct
output_pathin the plot function.
- Check the R console to see if any errors occurred while generating
the plots. If the plot was successfully created and saved, the R console
should indicate the output directory.
- If it still doesn’t work, try changing the directory to a local
folder. Sometimes, issues occur due to directory permissions or
cloud-synced folders.
- Restart the R session and try again.
Convert Different Units
Common Issue:
- The data units are not in the desired format.
- Monetary values are in units other than USD.
- You want to convert specific units that are not automatically handled.
Solution 1
Show details for Solution 1
You can convert all units across all data frames within the data list by using the following code:
your_data <- sl4.plot.data # Change this to your data list name
your_data <- convert_units(change_unit_from = c("BAHT", "million USD"),
change_unit_to = c("USD", "billion USD"),
adjustment = c("*34.5", "/1000"))
Show explanation for Solution 1
What does this code do?
Converts
BAHTtoUSDby multiplying the values by 34.5.Converts
million USDtobillion USDby dividing the values by 1,000.
Parameters:
change_unit_from: The original unit name to be changed in the"Unit"column.
If your dataset does not yet include a"Unit"column, please refer to adding unit and description columns.change_unit_to: The new unit name to be recorded in the"Unit"column.adjustment: The mathematical operation used to transform the values.
Accepts operators such as/,*,+, or-.
Missing Unit and Description
Common Issue:
Missing
"Description"and"Unit"columns in the data list.The
"Description"and"Unit"columns are not correctly defined for each variable.
Solution 1
Show details for Solution 1
The easiest way is to re-run 1.ProjectSetup.R file and
ensure that you have define:
OutputMapping.xlsxfile in the/mapfolder, specifically in column"Description"and"Unit"in sheet “SL4File” and “HARFile”.Make sure that you define the
mapping_infoparameter asYesormix
mapping_df <- data.frame(
Variable = c("qgdp", "EV", "ppriv"),
Description = c("Real GDP Index", "Welfare Equivalents", "Consumer Price Index"),
Unit = c("Percent", "million USD", "percent"),
stringsAsFactors = FALSE
)
datasets <- add_mapping_info(mapping_df, external_map = mapping_df, mapping = "Yes")
Note: Use ?add_mapping_info to understand
the meaning of mapping.