0

I need have a data.table named "clients", the name is same that is in my database. (this example is with table "clients" but i will need more tables with others names)

tbl_import <- function (table)
{
        sql<-glue_sql(paste0("select * from table"),.con=con)
    
    table <- dbGetQuery(con,sql)
    setDT(table)
      
    table <<- copy(table)
        table <<- table ##I replace with this too in another try but didn't work either
}
tbl_import("clients")

When i run this i don't have the data.table with the name of "clients",I get a table with name "table"

0

2 Answers 2

2

Use assign() to create a variable with a custom name (table_name) in the caller environment (parent.frame()

tbl_import <- function (table_name) {
  sql <- glue_sql("select * from {`table_name`}",.con=con)
  x <- dbGetQuery(con,sql)
  assign(table_name, setDT(x), parent.frame())
}
tbl_import("clients")
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thank you so much for the help! "assign" was the key
1

Just assign the function to a new name.

clients <- tbl_import("clients")

1 Comment

It works but Andrew response is more what i was looking for. with this solution i have to put twice the name of table and is just what i want to avoid. but thanks for response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.