I was wondering if it was possible to set the names of elements of a list at the end of a pipeline code.
data <- input_vars %>%
purrr::map(get_data)
names(data) <- input_vars
Currently I pass a string of variables into a function which retrieves a list of dataframes. Unfortunately this list does not automatically have named elements, but I add them "manually" afterwards. In order to improve readability I would like to have something as follows
data <- input_vars%>%
purrr::map(get_comm_data) %>%
setnames(input_vars)
However, this gives me the following error: Error in setnames(., input_vars) : x is not a data.table or data.frame
Does someone have an elegant solution to this?
Any help is appreciated!
To set names of an object in a pipeline, use
setNames
orpurrr::set_names
With
purrr::map
you could also name yourinput_vars
asmap
keep names.