Opasnet Base Connection for R: Difference between revisions

From Opasnet
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 22: Line 22:
==Downloading data==
==Downloading data==


*At the moment sensitive to glitches in the database and does not recognize probabilistic data yet, otherwise should work.  
*Tested and mostly works, in case of error with GetArray try GetDataFrame, it should work with data missing indexes and locations.  


===Low level functions (mainly sql queries)===
===Low level functions (mainly sql queries)===


Copy paste these second.
Copy paste these second.
 
  #Get latest series id
  #Get latest series id
  op_baseGetLatest <- function(ident) {
  op_baseGetLatest <- function(ident) {
Line 39: Line 39:
  }
  }
   
   
  #Get results (cell id, result)
  #Get results (cell id, result, obs)
  op_baseGetData <- function(series_id) {
  op_baseGetData <- function(page_ident, series_id) {
  sqlQuery(db, paste('
  sqlQuery(db, paste('
  SELECT cell.id, res.result  
  SELECT cell.id, res.result, res.obs
  FROM actobj  
  FROM obj
LEFT JOIN actobj
ON obj.id = actobj.obj_id
  LEFT JOIN cell  
  LEFT JOIN cell  
  ON actobj.id = cell.actobj_id  
  ON actobj.id = cell.actobj_id  
  LEFT JOIN res  
  LEFT JOIN res  
  ON cell.id = res.cell_id  
  ON cell.id = res.cell_id  
  <nowiki>WHERE actobj.series_id = ', series_id, sep = ''))</nowiki>
  WHERE actobj.series_id = ', series_id,
<nowiki>' AND obj.ident = "', page_ident, '"', sep = ''))</nowiki>
  }
  }
   
   
Line 63: Line 66:
  }
  }
   
   
  #Experimental: Sort locations by index
  #Experimental: match locations and indexes
  #tapply(dims_table[,3], dims_table[,5], paste)
  #tapply(dims_table[,3], dims_table[,5], paste)
   
   
  #Not in use: Get indexes from dims_table
  #Not in use: Get indexes from dims_table
  op_baseFindInd <- function(dims_table) {
  op_baseFindInd <- function(dims_table) {
  factor(op_baseDims[,4]) #could also use "obj_id_i"(obj.name for index) instead of 5 for column name
  factor(op_baseDims[,4])  
  }
  }
   
   
Line 74: Line 77:
  op_baseGetInd <- function (cell_id_list) {
  op_baseGetInd <- function (cell_id_list) {
  indexes <- sqlQuery(db, paste('
  indexes <- sqlQuery(db, paste('
  SELECT DISTINCT loc.obj_id_i  
  SELECT DISTINCT loc.obj_id_i
  FROM loccell  
  FROM cell
LEFT JOIN loccell  
ON cell.id = loccell.cell_id
  LEFT JOIN loc  
  LEFT JOIN loc  
  ON loccell.loc_id = loc.id  
  ON loccell.loc_id = loc.id  
  WHERE loccell.cell_id
  <nowiki>WHERE cell.n <> 0 AND</nowiki>
<nowiki>BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1],</nowiki>
cell.id BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1],
  <nowiki>' ORDER BY loc.obj_id_i', sep = ''))</nowiki>
  <nowiki>' ORDER BY loc.obj_id_i', sep = ''))</nowiki>
  as.numeric(indexes[,1])
  as.numeric(indexes[,1])
Line 91: Line 96:
  LEFT JOIN loc  
  LEFT JOIN loc  
  ON loccell.loc_id = loc.id  
  ON loccell.loc_id = loc.id  
  <nowiki>WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1], sep = ''))</nowiki>
  WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ',  
