Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beispiel Codeoptimierung create_json #2

Open
StefZimm opened this issue Jun 15, 2023 · 0 comments
Open

Beispiel Codeoptimierung create_json #2

StefZimm opened this issue Jun 15, 2023 · 0 comments

Comments

@StefZimm
Copy link
Collaborator

StefZimm commented Jun 15, 2023

Urzustand:

  • viele If- Bedingungen
  • redundante Inhalte
  • schwer lesbare Variablen
  • absolute Pfade in der Funktion
json_create_lite <- function(variable, varlabel, startyear, endyear, tabletype, exportpath, dataset) {
  if (dataset == "p_statistics") {  
  if (tabletype == "mean") {
    json_output <- jsonlite::toJSON(
      x = list(
        "dataset" = meta$dataset[meta$variable == variable],
        "title" = varlabel,
        "variable" = variable,
        "statistics" = c("mean", "median"),
        "dimensions" = list(
          list("variable" = meta$variable[meta$variable == "alter_gr"], 
               "label" = meta$label_de[meta$variable == "alter_gr"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$alter_gr))))))),
          list("variable" = meta$variable[meta$variable == "sex"], 
               "label" = meta$label_de[meta$variable == "sex"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sex))))))),
          list("variable" = meta$variable[meta$variable == "bula_h"], 
               "label" = meta$label_de[meta$variable == "bula_h"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bula_h))))))),
          list("variable" = meta$variable[meta$variable == "bildungsniveau"], 
               "label" = meta$label_de[meta$variable == "bildungsniveau"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bildungsniveau))))))),
          list("variable" = meta$variable[meta$variable == "sampreg"], 
               "label" = meta$label_de[meta$variable == "sampreg"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sampreg))))))),
          list("variable" = meta$variable[meta$variable == "migback"], 
               "label" = meta$label_de[meta$variable == "migback"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$migback))))))),
          list("variable" = meta$variable[meta$variable == "regtyp"], 
               "label" = meta$label_de[meta$variable == "regtyp"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$regtyp)))))))
        ),
        "description_de" = meta$description[meta$variable==variable],
        "start_year" = startyear,
        "end_year" = endyear,
        "types" = list("numerical")
      ), encoding = "UTF-8", pretty = TRUE, auto_unbox=TRUE
    )
    
    file_handler <- file("meta.json")
    writeLines(json_output, exportpath, useBytes=TRUE)
    close(file_handler)
  }
  
  if (tabletype == "prop") {
    json_output <- jsonlite::toJSON(
      x = list(
        "dataset" = meta$dataset[meta$variable == variable],
        "title" = varlabel,
        "variable" = variable,
        "statistics" = "percent",
        "dimensions" = list(
          list("variable" = meta$variable[meta$variable == "alter_gr"], 
               "label" = meta$label_de[meta$variable == "alter_gr"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$alter_gr))))))),
          list("variable" = meta$variable[meta$variable == "sex"], 
               "label" = meta$label_de[meta$variable == "sex"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sex))))))),
          list("variable" = meta$variable[meta$variable == "bula_h"], 
               "label" = meta$label_de[meta$variable == "bula_h"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bula_h))))))),
          list("variable" = meta$variable[meta$variable == "bildungsniveau"], 
               "label" = meta$label_de[meta$variable == "bildungsniveau"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bildungsniveau))))))),
          list("variable" = meta$variable[meta$variable == "sampreg"], 
               "label" = meta$label_de[meta$variable == "sampreg"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sampreg))))))),
          list("variable" = meta$variable[meta$variable == "migback"], 
               "label" = meta$label_de[meta$variable == "migback"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$migback))))))),
          list("variable" = meta$variable[meta$variable == "regtyp"], 
               "label" = meta$label_de[meta$variable == "regtyp"],
               "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$regtyp)))))))
        ),
        "description_de" = meta$description[meta$variable==variable],
        "start_year" = startyear,
        "end_year" = endyear,
        "types" = "categorical"
      ), encoding = "UTF-8", pretty = TRUE, auto_unbox=TRUE
    )
    
    file_handler <- file("meta.json")
    writeLines(json_output, exportpath, useBytes=TRUE)
    close(file_handler)
  }
  }
  if (dataset == "h_statistics") {  
    if (tabletype == "mean") {
      json_output <- jsonlite::toJSON(
        x = list(
          "dataset" = meta$dataset[meta$variable == variable],
          "title" = varlabel,
          "variable" = variable,
          "statistics" = c("mean", "median"),
          "dimensions" = list(
           list("variable" = meta$variable[meta$variable == "bula_h"], 
                 "label" = meta$label_de[meta$variable == "bula_h"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bula_h))))))),
            list("variable" = meta$variable[meta$variable == "sampreg"], 
                 "label" = meta$label_de[meta$variable == "sampreg"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sampreg))))))),
            list("variable" = meta$variable[meta$variable == "regtyp"], 
                 "label" = meta$label_de[meta$variable == "regtyp"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$regtyp))))))),
            list("variable" = meta$variable[meta$variable == "hhtyp"], 
                 "label" = meta$label_de[meta$variable == "hhtyp"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$hhtyp))))))),
            list("variable" = meta$variable[meta$variable == "quintil"], 
                 "label" = meta$label_de[meta$variable == "quintil"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$quintil)))))))
          ),
          "description_de" = meta$description[meta$variable==variable],
          "start_year" = startyear,
          "end_year" = endyear,
          "types" = list("numerical")
        ), encoding = "UTF-8", pretty = TRUE, auto_unbox=TRUE
      )
      
      file_handler <- file("meta.json")
      writeLines(json_output, exportpath, useBytes=TRUE)
      close(file_handler)
    }
    
    if (tabletype == "prop") {
      json_output <- jsonlite::toJSON(
        x = list(
          "dataset" = meta$dataset[meta$variable == variable],
          "title" = varlabel,
          "variable" = variable,
          "statistics" = "percent",
          "dimensions" = list(
            list("variable" = meta$variable[meta$variable == "bula_h"], 
                 "label" = meta$label_de[meta$variable == "bula_h"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$bula_h))))))),
            list("variable" = meta$variable[meta$variable == "sampreg"], 
                 "label" = meta$label_de[meta$variable == "sampreg"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$sampreg))))))),
            list("variable" = meta$variable[meta$variable == "regtyp"], 
                 "label" = meta$label_de[meta$variable == "regtyp"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$regtyp))))))),
            list("variable" = meta$variable[meta$variable == "hhtyp"], 
                 "label" = meta$label_de[meta$variable == "hhtyp"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$hhtyp))))))),
            list("variable" = meta$variable[meta$variable == "quintil"], 
                 "label" = meta$label_de[meta$variable == "quintil"],
                 "values" = list(as.list(str_trim(gsub("[[0-9]+]", "", levels(factor(data.file.fac$quintil)))))))
          ),
          "description_de" = meta$description[meta$variable==variable],
          "start_year" = startyear,
          "end_year" = endyear,
          "types" = "categorical"
        ), encoding = "UTF-8", pretty = TRUE, auto_unbox=TRUE
      )
      
      file_handler <- file("meta.json")
      writeLines(json_output, exportpath, useBytes=TRUE)
      close(file_handler)
    }
  }
}

