Leo96
Leo96

Reputation: 499

R:Exportig dataframe inside a function

I just have a simple function-question for exporting data Frames to Access.

foo_export <- function(Export_table){
        channel <- odbcDriverConnect(Path)
        sqlSave(channel,dat=Export_table)
        close(channel)
}
foo_export(Test)

If I´m using the data.frame "Test" as an argument for the function, so if I´m running foo_export(Test) the new Table in MS-Access is named Export_table and not as my stated name (Test). I tried to use "tablename" in SQLSave but it doesnt work. I read that functions just taking copies of data Frames and not the original one. Is that the Problem?

Upvotes: 0

Views: 44

Answers (2)

IRTFM
IRTFM

Reputation: 263352

If you wanted the name of the R object to get passed to the SQL engine programmatically, then try something like this:

foo_export <- function(Export_table){
        channel <- odbcDriverConnect(Path)
        t_name <- deparse(substitute(Export_table))
        sqlSave(channel,dat=Export_table, tablename=t_name)
        close(channel)
}

Upvotes: 0

jacobsg
jacobsg

Reputation: 121

There is an optional argument to supply a table name in the code like:

sqlSave(channel,dat=Export_table, tablename = 'foo')

The best option to make sure it is the right name is something like:

foo_export <- function(Export_table, table_name){
# table_name = character
    channel <- odbcDriverConnect(Path)
    sqlSave(channel,dat=Export_table, tablename = table_name)
    close(channel)
}
foo_export(Test)

Like

foo_export(Test, 'Test')

Hope that helps!

Upvotes: 1

Related Questions