<nowiki>cell_id_list[nrow(cell_id_list),1], sep = ''))</nowiki>
  as.numeric(locations[,1])
  as.numeric(locations[,1])
  }
  }
   
   
  #Not in use: Get location names (will not work properly if different indexes have locations with similar name)
  #Not in use: Get location names
  op_baseGetLocNames <- function(index, cell_id_list) {
  op_baseGetLocNames <- function(index, cell_id_list) {
  locations <- sqlQuery(db, paste('
  locations <- sqlQuery(db, paste('
  SELECT DISTINCT loc.location  
  SELECT DISTINCT loc.location, loc.obj_id_i
  FROM loccell  
  FROM loccell  
  LEFT JOIN loc  
  LEFT JOIN loc  
  ON loccell.loc_id = loc.id  
  ON loccell.loc_id = loc.id  
  <nowiki>WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1], sep = ''))</nowiki>
  WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ',  
<nowiki>cell_id_list[nrow(cell_id_list),1], sep = ''))</nowiki>
  as.numeric(locations[,1])
  as.numeric(locations[,1])
  }
  }
Line 119: Line 126:
  SELECT name  
  SELECT name  
  FROM obj  
  FROM obj  
  <nowiki>WHERE id = ', ind_id, sep = '')) }</nowiki>
  <nowiki>WHERE id = ', ind_id, sep = '')) </nowiki>
}
   
   
  #Get locations for a specific cell
  #Get locations for a specific cell
Line 128: Line 136:
  LEFT JOIN loc  
  LEFT JOIN loc  
  ON loccell.loc_id = loc.id  
  ON loccell.loc_id = loc.id  
  <nowiki>WHERE loccell.cell_id = ', cell_id,</nowiki>
  WHERE loccell.cell_id = ', cell_id,
  <nowiki>' ORDER BY loc.obj_id_i', sep = ''))</nowiki>
  <nowiki>' ORDER BY loc.obj_id_i', sep = ''))</nowiki>
}
#Get cell.n (cell id, cell.n)
op_baseGetCelln <- function(cell_id_list) {
sqlQuery(db, paste('
SELECT cell.id, cell.n
FROM cell
WHERE cell.id
<nowiki>BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1], sep = ''))</nowiki>
}
#Get results from probabilistic data (cell id, result, obs)
op_baseGetDataProb <- function(page_ident, series_id) {
sqlQuery(db, paste('
SELECT cell.id, res.result, res.obs
FROM obj
LEFT JOIN actobj
ON obj.id = actobj.obj_id
LEFT JOIN cell
ON actobj.id = cell.actobj_id
LEFT JOIN res
ON cell.id = res.cell_id
WHERE actobj.series_id = ', series_id,
<nowiki>' AND obj.ident = "', page_ident, '"', </nowiki>
<nowiki>' AND res.obs <> 0', sep = ''))</nowiki>
  }
  }


===High Level function===
===High Level function===


