[英]R user-defined function to import ACCESS table via ODBC
this might be something simple, but I couldn't figure out a viable solution. 这可能很简单,但是我找不到可行的解决方案。
My setup is: 我的设置是:
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Brazil.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.1 rpart_4.1-10
I'm building a function to import files from ACCESS DB via ODBC as folow: 我正在建立一个通过ODBC从ACCESS DB导入文件的功能,如下所示:
importa.sql <- function(someFile)
{
library(RODBC)
con <- odbcConnect("someTable")
qry<-paste("(","SELECT * FROM ",someFile,")")
someFile <- sqlQuery(con,qry,stringsAsFactors=FALSE)
}
I've tested each line and the code is working as expected. 我已经测试了每一行,并且代码按预期工作。 The problem is: when I run the function, it seems to work perfectly, but there's no file imported!!!
问题是:当我运行该函数时,它似乎运行良好,但是没有导入文件!!!
Does anyone could help me? 有人可以帮助我吗?
Your function return NULL
because the last statement of the function ir assignment to the local object someFile
. 您的函数返回
NULL
因为函数ir分配给本地对象someFile
的最后一条语句。 It would be good to close the connection. 最好关闭连接。 Try this function.
试试这个功能。
importa.sql <- function(someFile) {
library(RODBC)
con <- odbcConnect("someTable")
qry <- paste("(", "SELECT * FROM ", someFile, ")")
df <- sqlQuery(con, qry, stringsAsFactors = FALSE)
close(con)
return(df)
}
In case of someone has the same doubt, the solution is really really simple. 如果有人有同样的疑问,解决方案确实非常简单。
Just call the function this way: 只需这样调用函数:
file <- importa.sql(someFile)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.