Skip to content

Commit

Permalink
Merge pull request #18 from KWB-R/expand
Browse files Browse the repository at this point in the history
Add expand_to_all_key_combinations()
  • Loading branch information
hsonne committed Mar 18, 2024
2 parents 3731059 + 1856d46 commit cb61911
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ Remotes:
ByteCompile: true
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Config/testthat/edition: 3
38 changes: 35 additions & 3 deletions R/import_json_files_to_excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#' @param overwrite whether or not to overwrite the Excel \code{file} if it
#' exists. Default: \code{FALSE}.
#' @param open logical indicating whether or not to open the created Excel file
#' @param expand_keys If this argument is not \code{NULL} but a vector of (key)
#' column names, all sheets are expanded to the same number of rows. All
#' possible combinations of values in these key columns are then given in each
#' sheet even though there are no values for these key value combinations.
#' Default: \code{c("indicator", "process", "place", "exchange")}
#' @return path to created Excel file
#' @importFrom kwb.utils hsOpenWindowsExplorer substSpecialChars
#' @importFrom writexl write_xlsx
Expand All @@ -16,15 +21,23 @@ import_json_files_to_excel <- function(
json_dir,
file = file.path(json_dir, "umberto-results.xlsx"),
overwrite = FALSE,
open = TRUE
open = TRUE,
expand_keys = c("process", "place", "exchange")
)
{
#kwb.utils::assignPackageObjects("kwb.umberto");`%>%` <- magrittr::`%>%`

sheets <- json_dir %>%
import_rawdata_json(add_place = TRUE, old_format = FALSE) %>%
get_core_data() %>%
core_data_to_wide() %>%
split_by_columns("indicator") %>%
stats::setNames(sprintf("m%02d", seq_along(.)))
split_by_columns("indicator")

names(sheets) <- sprintf("m%02d", seq_along(sheets))

if (!is.null(expand_keys)) {
sheets <- expand_to_all_key_combinations(sheets, keys = expand_keys)
}

file_exists <- file.exists(file)
quoted_file <- dQuote(file, '"')
Expand Down Expand Up @@ -100,3 +113,22 @@ core_data_to_wide <- function(data)
"exchange"
))
}

# expand_to_all_key_combinations -----------------------------------------------
expand_to_all_key_combinations <- function(sheets, keys)
{
level_combis <- unique(do.call(rbind, lapply(
sheets,
FUN = function(sheet) unique(kwb.utils::selectColumns(sheet, keys))
)))

lapply(sheets, function(y) {
indicator <- unique(kwb.utils::selectColumns(y, "indicator"))
stopifnot(length(indicator) == 1L)
dplyr::left_join(
x = data.frame(indicator = indicator, level_combis),
y = y,
by = c("indicator", keys)
)
})
}
15 changes: 8 additions & 7 deletions R/import_rawdata_json.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
if (FALSE)
{
#remotes::install_github("kwb-r/kwb.umberto@expand")

json_dir <- "~/../Downloads/S/support/fabian/R-Umberto/Umberto11"
json_dir <- "~/../Downloads/S/support/lea/umberto"

json_dir <- "Y:/WWT_Department/Projects/Ultimate/Data_Work-packages/WP2-assessments/CS9_Kalundborg/LCAumberto11"

result_file <- kwb.umberto:::import_json_files_to_excel(json_dir, file = tempfile(fileext = ".xlsx"))

files <- dir(json_dir, "\\.json$", full.names = TRUE)

result_old_with_place <- kwb.umberto:::import_rawdata_json(
Expand All @@ -14,11 +19,7 @@ if (FALSE)
kwb.umberto:::import_rawdata_json(files = file, add_place = TRUE)
})

stopifnot(identical(do.call(rbind, results), result))

kwb.utils::assignPackageObjects("kwb.umberto")

contents <- read_json_files(json_dir)
contents <- kwb.umberto:::read_json_files(json_dir)

#jsTree::jsTree
}
Expand All @@ -45,7 +46,7 @@ import_rawdata_json <- function(
files = NULL
)
{
#kwb.utils::assignPackageObjects("kwb.umberto")
#kwb.utils::assignPackageObjects("kwb.umberto");files = NULL
contents <- read_json_files(json_dir, files = files)

result_tables <- lapply(contents, to_tables)
Expand Down
9 changes: 8 additions & 1 deletion man/import_json_files_to_excel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb61911

Please sign in to comment.