R/get_template_merge.R

Defines functions get_template_merge

Documented in get_template_merge

#' @title Retrieve merging template
#' 
#' @description Goes through the template load data to retrieve all the attributes mentionned.
#' 
#' @author Briac LE RAY (briac.leray@partnre.com)
#' 
#' @return A string with the correct format for merging.
#' @param table_x The first table. Type = character
#' @param table_x The second table. Type = character
#' @param by_x The attributes from the first table used for the merge. Type = character
#' @param by_y The attributes from the second table used for the merge. Type = character
#' @param result The dataframe resulting from the merge. Type = character.
#' @param type_of_merge The type ofmerging chosen. Either "left", "right", "inner" or "full". Type = character

#' @examples get_template_merge(table_x = my_dataframe_1, table_y = my_dataframe_2, result = my_dataframe_3, by_x = "c('attribute_1', 'attribute_2')", by_y = "c('attribute_3', 'attribute_4')", type_of_merge = "left")


get_template_merge <- function(table_x, table_y, result, by_x, by_y, type_of_merge) {
  # Provides the template to add to the R file for merging.
  
  # table_x = name of the left table
  # table_y = name of the right table
  # by_x = attributes to use for the x table
  # by_y = attributes to use for the y table
  # type_of_merge = right or left or full or inner
  
  type_of_merge <- tolower(type_of_merge)
  
  if(type_of_merge == "left") {
    return(glue("# Merge {table_x} and {table_y} ----
                {result} = merge(x = {table_x},
                y = {table_y},
                by.x = {by_x},
                by.y = {by_y},
                all.x = TRUE)"))
  }
  else if (type_of_merge == "right") {
    return(glue("# Merge {table_x} and {table_y} ----
                {result} = merge(x = {table_x},
                y = {table_y},
                by.x = {by_x},
                by.y = {by_y},
                all.y = TRUE)"))
  }
  else if (type_of_merge == "inner") {
    return(glue("# Merge {table_x} and {table_y} ----
                {result} = merge(x = {table_x},
                y = {table_y},
                by.x = {by_x},
                by.y = {by_y}"))
  }
  else if (type_of_merge == "full") {
    return(glue("# Merge {table_x} and {table_y} ----
                {result} = merge(x = {table_x},
                y = {table_y},
                by.x = {by_x},
                by.y = {by_y},
                all = TRUE)"))
  }
}
Tiipiac/StressTestOptimization documentation built on Dec. 18, 2021, 5:08 p.m.