I’m playing around with purrr’s map()
and partial()
and want to try and create a list of functions with pre-configured inputs and when I use those function it will return the functions output AND print out the function’s configuration (also as a validation check to make sure I’ve correct configured the function)
I’ve set up each function with pre-configured inputs without any problem but I cant get it to message the inputs.
library(tidyverse)
#arbitrary sequence
n_vec <- seq(5,100,5)
mean_vec <- seq(50,1000,50)
# create list of functions that are pre-configured with various inputs
rnorm_lst <- map2(
.x=n_vec
,.y=mean_vec
,.f=(.x,.y){
# When I use a function, I want it to message its input assumptions
print(.x)
print(.y)
# this is the underlying function that I want to create with partial
out <- partial(rnorm,n=.x,mean=.y)
return(out)
}
)
# create a name based on its configuration
names_lst <- paste0("n",n_vec,"_","mean",mean_vec)
# assign a name to each function
names(rnorm_lst) <- names_lst
# call a function to see if it works
rnorm_lst$n50_mean500()
The rnorm()
aspect of this works but its not printing or messaging its inputs.
I think it has to do with the environment it is? but I’m not sure.