2017-10-06 53 views
0

我只是有一个简单的函数问题来将数据帧导出为Access。R:在函数内部导出数据帧

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

如果I'm使用data.frame“测试”作为函数的参数,所以如果I'm运行foo_export(测试)在MS-Access中的新的表被命名为Export_table而不是我的声明的名称(测试)。我试图在SQLSave中使用“tablename”,但它不起作用。我只是读取数据帧的副本,而不是原来的功能。这是问题吗?

回答

1

有像代码提供的是表名的可选参数:

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

最好的办法,以确保它是正确的名称是一样的东西:

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) 

foo_export(Test, 'Test') 

希望帮助!

0

如果你想将R对象的名称得到传递给SQL引擎编程,然后再尝试这样的事:

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