# Example 1: Save a single matrix
har_path <- system.file("extdata", "TAR10-WEL.har", package = "HARplus")
har_data <- load_harx(har_path)
welfare_data <- get_data_by_var("A", har_data)
welfare_data_A <- welfare_data[["har_data"]][["A"]]
save_har(
data_list = list(WELF = welfare_data),
file_path = file.path(tempdir(), "output_single.har"),
dimensions = list(WELF = c("REG", "COLUMN")),
value_cols = c(WELF = "Value"),
long_desc = c(WELF = "Welfare Decomposition"),
export_sets = TRUE,
lowercase = FALSE
)
# Example 2: Save multiple matrices
welfare_data <- get_data_by_var(c("A", "A1"), har_data)
welfare_data <- welfare_data[["har_data"]]
save_har(
data_list = list(WELF = welfare_data[["A"]],
DECOM = welfare_data[["A1"]]),
file_path = file.path(tempdir(), "output_multi.har"),
dimensions = list(WELF = c("REG", "COLUMN"),
DECOM = c("REG", "ALLOCEFF")),
value_cols = list(WELF = "Value",
DECOM = "Value"),
long_desc = list(WELF = "Welfare Decomposition",
DECOM = "Allocative efficiency effect"),
export_sets = TRUE,
lowercase = FALSE
)
# Example 3: Apply mapping file for sorting
mapping <- list(
REG = c("RestofWorld", "MENA", "EU_28"),
COLUMN = c("alloc_A1", "tot_E1")
)
save_har(
data_list = list(
WELF = welfare_data[["A"]],
DECOM = welfare_data[["A1"]]
),
file_path = file.path(tempdir(), "output_sorted.har"),
dimensions = list(
WELF = c("REG", "COLUMN"),
DECOM = c("REG", "ALLOCEFF")
),
value_cols = list(
WELF = "Value",
DECOM = "Value"
),
long_desc = list(
WELF = "Welfare Decomposition",
DECOM = "Allocative efficiency effect"
),
export_sets = TRUE,
dim_order = mapping,
lowercase = FALSE
)
# Example 4: Rename duplicated dimension names
# to export as a single structure (e.g., COMMxREGxREG)
har_path <- system.file("extdata", "TAR10-WEL.har", package = "HARplus")
har_data <- load_harx(har_path)
welfare_data <- get_data_by_var("C17", har_data)
welfare_data <- welfare_data[["har_data"]][["C17"]]
mapping <- list(
COMM = c("TransComm", "MeatLstk"),
REG = c("SSA", "RestofWorld", "SEAsia")
)
# Export HAR
save_har(
data_list = list(TEST = welfare_data),
file_path = file.path(tempdir(), "output_single.har"),
dimensions = list(TEST = c("COMM", "REG", "REG.1")),
value_cols = list(TEST = "Value"),
long_desc = list(TEST = "Shock RTMS"),
dim_rename = list(TEST = c(COMM = "COMM", SREG = "REG", DREG = "REG.1")),
export_sets = TRUE,
dim_order = mapping,
lowercase = FALSE
)
Run the code above in your browser using DataLab