Recursive Function Returning Empty List

46 Views Asked by At

I am working on a BOM structure in R (using database but also using Tibbles) where I create a BOM tree, and calculate the costs based on a function "calculateTotalCosts" The results are correctly calculated when I call the function. However, they are not correctly stored in the list. This is my BOM structure:

   bomID fgID       fgName matID proCoMAT unitOfMeasure parentBomID unitCost totalCost
1      1    1 FoodBowlBlue     1    0.056            kg           5     0.25        NA
2      2    1 FoodBowlBlue     2    0.056            kg           5     0.15        NA
3      3    1 FoodBowlBlue     1    0.024            kg           6     0.25        NA
4      4    1 FoodBowlBlue     2    0.240            kg           6     0.15        NA
5      5    1 FoodBowlBlue     5    1.000            PC           8       NA        NA
6      6    1 FoodBowlBlue     6    1.000            PC           8       NA        NA
7      7    1 FoodBowlBlue     4    0.020            kg           8     0.10        NA
8      8    1 FoodBowlBlue    NA    1.000            PC          NA       NA        NA
9      9    2  FoodBowlRed     1    0.056            kg          13     0.25        NA
10    10    2  FoodBowlRed     3    0.056            kg          13     0.15        NA
11    11    2  FoodBowlRed     1    0.024            kg          14     0.25        NA
12    12    2  FoodBowlRed     3    0.240            kg          14     0.15        NA
13    13    2  FoodBowlRed     7    1.000            PC          16       NA        NA
14    14    2  FoodBowlRed     8    1.000            PC          16       NA        NA
15    15    2  FoodBowlRed     4    0.020            kg          16     0.10        NA
16    16    2  FoodBowlRed    NA    1.000            PC          NA       NA        NA

I need to calculate the totalCosts. It can be done using this recursive function:

calculateTotalCostsTest3 <- function(node) {
    costs <- list()
    if (is.null(node$children) || length(node$children) == 0) {
        if (!is.null(node$data$unitCost) && !is.null(node$data$proCoMAT)) {
            node$totalCost <- node$data$unitCost * node$data$proCoMAT
        } else {
            node$totalCost <- 0 
        }
        
        if (!is.null(node$data$bomID)) {
            costs[[as.character(node$data$bomID)]] <- node$totalCost
            print(paste("Stored bomID:", node$data$bomID, "Total cost:", node$totalCost))
        }
    } else {
        node$totalCost <- 0  # Initialize total cost for the node
        
        childCosts <- list()
        
        for (child in node$children) {
            childTotalCost <- calculateTotalCostsTest3(child)
            if (length(childTotalCost) > 0) {
                childCosts <- c(childCosts, childTotalCost)
                node$totalCost <- node$totalCost + as.numeric(childTotalCost)
            }
        }
        
        if (!is.null(node$data$bomID)) {
            if (node$totalCost > 0) {
                costs[[as.character(node$data$bomID)]] <- node$totalCost
                print(paste("Stored bomID:", node$data$bomID, "Total cost:", node$totalCost))
            } 
        }
    }
    
    return(costs)
}



#Call of the function
RootNode <- list(
    text = "All Finished Goods",
    children = list()
)

# Assuming these are your FG IDs
finishedGoodIDs <- c(1, 2)  
for (fgID in finishedGoodIDs) {
    fgTree <- BuildTree(fgID)  # Generate each FG's tree
    RootNode$children <- c(RootNode$children, fgTree)  # Append FG tree to RootNode
}

# Calculate and capture total costs for the entire structure
allCosts <- calculateTotalCostsTest3(RootNode)

# Display all calculated costs
print(allCosts)

The total costs are correctly calculated using this function, after building the BOM tree I call it for the constructed tree that includes all the finished goods. The result is as follow:

[1] "Stored bomID: 1 Total cost: 0.014"
[1] "Stored bomID: 2 Total cost: 0.0084"
[1] "Stored bomID: 5 Total cost: 0.0224"
[1] "Stored bomID: 3 Total cost: 0.006"
[1] "Stored bomID: 4 Total cost: 0.036"
[1] "Stored bomID: 6 Total cost: 0.042"
[1] "Stored bomID: 7 Total cost: 0.002"
[1] "Stored bomID: 8 Total cost: 0.0664"
[1] "Stored bomID: 9 Total cost: 0.014"
[1] "Stored bomID: 10 Total cost: 0.0084"
[1] "Stored bomID: 13 Total cost: 0.0224"
[1] "Stored bomID: 11 Total cost: 0.006"
[1] "Stored bomID: 12 Total cost: 0.036"
[1] "Stored bomID: 14 Total cost: 0.042"
[1] "Stored bomID: 15 Total cost: 0.002"
[1] "Stored bomID: 16 Total cost: 0.0664"
list()

Now my problem is that it returns an empty list() instead of returning the list of bomIDs with the corresponding totalCosts. Maybe because it didnt store the results within the function's execution? I have no clue why or how to solve my issue. How can I solve it?? Please help! Thank you!

0

There are 0 best solutions below