I am using a function called "boss" which calls another function called "worker". The worker function takes a dataset (e.g. mtcars) and uses an algorithm provided as a string (e.g. "Algorithm_1") to calculate an output. The worker function returns a dataframe which specifies the data and algorithm used to calculate the respective output.
I want to pass the data object to the boss function which calls the worker funtion three times and makes the same calculations with the different algorithms and combines all results in one dataframe in order to compare them.
My problem is that boss() returns "data" instead of "mtcars" in the column "Data" of the returned object. The argument "data" in the boss function somewhat masks the actual name of the dataset (e.g. mtcars).
This is my code:
# Load data
data("mtcars")
head(mtcars)
# Define worker function
worker <- function(data, algorithm){
# Model: For simplicity, I just generate a random number
output = runif(1)
# Get passed data name
data_name <- deparse(substitute(data))
# Create results dataframe and store used data and algorithm to compute test statistic
results <- data.frame(Data = data_name,
Algorithm = algorithm,
Output = output)
# Return used data and algorithm and the respective output
return(results)
}
# Define boss function which calls worker function
boss <- function(data){
# Run and save individual objects (same data but different algorithm)
model1 <- worker(data, "Algorithm_1")
model2 <- worker(data, "Algorithm_2")
model3 <- worker(data, "Algorithm_3")
# combine and output all models in one dataframe
return(rbind(model1, model2, model3))
}
worker_output <- worker (mtcars, "Algorithm_1")
worker_output
Data Algorithm Output
1 mtcars Algorithm_1 0.9275785
boss_output <- boss(mtcars)
boss_output
Data Algorithm Output
1 data Algorithm_1 0.9857309
2 data Algorithm_2 0.5066107
3 data Algorithm_3 0.2939690
As you can see, the worker function displays the actual name of the data (e.g. mtcars) used. However, the boss function displays just "data" and not "mtcars".
Any help how to change that would be very appreciated.
Thank you.