Copy paste this last.  
Copy paste this last.


  op_baseArray <- function(page_ident) {
  op_baseGetArray <- function(page_ident) {
 
 
  #Basic data downloading
  #Basic data downloading
 
 
  op_baseLatest <- op_baseGetLatest(page_ident)
  op_baseLatest <- op_baseGetLatest(page_ident)
  op_baseData <- op_baseGetData(op_baseLatest) #cell,result
  op_baseData <- op_baseGetData(page_ident, op_baseLatest) #cell,result, obs
  #final_array <- op_baseData[,2] #ideal case (data in proper order)
  #op_baseDims <- op_baseGetDims(op_baseData)
#Differentiate between probabilistic and non-probabilistic
  #op_baseIndexes <- op_baseFindInd(op_baseDims)
probabilistic <- FALSE #variable indicator of whether the object is probabilistic
for (i in 1:length(op_baseData)) {
if (op_baseData[i,3] > 0) probabilistic <- TRUE
}
if (probabilistic == TRUE) op_baseData <- op_baseGetDataProb(page_ident, op_baseLatest) #cell,result, obs
  #final_array <- op_baseData[,2] #ideal case (data in proper order, achievable by tweaking upload order)
  #op_baseDims <- op_baseGetDims(op_baseData) #with further understanding of R number of queries could be reduced
  #op_baseIndexes <- op_baseFindInd(op_baseDims) #improved code here
  op_baseInd <- op_baseGetInd(op_baseData)
  op_baseInd <- op_baseGetInd(op_baseData)
if (probabilistic == TRUE) op_baseCelln <- op_baseGetCelln(op_baseData)
 
 
  #Array structure building
  #Array structure building
Line 155: Line 198:
  IndNames <- c(1) #blank vector for index names
  IndNames <- c(1) #blank vector for index names
  for (i in 1:length(op_baseInd)) {
  for (i in 1:length(op_baseInd)) {
  <nowiki>IndIdList[[i]] <- op_baseGetLoc(op_baseInd[i], op_baseData) #fetch location ids in index to a list,  
  <nowiki>IndIdList[[i]] <- op_baseGetLoc(op_baseInd[i], op_baseData) #fetch location ids in index to a list, </nowiki>
#assumed every cell has locations for all unique index id values in loc.obj_id_i in the range of cells</nowiki>
  #assumed every cell has locations for all unique index id values in loc.obj_id_i in the range of cells
  <nowiki>IndNames[i] <- paste(op_baseGetIndName(op_baseInd[i])[1,1]) #fetch index name to a vector</nowiki>
  <nowiki>IndNames[i] <- paste(op_baseGetIndName(op_baseInd[i])[1,1]) #fetch index name to a vector</nowiki>
  <nowiki>IndLengths[i] <- length(IndIdList[[i]]) #backup: op_baseGetLoc(op_baseInd[i], op_baseData))</nowiki>
  <nowiki>IndLengths[i] <- length(IndIdList[[i]]) #backup: op_baseGetLoc(op_baseInd[i], op_baseData))</nowiki>
  <nowiki>for (j in 1:IndLengths[i]) { </nowiki>
  for (j in 1:IndLengths[i]) {  
  <nowiki>IndList[[i]][j] <- paste(op_baseGetLocName(IndIdList[[i]][j])[1,1])</nowiki>
  <nowiki>IndList[[i]][j] <- paste(op_baseGetLocName(IndIdList[[i]][j])[1,1])</nowiki>
  }
  }
  <nowiki>#IndList[[i]] <- temp[[i]]</nowiki>
}
if (probabilistic == TRUE) { #Probabilistic exceptions (add index: Iteration)
Iterations <- max(op_baseCelln[,2])
IndLengths[length(IndLengths) + 1] <- Iterations
  <nowiki>IndList[[length(IndList) + 1]] <- 1:Iterations</nowiki>
IndNames[length(IndNames) + 1] <- "Iteration"
  }
  }
  final_array <- rep(NA, prod(IndLengths))
  final_array <- rep(NA, prod(IndLengths))
  dim(final_array) <- IndLengths
  dim(final_array) <- IndLengths
  names(IndList) <- IndNames
  names(IndList) <- IndNames
names(IndLengths) <- op_baseInd
  dimnames(final_array) <- IndList
  dimnames(final_array) <- IndList
 
 
  #Cell population
  #Cell population
 
 
IndexNumber <- 1:length(op_baseInd)
names(IndexNumber) <- op_baseInd
  LocList <- c(1) #blank vector for locations per cell
  LocList <- c(1) #blank vector for locations per cell
  LocMap <- c(1) #blank vector for location to position mapping
  LocMap <- c(1) #blank vector for location to position mapping
  CellPosition <- c(1:length(IndLengths)) #blank vector for cell position
LocMapNames <- c(1)
  CellPosition <- rep(1, length(IndLengths)) #blank vector for cell position
  dim(CellPosition) <- c(1, length(IndLengths)) #form into matrix of appropriate form
  dim(CellPosition) <- c(1, length(IndLengths)) #form into matrix of appropriate form
  for (i in 1:length(op_baseData[,1])) { #for each cell
  for (i in 1:length(op_baseData[,2])) { #for each result
  <nowiki>LocList <- paste(op_baseGetLocs(op_baseData[i,1])[,1]) #get list of locations</nowiki>
  LocList <- op_baseGetLocs(op_baseData[i,1]) #get list of locations
  <nowiki>for (j in 1:length(LocList)) { #for each location per cell</nowiki>
  for (j in 1:length(LocList[,1])) { #for each location per cell
  <nowiki>LocMap <- 1:IndLengths[j] #get available positions in given index, assumed correct order of indexes in LocList</nowiki>
  LocMap <- 1:IndLengths[paste(LocList[j,3])] #get available positions in given index  
  <nowiki>names(LocMap) <- dimnames(final_array)[[j]] #add names (locations) of positions in given index</nowiki>
  names(LocMap) <- dimnames(final_array)[[paste(names(IndList)[IndexNumber[paste(LocList[j,3])]])]]
  <nowiki>CellPosition[j] <- LocMap[LocList[j]] #match cell specific locations to positions in index</nowiki>
#add names (locations) of positions in given index
  CellPosition[1,IndexNumber[paste(LocList[j,3])]] <- LocMap[paste(LocList[j,1])]  
#match cell specific locations to positions in index
  }
  }
  <nowiki>final_array[CellPosition] <- op_baseData[i,2] #insert result in array in position determined by CellPosition</nowiki>
  if (probabilistic == TRUE) CellPosition[1,length(CellPosition)] <- op_baseData[i,3]
final_array[CellPosition] <- op_baseData[i,2] #insert result in array in position determined by CellPosition
  }
  }
  final_array #print/return the final array
  final_array #print/return the final array
}
ArraytoDataFrame <- function(array) {
as.data.frame(as.table(array))
}
op_baseGetDataFrame <- function(page_ident) {
#Basic data downloading
op_baseLatest <- op_baseGetLatest(page_ident)
op_baseData <- op_baseGetData(page_ident, op_baseLatest) #cell,result, obs
#Differentiate between probabilistic and non-probabilistic
probabilistic <- FALSE #variable indicator of whether the object is probabilistic
for (i in 1:length(op_baseData)) {
if (op_baseData[i,3] > 0) probabilistic <- TRUE
}
if (probabilistic == TRUE) op_baseData <- op_baseGetDataProb(page_ident, op_baseLatest) #cell,result, obs
op_baseInd <- op_baseGetInd(op_baseData)
#Table building
for (i in 1:length(op_baseData[,2])) { #for each result
LocList <- paste(op_baseGetLocs(op_baseData[i,1])[,1]) #get list of locations
if (length(LocList) != 0) {
for (j in 1:length(LocList)) { #for each location per cell
op_baseData[i,(3+j)] <- LocList[j] #put in place in table
}
}
}
final_table <- data.frame(1:length(op_baseData[,1]))
#final_table[,1] <- op_baseData[,1]
x <- 0
if (probabilistic == TRUE) {
x <- 1
<nowiki>dimnames(final_table)[[2]][1] <- "Iteration"</nowiki>
final_table[,1] <- op_baseData[,3]
}
if (is.na(op_baseInd[1]) == TRUE) y <- 0 else y <- length(op_baseInd)
if (y > 0) {
for (i in 1:y) {
final_table[,x + i] <- op_baseData[,3+i]
}
}
final_table[,(x + y + 1)] <- op_baseData[,2]
if (y > 0) {
for (i in 1:y) {
<nowiki>dimnames(final_table)[[2]][x+i] <- paste(op_baseGetIndName(op_baseInd[i])[1,1])</nowiki>
}
}
<nowiki>dimnames(final_table)[[2]][(x + y + 1)] <- "Result"</nowiki>
final_table
  }
  }


