create a list of tables available in enviroment

My codes are creating so many tables but my question is do we have any solution to makes a list of tables created at the end of code

i have approx 500-1k tables with names (t1,t2,t3....t500)

df <- mtcars[1:10,]

t1 <- janitor::tabyl(df$disp)

t2 <- table(df$disp,df$wt,df$drat)

at the end i want to make a list list of tables like

tbls <- c(t1,t2,t3,t4.........t500)

do we have any solution for this ....??

df <- mtcars[1:10,]

t1 <- janitor::tabyl(df$disp)

t2 <- table(df$disp,df$wt,df$drat)

ls()

gottem <- mget(setdiff(ls(),"gottem"))

names(gottem)
str(gottem,max.level = 1)

Ideally avoid writing scripts that generate 100's of unrelated tables. Usually script generated tables have common features or relationships, and it might be better to make lists of them at the time of creation, that way you can manage what you have and where to look for them.

If the function class() returns a value that allows you to identify the objects you want to list, you can use a function like this one where I get a vector of the names of objects that have the class tbl

ClassFunc <- function(vec, ClassType) {
  TYPES <- vector(mode = "character", length = length(vec))
  for (i in seq_along(vec)) {
    ClassVec <- class(get(vec[i]))
    if (any(ClassVec == ClassType)) TYPES[i] <- vec[i]
  }
  return(TYPES[TYPES != ""])
}

ClassFunc(ls(), "tbl")

Of course, you can use ls() to search for objects whose name matches a pattern. That will work if you name all of your objects carefully.

generally the class of all the objects are dataframe ,etable and all the tables are like t1,t2,t3,t4.... but one thing is list of tables should be create as it is like
table_list <- list(t1,t2,t3,t4....) order of the tables should be consistent.

Lets say if will have many tables (etable,dataframes) assigned to (tb1, tb2,tb3,tb4,tb5.....tbn) and i want a list of those at the end. can we have any solution for this ..??

if you want to add a constraint that the component names begin with tb..

mget(setdiff(ls()[startsWith(ls(),"tb")],"gottem"))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.