我昨天问了这个问题,得到了回答,但我认为他们并没有真正理解我的问题。我需要从数据库中提取数据。但是用户提供了变量列表。我需要我的代码循环遍历每个请求的变量,提取与之相关的数据,并将其放入data.frame中,以便进行分析。
f.extractVariables<-
structure(function
(dbPath,dbName,table,variables
){
# LOAD LIBRARIES
require(RODBC)
require(xlsx)
setwd(dbPath)
db <- odbcConnectAccess2007(dbName)
#This was my idea on how to loop through the variable list but it won't run
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, 'SELECT table.variables[[i]]
FROM table;')
}
#This piece was the solution I was given which runs but all it does is parrot back
#the variable list it doesn't retrieve anything.
variables=paste(variables,collapse=",")
query<-paste(paste("SELECT",variables),
"FROM table",sep='n')
cat(query)
odbcClose(db)
}
)
#And the user provided input looks something like this. I can change the way the
#variable list comes in to make it easier.
dbPath = 'z:/sites/'
dbName = 'oysterSites.accdb'
table = 'tblDataSiteOysterSamplingPlan'
variables= 'nwLon,nwLat,neLon,neLat'
f.extractVariables(dbPath,dbName,table,variables)
您认为您在用哪种语言进行编码?这不是.NET…
删除此
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, 'SELECT table.variables[[i]]
FROM table;')
}
在循环的所有循环中,'SELECT table.variables[[i]] FROM table;'
将始终是SAME,并随着而变化(至少我猜你想做什么)
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, paste0('SELECT table.variables[[', i, ']]
FROM table;'))
}
你说以上不起作用。没有必要继续审查您的代码,修复上面的问题,然后返回