2017-10-06 52 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) 
}