Purrr equivalent to plyr::ldply

This is my first question in the community forums so I hope this isn't too long or in the wrong category.

background

I am writing a program that establishes a database connection (via ODBC), retrieves table and view information for schemas within the database then grabs unique table and view names. I am able to successfully display column information and other meta-data for each table and view I retrieved from the schema using plyr.

question

I have been unsuccessful in changing my plyr line of code to purr. How could this be done using Purrr (i.e. the last line below)?

code

library("RODBC",      warn.conflicts = FALSE)
library("dplyr",           warn.conflicts = FALSE)
library("stringr",         warn.conflicts = FALSE)
library("fst",               warn.conflicts = FALSE)

con <- RODBC::odbcConnect(dsn = db, believeNRows = TRUE)

db_objects     <- RODBC::sqlTables(con, tableType = c("TABLE", "VIEW"))
table_names  <- db_objects$TABLE_NAME %>% base::unique()

tables_all_cols  <- plyr::ldply(table_names, function(x) RODBC::sqlColumns(con, x))
1 Like

This should work for you:

tables_all_col <- purrr::map_df(table_names, ~ RODBC::sqlColumns(con, .x))
4 Likes

Thank you! I will give it a try soon. :slight_smile: