Search code examples
rlistrbind

feeding a list of objects to rbind.fill


I have a large number or tables in the environment I wish to bind into a single table. There are many tables in the environment, but all the ones I want to bind are similarly named ending in "_Fields". I wish to search the environment for all the so-named tables and bind them together.

Using the grep function I can search and find all the table names as follows:

mylist <- list(grep("_Fields", ls(), value=T))

This yields the following result for mylist:

 [[1]]
 [1] "Account_Fields"            "AccountHistory_Fields"     "Case_Fields"               "CaseHistory_Fields"        "Contact_Fields"           
 [6] "Lead_Fields"               "LeadHistory_Fields"        "Opportunity_Fields"        "OpportunityHistory_Fields" "User_Fields"

These are names of the desired tables in the environment, and I wish to bind them with the following:

tmp <- do.call(plyr::rbind.fill,mylist)

However all this yields is the list of names contained in mylist and not the bound tables. How do I get the list of objects to be passed to the function to bind the tables?

The following works, but the number of tables may vary and I would have to change this code each time.

mylist <- list(Account_Fields,Opportunity_Fields,Lead_Fields,Contact_Fields,
            User_Fields,Case_Fields,AccountHistory_Fields,OpportunityHistory_Fields,
            LeadHistory_Fields, CaseHistory_Fields)

tmp <- do.call(plyr::rbind.fill,mylist)```


Solution

  • We may use mget

    do.call(rbind.fill, mget(ls(pattern = "_Fields")))