Let's say, for the sake of the example, that I have a list of departments. Everyone of them is on a separate table named "departmentName", so I created a list this way.
depts <- c("financial","sales",.....)
and then iterate to get members this way creating a function:
get.employees <- function(tablename) {
con <- DBI::dbConnect(connectiondata....)
query <- glue::glue("select name,position,area from {tablename}")
assign(tablename,
dplyr::tbl(conn, sql(query)) %>% collect())
}
lapply(depts,get.employees)
It works fine but It returned a list of data frames with no name assigned to every element as I was expecting.
I need every dataframe named as the department name.
lapply(setNames(nm = depts), get.employees)