-4

昨日この質問をしたところ、回答がありましたが、彼らが私の質問を本当に理解していたとは思えません。データベースからデータを抽出する必要があります。ただし、ユーザーは変数のリストを提供します。要求された各変数をループし、それに関連付けられたデータを取得して 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)
4

1 に答える 1