Newer
Older
requiredPackages = c('caprir','capriv','gdxrrw', 'usethis','hablar', 'tibble',
'networkD3','readxl','tidyr','dplyr','reshape2','data.table','plotly','webshot','gt')
for(p in requiredPackages){
if(!require(p,character.only = TRUE)) install.packages(p)
library(p,character.only = TRUE)
}
# set gams path, if necessary
gamsPath <- "D://gams//win64//24.9"
igdx(gamsPath)
# set currently working directory
#setwd("D:/public/yang/CAPRIR_Project/caprir_yang")
gdx.dir <- paste0(getwd(), "/inst/extdata/gdx")
# define gdx file name for reading
sanky_file1 <- "res_2_1230cap_after_2014_refdefaultA" # refdefautA
sanky_file2 <- "res_2_1230fta_import_bandefaultA" # bandefaultA
# loaded gdx files and saved in dataout
outdata.dir <- paste0(getwd(), "/inst/outdata")
prod_list = c("WHEA","MAIZ","BARL","RAPE","SUNF","PULS",
"DDGS","SUGR","ACQU", "SOYC", "OCER","SOYA",
"RAPO","POUM","RAPC","SUNC","BIOE","SOYO",
balance_detailed_Baseline <- convert_balance_detailed("EU",
prod_list,
sanky_file1,
folder = gdx.dir)
balance_detailed_Scenario <- convert_balance_detailed("EU",
prod_list,
sanky_file2,
folder = gdx.dir)
# biofuels = T
baseline_biofuels <- prelinks(balance_detailed = balance_detailed_Baseline, p_biofuels = T)
scenario_biofuels <- prelinks(balance_detailed = balance_detailed_Scenario, p_biofuels = T)
# biofuels = F
baseline <- prelinks(balance_detailed = balance_detailed_Baseline, p_biofuels = FALSE)
scenario <- prelinks(balance_detailed = balance_detailed_Scenario, p_biofuels = FALSE)
# get links and nodes for different selected market -------------------
# biofuels, baseline
biofuels_ln_baseline <- links_nodes(baseline_biofuels, scenario_biofuels, p_baseline =TRUE)
# biofuels, scenario
biofuels_ln <- links_nodes(baseline_biofuels, scenario_biofuels, p_baseline = FALSE)
# others, baseline
ln_baseline <- links_nodes(baseline, scenario, TRUE)
# others, scenario
ln <- links_nodes(baseline, scenario, FALSE)
# sankey plots ----------------------
# biofuels, baseline,
p1 <- plot_sankey(biofuels_ln_baseline[[1]],
nodes_position(biofuels_ln_baseline[[2]]),
p_baseline = TRUE,
png = FALSE)
p2 <- plot_sankey(biofuels_ln[[1]],
nodes_position(biofuels_ln[[2]]),
p_baseline = FALSE,
png = FALSE)
# cereals, sugar .. and baseline
plot_sankey(ln_baseline[[1]],nodes_position(ln_baseline[[2]]),TRUE, png = FALSE) ###
# cereals, sugar .. and scenario
plot_sankey(ln[[1]],nodes_position(ln[[2]]), FALSE, png = TRUE)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# tables -----
# basline: load balance market and split into cake oil market and others.
res_b <- market_balance(balance_detailed_Baseline)
# Scenario: load balance market and split into cake oil market and others.
res_S <- market_balance(balance_detailed_Scenario)
oil_cake_market_baseline <- res_b[[1]]
other_baseline <- res_b[[2]]
oil_cake_market_Scenario <- res_S[[1]]
other_Scenario <- res_S[[2]]
# calculate the absolute and percentage changes between baseline and scenario.
oil_cake <- output_df(oil_cake_market_baseline,oil_cake_market_Scenario)
cereals <- output_df(other_baseline,other_Scenario)
# make a nice table
oil <- nicetable(oil_cake,"baseline for oil and cake markets") %>%
tab_footnote(
locations = cells_stub(rows = c(2)),
footnote = md("Destilled dried grains from bio-ethanol processing")
)%>%
gtsave(
"oil_cake.html", inline_css = FALSE,
path = outdata.dir)
cereals <- nicetable(cereals,"baseline for cereals, sugar, and meat markets")%>%
gtsave(
"cereals.html", inline_css = FALSE,
path = outdata.dir)
#
webshot(paste0(outdata.dir,"/oil_cake.html"),paste0(outdata.dir,"/png/","oil_cake.png"),
vwidth = 1100)
webshot(paste0(outdata.dir,"/cereals.html"),paste0(outdata.dir,"/png/","cereals.png"),
vwidth = 1100)