# get_GUI_table___<- function(table = "supply details", scenario_list, folder = "mydata"){
#
#
#
# # extract 'supply tables'
# if(table == "supply details"){
#
# print("table == supply details")
#
# selection_regions <- regions %>% filter(grepl("Regions and C", sel, ignore.case = TRUE)) %>% select(key)
# selection_dim5s <- as_tibble(data.frame(key = c("")))
# selection_cols <- activities %>% select(key)
#
#
# print("selection_cols 1")
# selection_regions <- selection_regions$key
# selection_dim5s <- selection_dim5s$key
# selection_cols <- selection_cols$key
#
#
# # load("data/table_coco.RData")
#
# # initialize code list
# code_list <- as_tibble(data.frame())
# print("selection_cols 2")
# # add the columns without numbering
# x <- coco %>% filter(name == "Supply details") %>% select(item.key,
# item.itemName,
# item.unit)
# colnames(x) <- c("key","name","unit")
# code_list <- rbind(code_list, x)
#
# for(i in 1:17){
# x <- coco %>% filter(name == "Supply details") %>%
# select(paste("item.key.", i, sep = ""),
# paste("item.itemName.", i, sep = ""),
# paste("item.unit.", i, sep = ""))
# colnames(x) <- c("key","name","unit")
# code_list <- rbind(code_list, x)
#
# }
#
#
# # remove NA's and duplicates
# x <- code_list %>% filter(!is.na(key)) %>% filter(!duplicated(key))
#
# selection_rows <- x$key
# name_rows <- x$name
# unit_rows <- x$unit
#
# # call extract_gui_table()
# supply_details <- extract_gui_table(region_list = selection_regions, dim5_list = selection_dim5s, cols_list = selection_cols,
# rows_list = selection_rows,
# scenario_list = scenario_list, folder )
#
# # xinxin did
# # names of supply_deteils: .i1 .i2 .i3 .i4 .i5 dataOut scenario name
# # i1- i5 -> .i1-i5
# # dataOut <- value
#
# supply_details <- left_join(supply_details, x, c(".i4" = "key"))
#
# supply_details <- supply_details %>% select(.i1, .i3, .i4, name, unit, .i5, scenario, dataOut)
#
# products <- products %>% select(key, itemName)
# supply_details <- left_join(supply_details, products, c(".i3" = "key"))
# supply_details <- supply_details %>% select(.i1, .i3,itemName, .i4, name, unit, .i5, scenario, dataOut)
#
# colnames(supply_details) <- c("region", "activity_code", "activity_name", "variable_code", "variable_name", "unit", "year", "scenario", "value")
# rm(x)
#
# # calculate additional columns
#
# # 1 a. crop shares UAAR.LEVL:
# levels <- supply_details %>% group_by(region, activity_code) %>% filter(variable_code == "LEVL")
# uaars <- supply_details %>% filter(variable_code == "LEVL") %>% filter(activity_code == "UAAR")
# uaars <- uaars %>% select(region, activity_code, year, scenario, value)
#
#
# x <- left_join(levels, uaars, c("region","year","scenario"))
# rm(levels, uaars)
#
# colnames(x) <- c("region","activity_code","activity_name","variable_code","variable_name","unit","year","scenario","valuex","todelete","valuey")
# x <- x %>% mutate(value = valuex/valuey)
# x <- x %>% select(-valuex, -valuey, -todelete)
#
# x$variable_code <- gsub('LEVL', 'UAAR', x$variable_code, ignore.case = TRUE)
# x$variable_name <- rep("Crop Share", length(x$variable_name))
# x$unit <- rep("% or 0.01 animals/ha", length(x$unit))
# x <- x %>% filter(activity_code != "UAAR")
#
# #
# supply_details <- bind_rows(supply_details, x)
# rm(x)
# library(data.table)
# #
# #
# #
# #
# # # xinxin did: bind_rows(one, two)
# # supply_details <- bind_rows(supply_details, x)
#
#
# # convert matrix to df
# supply_details <- as_tibble(supply_details)
#
#
# # rename UAAR to CropShare to avoid misunderstandings
# setDT(supply_details)[variable_name == "Crop Share", variable_code := "CropShare"]
#
# supply_details <- as_tibble(supply_details)
#
#
# # 1 b. crop shares per arable land ARAB.LEVL:
# levels <- supply_details %>% group_by(region, activity_code) %>% filter(variable_code == "LEVL")
# arabs <- supply_details %>% filter(variable_code == "LEVL") %>% filter(activity_code == "ARAB")
# arabs <- arabs %>% select(region, activity_code, year, scenario, value)
#
#