-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
116 lines (95 loc) · 4.66 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
112
113
114
115
116
#options
#---------------------------------------------------------------------------------------
options(
encoding = "UTF-8",
stringsAsFactors = FALSE,
dplyr.summarise.inform = FALSE
)
#environment
#--------------------------------------------------------------------------------------
try(dotenv::load_dot_env(file = ".REnviron"), silent = TRUE)
#packages
#---------------------------------------------------------------------------------------
source("assets/core/package_utils.R")
loadAppPackages()
#config
#---------------------------------------------------------------------------------------
#default config_file path for DEPLOYMENT
config_file <- "/etc/shiny-server/config.yml"
#local configuration
#If you are an R developer, you need to create a .REnviron file (no file extension) in /shiny-calipseo dir
#The file should include the local path for your shiny config file in that way:
#CALIPSEO_SHINY_CONFIG=<your config path>
local_config_file <- Sys.getenv("CALIPSEO_SHINY_CONFIG")
if(nzchar(local_config_file)) config_file <- local_config_file
appConfig <- suppressWarnings(yaml::read_yaml(config_file))
if(is.null(appConfig$auth)) appConfig$auth <- FALSE
#language (in case not part of configuration)
if(is.null(appConfig$language)) appConfig$language <- "en"
#shiny-storage check
default_store_dir <- "/srv/shiny-server/shiny-calipseo-store"
#shiny-storage in dev (assume Windows OS for now)
if(Sys.info()[["sysname"]] == "Windows") default_store_dir <- "out"
if(is.null(appConfig$store)) appConfig$store <- default_store_dir
if(!dir.exists(appConfig$store) && Sys.info()[["sysname"]] != "Windows") dir.create(appConfig$store)
#DB connections
#---------------------------------------------------------------------------------------
pool <- pool::dbPool(
drv = DBI::dbDriver(appConfig$openfismis$dbi$drv),
dbname = appConfig$openfismis$dbi$dbname,
host = appConfig$openfismis$dbi$host,
port = appConfig$openfismis$dbi$port,
user = appConfig$openfismis$dbi$user,
password = appConfig$openfismis$dbi$password,
bigint = "numeric" #required otherwise RMariaDB coerces integers as integer64
)
#global variables / environment
#---------------------------------------------------------------------------------------
CALIPSEO_SHINY_ENV <- new.env()
#utilities
#---------------------------------------------------------------------------------------
#core R script utils
core_assets <- list.files("assets/core", pattern = ".R", full.names = T)
for(core_asset in core_assets) source(core_asset)
addResourcePath(prefix = "calipseo-data", directoryPath = "../calipseo-data")
#country R script utils
country_assets <- list.files(path = file.path("./assets/country", appConfig$country_profile$iso3),
pattern = ".R", recursive = TRUE, full.names = TRUE)
for(country_asset in country_assets){ source(country_asset) }
#country profile
#---------------------------------------------------------------------------------------
appConfig <- loadCountryProfile(appConfig, pool)
print(appConfig$country_profile$data)
#country parameters
#---------------------------------------------------------------------------------------
COUNTRY_PARAMS <- accessCountryParam(pool)
HAS_REGMANGT <- ifelse(COUNTRY_PARAMS[COUNTRY_PARAMS$CODE=='REGMANGT',]$BOOLEAN == 1, TRUE, FALSE)
PREF_UNIT_WEIGHT<-accessCountryPrefUnitWeight(pool)
if(PREF_UNIT_WEIGHT$CODE=="lb") PREF_UNIT_WEIGHT$CODE <- "lbs" #patch to make sure unit conversion
PREF_CURRENCY<-accessCountryPrefCurrency(pool)[1,1]
#local datasets
#---------------------------------------------------------------------------------------
loadLocalCountryDatasets(appConfig)
#remote datasets
#---------------------------------------------------------------------------------------
loadRemoteReferenceDataset("cl_asfis_species","https://raw.githubusercontent.com/fdiwg/fdi-codelists/main/global/cwp/cl_asfis_species.csv")
loadRemoteReferenceDataset("cl_isscaap_group","https://raw.githubusercontent.com/fdiwg/fdi-codelists/main/global/cwp/cl_isscaap_group.csv")
#language/i18n
#---------------------------------------------------------------------------------------
#we extend appConfig with i18n iterms
appConfig$i18n <- getModuleI18nTerms()
#modules
#---------------------------------------------------------------------------------------
loadModuleScripts(appConfig)
#main Shiny scripts
#---------------------------------------------------------------------------------------
source("ui.R")
source("server.R")
#onStop
#---------------------------------------------------------------------------------------
onStop(function(){
cat("Closing DB pool connection\n")
pool::poolClose(pool)
cat("Removing CALIPSEO_SHINY_ENV environment\n")
rm(CALIPSEO_SHINY_ENV)
})