From ca841a42c98905b4e85fb5fccbb1dbd03e6532dc Mon Sep 17 00:00:00 2001 From: Xin Zhao Date: Thu, 17 Aug 2023 22:24:42 -0400 Subject: [PATCH] fix environments --- R/constants.R | 5 ++-- R/xfaostat_L101_RawDataPreProc1_QCL.R | 11 ++++---- R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R | 15 ++++++----- R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R | 13 ++++----- R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R | 10 ++++--- R/xfaostat_L101_RawDataPreProc5_TCL.R | 19 +++---------- R/xfaostat_L101_RawDataPreProc6_TM.R | 31 +++++++++++----------- R/xfaostat_L101_RawDataPreProc7_FO.R | 11 ++++---- R/xfaostat_L101_RawDataPreProc8_RL_RFN.R | 9 +++---- R/xfaostat_L102_ProductionArea.R | 12 ++++++--- R/xfaostat_L103_ProducerPrices.R | 4 +-- R/xfaostat_L104_ProductionAreaAddFodder.R | 6 +++-- R/xfaostat_L106_FoodMacroNutrient.R | 8 +++--- R/xfaostat_helper_funcs.R | 26 +++++++++--------- 14 files changed, 90 insertions(+), 90 deletions(-) diff --git a/R/constants.R b/R/constants.R index 5a53e182..a6dba92d 100644 --- a/R/constants.R +++ b/R/constants.R @@ -15,9 +15,8 @@ OUTPUT_Export_CSV <- TRUE ## Fao raw data folder DIR_RAW_DATA_FAOSTAT <- "inst/extdata/aglu/FAO/FAOSTAT" ## Output GCAM csv -#DIR_OUTPUT_CSV <- "inst/extdata/aglu/FAO/temp" -DIR_OUTPUT_CSV <- "outputs/CSV" -dir.create(file.path(DIR_OUTPUT_CSV), showWarnings = FALSE) +DIR_OUTPUT_CSV <- file.path("outputs", "CSV") +dir.create(DIR_OUTPUT_CSV, recursive = T, showWarnings = FALSE) # Historical years of focus ---- diff --git a/R/xfaostat_L101_RawDataPreProc1_QCL.R b/R/xfaostat_L101_RawDataPreProc1_QCL.R index 9d893ad7..ce64f854 100644 --- a/R/xfaostat_L101_RawDataPreProc1_QCL.R +++ b/R/xfaostat_L101_RawDataPreProc1_QCL.R @@ -35,24 +35,25 @@ module_xfaostat_L101_RawDataPreProc1_QCL <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + Curr_Envir <- environment() if(Process_Raw_FAO_Data == FALSE) { - # Prebuilt data is read here ---- + # Load from Prebuilt data ---- QCL_wide <- extract_prebuilt_data("QCL_wide") QCL_area_code_map <- extract_prebuilt_data("QCL_area_code_map") } else { + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + # *[QCL] FAOSTAT Production and area ---- ## Load raw data - FAOSTAT_load_raw_data(DATASETCODE = "QCL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + FAOSTAT_load_raw_data(DATASETCODE = "QCL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) QCL %>% distinct(element_code, element) diff --git a/R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R b/R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R index f1cb3350..de762b69 100644 --- a/R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R +++ b/R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R @@ -40,27 +40,28 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + Curr_Envir <- environment() if(Process_Raw_FAO_Data == FALSE) { - # Prebuilt data is read here ---- + # Load from Prebuilt data ---- PP_wide <- extract_prebuilt_data("PP_wide") PD <- extract_prebuilt_data("PD") OA <- extract_prebuilt_data("OA") } else { + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + # Get area code ---- QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull() # *[PP] Producer price ---- - FAOSTAT_load_raw_data(DATASETCODE = "PP", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + FAOSTAT_load_raw_data(DATASETCODE = "PP", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) # check data PP %>% distinct(element, element_code, unit) @@ -128,7 +129,7 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) { # [PD] FAO_GDP_deflators ---- #************************************** - FAOSTAT_load_raw_data(DATASETCODE = "PD", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + FAOSTAT_load_raw_data(DATASETCODE = "PD", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) # read in Taiwan values as FAO does not have Taiwan price data # GDP_deflator_Taiwan @@ -183,7 +184,7 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) { # *[OA]: Population ---- - FAOSTAT_load_raw_data(DATASETCODE = "OA", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + FAOSTAT_load_raw_data(DATASETCODE = "OA", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) OA %>% distinct(element, element_code) OA %>% distinct(item, item_code) diff --git a/R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R b/R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R index e694d7ad..43b6f57f 100644 --- a/R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R +++ b/R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R @@ -37,9 +37,7 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + Curr_Envir <- environment() if(Process_Raw_FAO_Data == FALSE) { @@ -50,20 +48,23 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) { } else { + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + # Get area code ---- QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull() ## *[SCL] SUA: supply utilization accounting ---- - FAOSTAT_load_raw_data("SCL") # SUA 2010+ + FAOSTAT_load_raw_data("SCL", .Envir = Curr_Envir) # SUA 2010+ SCL %>% distinct(element, element_code, unit) if (is.character(SCL$item_code)){ - FAOSTAT_load_raw_data("SCL", GET_MAPPINGCODE = "ItemCodes") + FAOSTAT_load_raw_data("SCL", GET_MAPPINGCODE = "ItemCodes", .Envir = Curr_Envir) SCL %>% rename(cpc_code = item_code) %>% left_join_error_no_match( @@ -117,7 +118,7 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) { ## *[FBS] new food balance sheet (2010-) ---- ## Load raw data - FAOSTAT_load_raw_data("FBS") # New FBS 2010+ + FAOSTAT_load_raw_data("FBS", .Envir = Curr_Envir) # New FBS 2010+ FBS %>% distinct(element, element_code, unit) FBS %>% filter( diff --git a/R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R b/R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R index ffb904e5..80e8fb6a 100644 --- a/R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R +++ b/R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R @@ -38,9 +38,8 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- + Curr_Envir <- environment() - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) if(Process_Raw_FAO_Data == FALSE) { @@ -50,6 +49,9 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) { } else { + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + ## Get area code ---- QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull() @@ -59,7 +61,7 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) { ## *[FBSH] old food balance sheet (-2013) ---- - FAOSTAT_load_raw_data("FBSH") # Old FBS -2013 + FAOSTAT_load_raw_data("FBSH", .Envir = Curr_Envir) # Old FBS -2013 FBSH %>% distinct(element, element_code, unit) # Keep population (old) FBSH %>% @@ -80,7 +82,7 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) { ## *[CB] Non-food Balance ---- - FAOSTAT_load_raw_data("CB") # Old FBS-nonfood -2013 + FAOSTAT_load_raw_data("CB", .Envir = Curr_Envir) # Old FBS-nonfood -2013 CB %>% distinct(element, element_code, unit) # Keep population (old) diff --git a/R/xfaostat_L101_RawDataPreProc5_TCL.R b/R/xfaostat_L101_RawDataPreProc5_TCL.R index 8159a91f..ea56c66c 100644 --- a/R/xfaostat_L101_RawDataPreProc5_TCL.R +++ b/R/xfaostat_L101_RawDataPreProc5_TCL.R @@ -36,9 +36,8 @@ module_xfaostat_L101_RawDataPreProc5_TCL <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- + Curr_Envir <- environment() - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) if(Process_Raw_FAO_Data == FALSE) { @@ -48,24 +47,14 @@ module_xfaostat_L101_RawDataPreProc5_TCL <- function(command, ...) { } else { - - # FAOSTAT_RDS <- c("TCL") - # - # DIR_PREBUILT_FAOSTAT <- "data/PREBUILT_FAOSTAT" - # - # lapply(FAOSTAT_RDS, function(d){ - # assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds")))) - # assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))), - # envir = parent.env(environment())) - # }) - # - # TCL %>% filter(year >= min(FAOSTAT_Hist_Year_TCL)) %>% spread(year, value) -> TCL_wide + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull() # *[TCL] Gross trade ---- - FAOSTAT_load_raw_data("TCL") # Gross trade + FAOSTAT_load_raw_data("TCL", .Envir = Curr_Envir) # Gross trade TCL %>% distinct(element, element_code, unit) TCL %>% distinct(item, item_code) diff --git a/R/xfaostat_L101_RawDataPreProc6_TM.R b/R/xfaostat_L101_RawDataPreProc6_TM.R index 7bea92ac..be53dbba 100644 --- a/R/xfaostat_L101_RawDataPreProc6_TM.R +++ b/R/xfaostat_L101_RawDataPreProc6_TM.R @@ -35,9 +35,7 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + Curr_Envir <- environment() if(Process_Raw_FAO_Data == FALSE) { @@ -47,17 +45,8 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) { } else { - - # FAOSTAT_RDS <- c("TM_bilateral_wide") - # - # DIR_PREBUILT_FAOSTAT <- "data" - # - # lapply(FAOSTAT_RDS, function(d){ - # assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds")))) - # assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))), - # envir = parent.env(environment())) - # }) - + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) # Get area code ---- @@ -66,7 +55,7 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) { # *[TM] Bilateral trade ---- #*FAO has better quality bilateral data since 1992, covering most SUA items - FAOSTAT_load_raw_data("TM") # Bilateral trade + FAOSTAT_load_raw_data("TM", .Envir = Curr_Envir) # Bilateral trade TM %>% # Only keep quantities for elements with a unit of tonnes @@ -143,3 +132,15 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) { stop("Unknown command") } } + +# FAOSTAT_RDS <- c("TM_bilateral_wide") +# +# DIR_PREBUILT_FAOSTAT <- "data" +# +# lapply(FAOSTAT_RDS, function(d){ +# assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds")))) +# assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))), +# envir = parent.env(environment())) +# }) + + diff --git a/R/xfaostat_L101_RawDataPreProc7_FO.R b/R/xfaostat_L101_RawDataPreProc7_FO.R index c9e36714..3039abcb 100644 --- a/R/xfaostat_L101_RawDataPreProc7_FO.R +++ b/R/xfaostat_L101_RawDataPreProc7_FO.R @@ -33,10 +33,7 @@ module_xfaostat_L101_RawDataPreProc7_FO <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + Curr_Envir <- environment() @@ -46,7 +43,11 @@ module_xfaostat_L101_RawDataPreProc7_FO <- function(command, ...) { } else { - FAOSTAT_load_raw_data(DATASETCODE = "FO", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) + + FAOSTAT_load_raw_data(DATASETCODE = "FO", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) # Only keep roundwood FO %>% filter(year %in% FAOSTAT_Hist_Year, diff --git a/R/xfaostat_L101_RawDataPreProc8_RL_RFN.R b/R/xfaostat_L101_RawDataPreProc8_RL_RFN.R index 30338855..84364a28 100644 --- a/R/xfaostat_L101_RawDataPreProc8_RL_RFN.R +++ b/R/xfaostat_L101_RawDataPreProc8_RL_RFN.R @@ -35,10 +35,7 @@ module_xfaostat_L101_RawDataPreProc8_RL_RFN <- function(command, ...) { all_data <- list(...)[[1]] - # Load required inputs ---- - - get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) - + Curr_Envir <- environment() if(Process_Raw_FAO_Data == FALSE) { @@ -50,8 +47,10 @@ module_xfaostat_L101_RawDataPreProc8_RL_RFN <- function(command, ...) { } else { + # Load required inputs ---- + get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) - FAOSTAT_load_raw_data(DATASETCODE = "RL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT) + FAOSTAT_load_raw_data(DATASETCODE = "RL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir) RL %>% filter(year %in% FAOSTAT_Hist_Year, diff --git a/R/xfaostat_L102_ProductionArea.R b/R/xfaostat_L102_ProductionArea.R index 1e079d3b..8dd9aff2 100644 --- a/R/xfaostat_L102_ProductionArea.R +++ b/R/xfaostat_L102_ProductionArea.R @@ -354,9 +354,13 @@ module_xfaostat_L102_ProductionArea <- function(command, ...) { FBS %>% distinct(element, element_code, unit) # Get fish items through mapping - + Curr_Envir <- environment() checkitem <- - FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item") %>% + FF_join_checkmap(DFs = c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), + COL_by = "item_code", + COL_rename = "item", + .ENVIR = Curr_Envir + ) %>% mutate(match = if_else(QCL_COMM_AN_PRIMARY_item == FAO_an_items_PRODSTAT_item, T, F)) @@ -467,13 +471,13 @@ module_xfaostat_L102_ProductionArea <- function(command, ...) { # P.S. Check primary product mapping ---- # checkitem <- - # FF_join_checkmap(c("QCL_COMM_CROP_PRIMARY", "FAO_ag_items_PRODSTAT"), "item_code", "item") %>% + # FF_join_checkmap(c("QCL_COMM_CROP_PRIMARY", "FAO_ag_items_PRODSTAT"), "item_code", "item",.ENVIR = Curr_Envir) %>% # mutate(match = if_else(QCL_COMM_CROP_PRIMARY_item == FAO_ag_items_PRODSTAT_item , T, F)) # checkitem %>% filter(is.na(match)|match == F) # # 160 primary items (matching here) + 15/16 fodder crops # # checkitem <- - # FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item") %>% + # FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item",.ENVIR = Curr_Envir) %>% # mutate(match = if_else(QCL_COMM_AN_PRIMARY_item == FAO_an_items_PRODSTAT_item, T, F)) # checkitem %>% filter(is.na(match)|match == F) # # Snails, not sea production came from SCL diff --git a/R/xfaostat_L103_ProducerPrices.R b/R/xfaostat_L103_ProducerPrices.R index 2b0113e9..12818b5e 100644 --- a/R/xfaostat_L103_ProducerPrices.R +++ b/R/xfaostat_L103_ProducerPrices.R @@ -66,9 +66,9 @@ module_xfaostat_L103_ProducerPrices <- function(command, ...) { #FF_check_count_plot(QCL_PRIMARY) # 205 items #FF_check_count_plot(PP) # PP %>% distinct(area) - + Curr_Envir <- environment() # check items - FF_join_checkmap(c("QCL_PRIMARY", "PP"), "item_code", "item") -> A + FF_join_checkmap(c("QCL_PRIMARY", "PP"), "item_code", "item", .ENVIR = Curr_Envir) -> A A %>% filter(!is.na(QCL_PRIMARY_item)) %>% filter(is.na(PP_item)) -> PP_NO_DATA_ITEM # 20 items out of 205 were missing; mostly animal offals, fats or skins diff --git a/R/xfaostat_L104_ProductionAreaAddFodder.R b/R/xfaostat_L104_ProductionAreaAddFodder.R index 11a00cb0..9883e001 100644 --- a/R/xfaostat_L104_ProductionAreaAddFodder.R +++ b/R/xfaostat_L104_ProductionAreaAddFodder.R @@ -107,9 +107,11 @@ module_xfaostat_L104_ProductionAreaAddFodder <- function(command, ...) { "QCL_area_code_map")-> QCL_FODDERCROP - + Curr_Envir <- environment() # 160 primary items +16 fodder crops - FF_join_checkmap(c("QCL_FODDERCROP", "FAO_ag_items_PRODSTAT"), "item_code", "item") %>% + FF_join_checkmap(DFs = c("QCL_FODDERCROP", "FAO_ag_items_PRODSTAT"), + COL_by = "item_code", + COL_rename = "item", .ENVIR = Curr_Envir) %>% mutate(match = if_else(QCL_FODDERCROP_item == FAO_ag_items_PRODSTAT_item , T, F))-> checkitem checkitem %>% filter(match == T) # 15 fodder crops + 1 pumpkin removed in mapping diff --git a/R/xfaostat_L106_FoodMacroNutrient.R b/R/xfaostat_L106_FoodMacroNutrient.R index 3299eccf..0b7df55a 100644 --- a/R/xfaostat_L106_FoodMacroNutrient.R +++ b/R/xfaostat_L106_FoodMacroNutrient.R @@ -84,14 +84,14 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) { # Quick checks---- #******************************************* # Check area - + Curr_Envir <- environment() # 19 countries not in FBS/SUA but in QCL # including (3 populous) Somalia, South Sudan, and Singapore (UAE was recently added in FAOSTAT) - FF_join_checkmap(c("FBS", "SCL"), "area_code", "area") -> checkarea + FF_join_checkmap(c("FBS", "SCL"), "area_code", "area", .ENVIR = Curr_Envir) -> checkarea #Check element - FF_join_checkmap(c("FBS", "SCL"), "item_code", "item") -> checkitem - FF_join_checkmap(c("FBS", "SCL"), "element_code", "element") -> checkelement + FF_join_checkmap(c("FBS", "SCL"), "item_code", "item", .ENVIR = Curr_Envir) -> checkitem + FF_join_checkmap(c("FBS", "SCL"), "element_code", "element", .ENVIR = Curr_Envir) -> checkelement #******************************************* # Check calories calculation first to make sure we can trace appropriately # the key here is elements, deal with missing values, and conversion rate (cal/g) diff --git a/R/xfaostat_helper_funcs.R b/R/xfaostat_helper_funcs.R index be25c388..d9f311b2 100644 --- a/R/xfaostat_helper_funcs.R +++ b/R/xfaostat_helper_funcs.R @@ -34,8 +34,9 @@ FAOSTAT_metadata <- function (code = NULL){ #' @description Read csv data and "." in column name is substituted with "_". #' #' @param DATASETCODE Dataset code in FAO metadata or the name of a csv file. -#' @param GET_MAPPINGCODE if NULL return data if char return other mapping files +#' @param GET_MAPPINGCODE If NULL return data if char return other mapping files #' @param DATA_FOLDER Path to the folder storing the data. +#' @param .Envir Qutput environment #' #' @importFrom readr read_csv #' @importFrom magrittr %>% @@ -44,7 +45,8 @@ FAOSTAT_metadata <- function (code = NULL){ FAOSTAT_load_raw_data <- function(DATASETCODE, DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, - GET_MAPPINGCODE = NULL){ + GET_MAPPINGCODE = NULL, + .Envir = .GlobalEnv){ assertthat::assert_that(is.character(DATASETCODE)) assertthat::assert_that(is.character(DATA_FOLDER)) @@ -66,11 +68,8 @@ FAOSTAT_load_raw_data <- function(DATASETCODE, df <- readr::read_csv(unz(zip_file_name, csv_file_name), col_types = NULL) # Lower case col names and use _ as delimiter names(df) <- tolower(gsub("\\.| ", "_", names(df))) - # Assigned to parent env - #assign(CODE, df, envir = parent.frame()) - assign(CODE, df, envir = parent.env(environment())) - # Assigned to current env - #assign(CODE, df, envir = .GlobalEnv) + # Assigned to .Envir + assign(CODE, df, envir = .Envir) } } else if(is.character(GET_MAPPINGCODE) == T){ @@ -88,10 +87,8 @@ FAOSTAT_load_raw_data <- function(DATASETCODE, df <- readr::read_csv(unz(zip_file_name, csv_file_name), col_types = NULL) # Lower case col names and use _ as delimiter names(df) <- tolower(gsub("\\.| ", "_", names(df))) - # Assigned to parent env - assign(paste0(CODE, "_", GET_MAPPINGCODE), df, envir = parent.env(environment())) - # Assigned to current env - #assign(paste0(CODE, "_", GET_MAPPINGCODE), df, envir = .GlobalEnv) + # Assigned to .Envir + assign(CODE, df, envir = .Envir) } } else {stop("Wrong GET_MAPPINGCODE")} @@ -301,16 +298,19 @@ FF_FILL_NUMERATOR_DENOMINATOR <- function(.DF, NUMERATOR_c, DENOMINATOR_c, #' #' @param DFs Data frames to be full joined. #' @param COL_by By variable in join. +#' @param .ENVIR Environment of the input data frames #' @param COL_rename Other common variables to rename (by adding df names as prefix) before the join. +#' #' @importFrom magrittr %>% #' @importFrom dplyr rename_at select any_of all_of full_join #' @importFrom purrr reduce #' @return A joined data frame #' @export -FF_join_checkmap <- function(DFs, COL_by, COL_rename){ +FF_join_checkmap <- function(DFs, COL_by, COL_rename, + .ENVIR = .GlobalEnv){ lapply(DFs, function(df){ - get(df, envir = parent.frame(n = 3)) %>% + get(df, envir = .ENVIR) %>% select(all_of(COL_by), any_of(COL_rename)) %>% distinct() %>% dplyr::rename_at(vars(any_of(COL_rename)), list(~paste0(df, "_", .))) }) %>% purrr:: reduce(full_join, by = COL_by)