Neue Variante:

  • Mehr Nutzung von Listen
  • weniger redundante Codezeilen
  • Weniger Ifs
  • Lesbarere Variablen
################################################################################
# creation of json metadata

#' @title json_create
#'
#' @description json_create creates json metadata
#' 
#' @param table data.frame from which json metadata should be created
#' @param variable variable name as character
#' @param table_type table type (e.g. "numerical", "categorical")
#'
#' @author Stefan Zimmermann, \email{[email protected]}
#' @keywords
#'

json_create <-
  function(table,
           variable,
           table_type) {
    
    all_grouping_variables <- metadaten_variables$variable[
      metadaten_variables$statistical_type == "dimension" &
        metadaten_variables$variable  != "syear"
    ]
    
    exportfile <- paste0(export_path, table_type, "/",
                         variable, "/meta.json")
    
    grouping_information <- list(NULL)
    
    for (groupingvar in all_grouping_variables) {
      grouping_information[[groupingvar]] <-
        list(
          "variable" = metadaten_variables$variable[
            metadaten_variables$variable == groupingvar
          ],
          "label" = metadaten_variables$label_de[
            metadaten_variables$variable == groupingvar
          ],
          "values" = list(as.list(stringr::str_trim(
            gsub("[[0-9]+]", "", levels(
              factor(datafile_with_labels[[groupingvar]])
            ))
          )))
        )
    }
    
    statistics <- list(NULL)
    statistics[["numerical"]] <- 
      numeric_statistics_column_names
    statistics[["categorical"]] <- 
      categorical_statistics_column_names

    json_output <- jsonlite::toJSON(
      x = list(
        "title" = metadaten_variables$label_de[
          metadaten_variables$variable == variable
        ],
        "variable" = metadaten_variables$variable[
          metadaten_variables$variable == variable
        ],
        "statistics" = statistics[[table_type]],
        "dimensions" = grouping_information,
        "description_de" = metadaten_variables$description_de[
          metadaten_variables$variable == variable
        ],
        "start_year" = min(table$year),
        "end_year" = max(table$year),
        "types" = table_type
      ),
      encoding = "UTF-8",
      pretty = TRUE,
      auto_unbox = TRUE
    )
    file_handler <- file("meta.json")
    writeLines(json_output, exportfile, useBytes = TRUE)
    close(file_handler)
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant