Newer
Older
# install devtools
install.packages("devtools")
library(devtools)
# install tidykml
devtools::install_github("briatte/tidykml")
library(tidykml)
# install gdxrrw
# 1: download from website: https://support.gams.com/gdxrrw:interfacing_gams_and_r
# 2: install package
install.packages("D:/public/yang/CAPRIR_Project/gdxrrw_1.0.5.zip", repos = NULL, type="source")
# or install from github
devtools::install_github("GAMS-dev/gdxrrw/gdxrrw")
library(gdxrrw)
# install capriR package
# remotes::install_github("trialsolution/caprir", force = TRUE)
devtools::install_git("https://git-dmz.thuenen.de/mindstep/caprir.git")
library(caprir)
devtools::install_git("https://git-dmz.thuenen.de/mindstep/capriv.git")
library(capriv)
#
# requiredPackages_Github <- c("briatte/tidykml", "GAMS-dev/gdxrrw/gdxrrw")
#
#
# requiredPackages_Gitlab <- "https://git-dmz.thuenen.de/mindstep/caprir.git"
# # "https://git-dmz.thuenen.de/mindstep/capriv.git")
requiredPackages = c('caprir','capriv','gdxrrw', 'usethis','hablar', 'tibble',
'networkD3','readxl','tidyr','dplyr','reshape2',
'data.table','plotly', 'gt', 'ggtext', 'rlang', 'Hmisc')
if(!require(p,character.only = TRUE))install.packages(p)
# 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)
capriv::plot_sankey(ln_baseline, p_baseline =TRUE, png = FALSE) ###
capriv::plot_sankey(ln, p_baseline = FALSE, png = FALSE)
# two sankey diagrams in one page ----
cake_oil_m <- combine_dfs(biofuels_ln_baseline, biofuels_ln)
cereals <- combine_dfs(ln_baseline, ln)
capriv::plot_sankey(cake_oil_m,
p_baseline = TRUE,
png = FALSE)
capriv::plot_sankey(cereals,
p_baseline = TRUE,
png = TRUE)
#### ---
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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)