I have a dataframe and I want to fit a mixR model for each column and print the plot. The code without using function without any problem, but when it was called in a function, an error happened. I checked them multiple times, but not able to find any abnormality. Any hint? Thanks.
library(mixR)
twins = data.frame(
apple = rnorm(100),
orange = rnorm(100, mean = 2),
banana = rnorm(100, mean = 5),
peach = rnorm(100, mean = 10)
)
fit = mixR::mixfit(twins[["apple"]], ncomp = 2)
print(plot(fit, what = "after_stat(density)", title = paste0("Gaussian mixR Model ", "apple"), cex.main = 20))
The chunk of code is normal, but the code below in a function returns an error:Error in eval(mc, environment()) : object 'column_names' not found
mixR_plot = function(data, column_names){
fit = mixR::mixfit(data[[column_names]], ncomp = 2)
print(plot(fit, what = "after_stat(density)", title = paste0("Gaussian mixR Model", column_names), cex.main = 20))
}
mixR_plot(twins, "apple")
It seems quite weird, I have spent hours for debugging.