Line 190: Line 300:


  variable <- op_baseArray("<page identifier>")
  variable <- op_baseArray("<page identifier>")
variable <- ArraytoDataFrame(<array name>)
variable <- op_baseGetDataFrame("<page identifier>")

Revision as of 06:49, 27 August 2010


Code for R for the purpose of interacting with the Opasnet Base is collected on this page. To use it, copy paste the code to R; this defines the functions, after which they can be called in R. I've tried to explain the code in comments (#). This is a work in progress, if you have comments or new ideas post them using the comment templates on a new line after the one you want to comment, I'm no expert in programming so input is appreciated :P.

Package dependencies

These packages are required for most of the code to work. Load these first.

  1. utils
  2. RODBC

Setup

Establishes the connection to the database. Copy paste first.

db <- odbcConnect("opasnet_base") #assuming "opasnet_base" is the defined DSN

Downloading data

  • Tested and mostly works, in case of error with GetArray try GetDataFrame, it should work with data missing indexes and locations.

Low level functions (mainly sql queries)

Copy paste these second.

#Get latest series id
op_baseGetLatest <- function(ident) {
	series_list <- sqlQuery(db, paste('
		SELECT actobj.series_id 
		FROM obj 
		LEFT JOIN actobj 
		ON obj.id = actobj.obj_id 
		WHERE obj.ident = "', ident, '"', sep = ''))
	series_list[nrow(series_list),1]
}

#Get results (cell id, result, obs)
op_baseGetData <- function(page_ident, series_id) {
	sqlQuery(db, paste('
		SELECT cell.id, res.result, res.obs 
		FROM obj 
		LEFT JOIN actobj 
		ON obj.id = actobj.obj_id
		LEFT JOIN cell 
		ON actobj.id = cell.actobj_id 
		LEFT JOIN res 
		ON cell.id = res.cell_id 
		WHERE actobj.series_id = ', series_id, 
		' AND obj.ident = "', page_ident, '"', sep = ''))
}

#Not in use: Get indexes and locations per cell
op_baseGetDims <- function(cell_id_list) {
	sqlQuery(db, paste('
		SELECT loccell.cell_id, loc.id, loc.location, loc.obj_id_i, obj.name 
		FROM loccell 
		LEFT JOIN loc 
		ON loccell.loc_id = loc.id 
		LEFT JOIN obj 
		ON loc.obj_id_i = obj.id 
		WHERE loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1], sep = ''))
}

#Experimental: match locations and indexes
#tapply(dims_table[,3], dims_table[,5], paste)

#Not in use: Get indexes from dims_table
op_baseFindInd <- function(dims_table) {
	factor(op_baseDims[,4]) 
}

#Get indexes
op_baseGetInd <- function (cell_id_list) {
	indexes <- sqlQuery(db, paste('
		SELECT DISTINCT loc.obj_id_i
		FROM cell 
		LEFT JOIN loccell 
		ON cell.id = loccell.cell_id
		LEFT JOIN loc 
		ON loccell.loc_id = loc.id 
		WHERE cell.n <> 0 AND
		cell.id BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1],
		' ORDER BY loc.obj_id_i', sep = ''))
	as.numeric(indexes[,1])
}

#Get locations
op_baseGetLoc <- function(index, cell_id_list) {
	locations <- sqlQuery(db, paste('
		SELECT DISTINCT loc.id 
		FROM loccell 
		LEFT JOIN loc 
		ON loccell.loc_id = loc.id 
		WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ', 
		cell_id_list[nrow(cell_id_list),1], sep = ''))
	as.numeric(locations[,1])
}

#Not in use: Get location names
op_baseGetLocNames <- function(index, cell_id_list) {
	locations <- sqlQuery(db, paste('
		SELECT DISTINCT loc.location, loc.obj_id_i 
		FROM loccell 
		LEFT JOIN loc 
		ON loccell.loc_id = loc.id 
		WHERE loc.obj_id_i = ', index, ' AND loccell.cell_id BETWEEN ', cell_id_list[1,1], ' AND ', 
		cell_id_list[nrow(cell_id_list),1], sep = ''))
	as.numeric(locations[,1])
}

#Get location name (simple)
op_baseGetLocName <- function(loc_id) {
	sqlQuery(db, paste('
		SELECT location 
		FROM loc 
		WHERE id = ', loc_id, sep = ''))
}

#Get index name
op_baseGetIndName <- function(ind_id) {
	sqlQuery(db, paste('
		SELECT name 
		FROM obj 
		WHERE id = ', ind_id, sep = '')) 
}

#Get locations for a specific cell
op_baseGetLocs <- function(cell_id) {
	sqlQuery(db, paste('
		SELECT DISTINCT loc.location, loc.id, loc.obj_id_i 
		FROM loccell 
		LEFT JOIN loc 
		ON loccell.loc_id = loc.id 
		WHERE loccell.cell_id = ', cell_id,
		' ORDER BY loc.obj_id_i', sep = ''))
}

#Get cell.n (cell id, cell.n)
op_baseGetCelln <- function(cell_id_list) {
	sqlQuery(db, paste('
		SELECT cell.id, cell.n
		FROM cell 
		WHERE cell.id 
		BETWEEN ', cell_id_list[1,1], ' AND ', cell_id_list[nrow(cell_id_list),1], sep = ''))
}

#Get results from probabilistic data (cell id, result, obs)
op_baseGetDataProb <- function(page_ident, series_id) {
	sqlQuery(db, paste('
		SELECT cell.id, res.result, res.obs 
		FROM obj 
		LEFT JOIN actobj 
		ON obj.id = actobj.obj_id
		LEFT JOIN cell 
		ON actobj.id = cell.actobj_id 
		LEFT JOIN res 
		ON cell.id = res.cell_id 
		WHERE actobj.series_id = ', series_id, 
		' AND obj.ident = "', page_ident, '"', 
		' AND res.obs <> 0', sep = ''))
}

High Level function

Copy paste this last.

op_baseGetArray <- function(page_ident) {
	
	#Basic data downloading
	
	op_baseLatest <- op_baseGetLatest(page_ident)
	op_baseData <- op_baseGetData(page_ident, op_baseLatest) #cell,result, obs
	
	#Differentiate between probabilistic and non-probabilistic
	
	probabilistic <- FALSE #variable indicator of whether the object is probabilistic
	for (i in 1:length(op_baseData)) {
		if (op_baseData[i,3] > 0) probabilistic <- TRUE
	}
	if (probabilistic == TRUE) op_baseData <- op_baseGetDataProb(page_ident, op_baseLatest) #cell,result, obs
	
	#final_array <- op_baseData[,2] #ideal case (data in proper order, achievable by tweaking upload order)
	#op_baseDims <- op_baseGetDims(op_baseData) #with further understanding of R number of queries could be reduced
	#op_baseIndexes <- op_baseFindInd(op_baseDims) #improved code here
	op_baseInd <- op_baseGetInd(op_baseData)
	if (probabilistic == TRUE) op_baseCelln <- op_baseGetCelln(op_baseData)
	
	#Array structure building
	
	IndLengths <- 1:3 #blank vector for index lengths
	IndList <- rep(vector("list", 1), length(op_baseInd)) #blank list of appropriate length for indexes and 
	#locations for naming the dimensions of the array
	IndIdList <- rep(vector("list", 1), length(op_baseInd)) #blank list of appropriate length for ids of indexes
	IndNames <- c(1) #blank vector for index names
	for (i in 1:length(op_baseInd)) {
		IndIdList[[i]] <- op_baseGetLoc(op_baseInd[i], op_baseData) #fetch location ids in index to a list, 
 		#assumed every cell has locations for all unique index id values in loc.obj_id_i in the range of cells
		IndNames[i] <- paste(op_baseGetIndName(op_baseInd[i])[1,1]) #fetch index name to a vector
		IndLengths[i] <- length(IndIdList[[i]]) #backup: op_baseGetLoc(op_baseInd[i], op_baseData))
		for (j in 1:IndLengths[i]) { 
			IndList[[i]][j] <- paste(op_baseGetLocName(IndIdList[[i]][j])[1,1])
		}
	}
	if (probabilistic == TRUE) { #Probabilistic exceptions (add index: Iteration)
		Iterations <- max(op_baseCelln[,2])
		IndLengths[length(IndLengths) + 1] <- Iterations
		IndList[[length(IndList) + 1]] <- 1:Iterations
		IndNames[length(IndNames) + 1] <- "Iteration"
	}
	final_array <- rep(NA, prod(IndLengths))
	dim(final_array) <- IndLengths
	names(IndList) <- IndNames
	names(IndLengths) <- op_baseInd
	dimnames(final_array) <- IndList
	
	#Cell population
	
	IndexNumber <- 1:length(op_baseInd)
	names(IndexNumber) <- op_baseInd
	LocList <- c(1) #blank vector for locations per cell
	LocMap <- c(1) #blank vector for location to position mapping
	LocMapNames <- c(1)
	CellPosition <- rep(1, length(IndLengths)) #blank vector for cell position
	dim(CellPosition) <- c(1, length(IndLengths)) #form into matrix of appropriate form
	for (i in 1:length(op_baseData[,2])) { #for each result
		LocList <- op_baseGetLocs(op_baseData[i,1]) #get list of locations
		for (j in 1:length(LocList[,1])) { #for each location per cell
			LocMap <- 1:IndLengths[paste(LocList[j,3])] #get available positions in given index 
			names(LocMap) <- dimnames(final_array)[[paste(names(IndList)[IndexNumber[paste(LocList[j,3])]])]] 
			#add names (locations) of positions in given index
			CellPosition[1,IndexNumber[paste(LocList[j,3])]] <- LocMap[paste(LocList[j,1])] 
			#match cell specific locations to positions in index
		}
		if (probabilistic == TRUE) CellPosition[1,length(CellPosition)] <- op_baseData[i,3]
		final_array[CellPosition] <- op_baseData[i,2] #insert result in array in position determined by CellPosition
	}
	final_array #print/return the final array
}

ArraytoDataFrame <- function(array) {
	as.data.frame(as.table(array))
}

op_baseGetDataFrame <- function(page_ident) {
	
	#Basic data downloading
	
	op_baseLatest <- op_baseGetLatest(page_ident)
	op_baseData <- op_baseGetData(page_ident, op_baseLatest) #cell,result, obs
	
	#Differentiate between probabilistic and non-probabilistic
	
	probabilistic <- FALSE #variable indicator of whether the object is probabilistic
	for (i in 1:length(op_baseData)) {
		if (op_baseData[i,3] > 0) probabilistic <- TRUE
	}
	if (probabilistic == TRUE) op_baseData <- op_baseGetDataProb(page_ident, op_baseLatest) #cell,result, obs
	
	op_baseInd <- op_baseGetInd(op_baseData)
	
	#Table building
	
	for (i in 1:length(op_baseData[,2])) { #for each result
		LocList <- paste(op_baseGetLocs(op_baseData[i,1])[,1]) #get list of locations
		if (length(LocList) != 0) {
			for (j in 1:length(LocList)) { #for each location per cell
				op_baseData[i,(3+j)] <- LocList[j] #put in place in table
			}
		}
	}
	final_table <- data.frame(1:length(op_baseData[,1]))
	#final_table[,1] <- op_baseData[,1]
	x <- 0
	if (probabilistic == TRUE) {
		x <- 1
		dimnames(final_table)[[2]][1] <- "Iteration"
		final_table[,1] <- op_baseData[,3]
	}
	if (is.na(op_baseInd[1]) == TRUE) y <- 0 else y <- length(op_baseInd)
	if (y > 0) {
		for (i in 1:y) {
			final_table[,x + i] <- op_baseData[,3+i]
		}
	}
	final_table[,(x + y + 1)] <- op_baseData[,2]
	if (y > 0) {
		for (i in 1:y) {
			dimnames(final_table)[[2]][x+i] <- paste(op_baseGetIndName(op_baseInd[i])[1,1])
		}
	}
	dimnames(final_table)[[2]][(x + y + 1)] <- "Result"
	final_table
}

Usage

variable <- op_baseArray("<page identifier>")
variable <- ArraytoDataFrame(<array name>)
variable <- op_baseGetDataFrame("<page identifier>")