Difference between revisions of "Opasnet Base Connection for R"

From Testiwiki
Jump to: navigation, search
m (Function)
(See also)
 
(26 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
[[Category:Open assessment]]
 
[[Category:Open assessment]]
 
[[Heande:Opasnet Base Connection for R]]
 
[[Heande:Opasnet Base Connection for R]]
 +
[[Category:Code under inspection]]
 
{{tool|moderator=Teemu R}}
 
{{tool|moderator=Teemu R}}
Code for R for the purpose of interacting with the [[Opasnet Base]] is collected on this page. To use it, copy paste the code you need to the R console; this defines the functions, after which they can be called for in that R session. Or alternatively install the [[OpasnetBaseUtils]] package.  
+
{{comment|# |Should we merge this page with [[OpasnetBaseUtils]]?|--[[User:Jouni|Jouni]] 20:18, 28 December 2011 (EET)}}
 +
 
 +
Code for [[R]] for the purpose of interacting with the [[Opasnet Base]] is collected on this page. To use it, copy paste the code you need to the R console; this defines the functions, after which they can be called for in that R session. Or alternatively install the [[OpasnetBaseUtils]] package.  
  
 
==Package dependencies==
 
==Package dependencies==
Line 12: Line 15:
 
These packages are required for most of the code to work. To install: from the top bar menu Packages>Install. To load: copy-paste.  
 
These packages are required for most of the code to work. To install: from the top bar menu Packages>Install. To load: copy-paste.  
  
library(utils)
 
 
  library(RODBC)
 
  library(RODBC)
  
 
==Downloading data==
 
==Downloading data==
  
===Functions===
+
'''op_baseGetData
  
<nowiki>
+
<rcode name="op_baseGetData">
op_baseGetData <- function(dsn, ident, include = NULL, exclude = NULL, series_id = NULL, iterations = NULL) {
+
op_baseGetData <- function(dsn, ident, include = NULL, exclude = NULL, series_id = NULL, iterations = NULL, use.utf8 = TRUE, apply.utf8 = TRUE) {
db <- odbcConnect(dsn, DBMSencoding = "UTF-8")
+
if (use.utf8) db <- odbcConnect(dsn, DBMSencoding = "UTF-8") else db <- odbcConnect(dsn)
 
obj_id <- sqlQuery(db, paste('SELECT id FROM obj WHERE ident = "', ident, '"', sep=''))[1,1]
 
obj_id <- sqlQuery(db, paste('SELECT id FROM obj WHERE ident = "', ident, '"', sep=''))[1,1]
 
if (length(series_id) == 0) {series_id <- sqlQuery(db, paste('SELECT series_id FROM actobj WHERE obj_id = ', obj_id,  
 
if (length(series_id) == 0) {series_id <- sqlQuery(db, paste('SELECT series_id FROM actobj WHERE obj_id = ', obj_id,  
Line 41: Line 43:
 
}
 
}
 
if (sliced == FALSE) {
 
if (sliced == FALSE) {
Data <- sqlQuery(db, paste('SELECT cell.id, res.obs, obj.ident AS ind, loc.location AS loc, res.result',  
+
Data <- sqlQuery(db, paste('SELECT cell.id, res.obs, obj.ident AS ind, loc.location AS loc, res.result,',  
' FROM actobj LEFT JOIN cell ON actobj.id = cell.actobj_id LEFT JOIN res ON cell.id =',  
+
' res.restext FROM actobj LEFT JOIN cell ON actobj.id = cell.actobj_id LEFT JOIN res ON cell.id =',  
 
' res.cell_id LEFT JOIN loccell ON cell.id = loccell.cell_id LEFT JOIN loc ON loccell.loc_id',
 
' res.cell_id LEFT JOIN loccell ON cell.id = loccell.cell_id LEFT JOIN loc ON loccell.loc_id',
 
' = loc.id LEFT JOIN obj ON loc.obj_id_i = obj.id WHERE actobj.obj_id = ', obj_id,  
 
' = loc.id LEFT JOIN obj ON loc.obj_id_i = obj.id WHERE actobj.obj_id = ', obj_id,  
 
' AND actobj.series_id = ', series_id, if(length(iterations)==1){paste(" AND obs <= ", iterations,  
 
' AND actobj.series_id = ', series_id, if(length(iterations)==1){paste(" AND obs <= ", iterations,  
 
sep = "")}, sep = '')) } else {
 
sep = "")}, sep = '')) } else {
Data <- sqlQuery(db, paste('SELECT cell.id, res.obs, obj.ident AS ind, loc.location AS loc, res.result',  
+
Data <- sqlQuery(db, paste('SELECT cell.id, res.obs, obj.ident AS ind, loc.location AS loc, res.result,',  
' FROM actobj LEFT JOIN cell ON actobj.id = cell.actobj_id LEFT JOIN res ON cell.id =',  
+
' res.restext FROM actobj LEFT JOIN cell ON actobj.id = cell.actobj_id LEFT JOIN res ON cell.id =',  
 
' res.cell_id LEFT JOIN loccell ON cell.id = loccell.cell_id LEFT JOIN loc ON loccell.loc_id',
 
' res.cell_id LEFT JOIN loccell ON cell.id = loccell.cell_id LEFT JOIN loc ON loccell.loc_id',
 
' = loc.id LEFT JOIN obj ON loc.obj_id_i = obj.id WHERE actobj.obj_id = ', obj_id,  
 
' = loc.id LEFT JOIN obj ON loc.obj_id_i = obj.id WHERE actobj.obj_id = ', obj_id,  
Line 60: Line 62:
 
nres <- nrow(Data)/nind
 
nres <- nrow(Data)/nind
 
dataframe <- Data[1:nres*nind, c(1,2)]
 
dataframe <- Data[1:nres*nind, c(1,2)]
for (i in nind:1) {
+
for (i in 1:nind) {
dataframe[,2 + nind - i + 1] <- factor(Data[1:nres*nind - i + 1, 4])
+
dataframe[,2 + i] <- factor(Data[1:nres*nind - (nind - i), 4])
colnames(dataframe)[2 + nind - i + 1] <- as.character(Data[nind - i + 1, 3])
+
levels(dataframe[,2 + i]) <- gsub(" *$", "",gsub("^ *", "", levels(dataframe[,2 + i])))
 +
colnames(dataframe)[2 + i] <- as.character(Data[i, 3])
 +
if(apply.utf8) Encoding(levels(dataframe[,2 + i])) <- "UTF-8"
 
}
 
}
dataframe[,3 + nind] <- Data[1:nres*nind, 5]
+
dataframe[,1:2 + 2 + nind] <- Data[1:nres*nind, 5:6]
colnames(dataframe)[3 + nind] <- "Result"
+
colnames(dataframe)[1:2 + 2 + nind] <- c("Result", "Result.Text")
 +
if(apply.utf8) {if(is.factor(dataframe[,"Result.Text"])) {Encoding(levels(dataframe[,"Result.Text"])) <- "UTF-8"} else if(is.character(dataframe[,"Result.Text"])) {
 +
Encoding(dataframe[,"Result.Text"]) <- "UTF-8"}}
 
rownames(dataframe) <- 1:nres
 
rownames(dataframe) <- 1:nres
 
return(dataframe)
 
return(dataframe)
}</nowiki>
+
}
 +
</rcode>
  
====Usage====
+
===Usage===
  
 
  variable <- op_baseGetData("opasnet_base", "page identifier", include = vector_of_loc_ids, exclude = vector_of_loc_ids)
 
  variable <- op_baseGetData("opasnet_base", "page identifier", include = vector_of_loc_ids, exclude = vector_of_loc_ids)
Line 86: Line 93:
 
===Finding index data===
 
===Finding index data===
  
====Function====
+
'''op_baseGetLocs
  
<nowiki>
+
<rcode name="op_baseGetLocs">
op_baseGetLocs <- function(dsn, ident, series_id = NULL) {
+
op_baseGetLocs <- function(dsn, ident, series_id = NULL, use.utf8 = TRUE, apply.utf8 = TRUE) {
db <- odbcConnect(dsn, DBMSencoding = "UTF-8")
+
if (use.utf8) db <- odbcConnect(dsn, DBMSencoding = "UTF-8") else db <- odbcConnect(dsn)
 
obj_id <- sqlQuery(db, paste('SELECT id FROM obj WHERE ident = "', ident, '"', sep=''))[1,1]
 
obj_id <- sqlQuery(db, paste('SELECT id FROM obj WHERE ident = "', ident, '"', sep=''))[1,1]
 
if (length(series_id) == 0) {series_id <- sqlQuery(db, paste('SELECT series_id FROM actobj WHERE obj_id = ', obj_id,  
 
if (length(series_id) == 0) {series_id <- sqlQuery(db, paste('SELECT series_id FROM actobj WHERE obj_id = ', obj_id,  
Line 99: Line 106:
 
series_id, sep = ""))
 
series_id, sep = ""))
 
odbcClose(db)
 
odbcClose(db)
Locs <- Locs[order(Locs[,1]),]
+
Locs <- Locs[order(Locs$ind, Locs$loc_id),]
 
rownames(Locs) <- 1:nrow(Locs)
 
rownames(Locs) <- 1:nrow(Locs)
 +
if(apply.utf8) {Encoding(levels(Locs$ind)) <- "UTF-8"; Encoding(levels(Locs$loc)) <- "UTF-8"}
 
return(Locs)
 
return(Locs)
}</nowiki>
+
}
 +
</rcode>
  
 
*Returns all indexes and locations and their ids in a table of format: ind, loc, loc_id.
 
*Returns all indexes and locations and their ids in a table of format: ind, loc, loc_id.
Line 108: Line 117:
 
==Manipulating data==
 
==Manipulating data==
  
===Functions===
+
'''DataframeToArray
  
<nowiki>
+
<rcode name="DataframeToArray">
 
DataframeToArray <- function(dataframe, rescol = NULL) {
 
DataframeToArray <- function(dataframe, rescol = NULL) {
 
ColNames <- colnames(dataframe[(colnames(dataframe) == "id") == FALSE])
 
ColNames <- colnames(dataframe[(colnames(dataframe) == "id") == FALSE])
Line 141: Line 150:
 
array[as.matrix(dataframe[,ColNames])] <- dataframe[,rescol]
 
array[as.matrix(dataframe[,ColNames])] <- dataframe[,rescol]
 
return(array)
 
return(array)
}</nowiki>
+
}
 +
</rcode>
  
====Usage====
+
===Usage===
  
 
  variable2 <- DataframeToArray(variable1, rescol = NULL)
 
  variable2 <- DataframeToArray(variable1, rescol = NULL)
Line 151: Line 161:
 
**"obs" column will also be ignored if there's only one.
 
**"obs" column will also be ignored if there's only one.
 
*The column containing the values may be defined in the parameters, otherwise it is assumed to be either "Freq", "Result" or "result" in that order.
 
*The column containing the values may be defined in the parameters, otherwise it is assumed to be either "Freq", "Result" or "result" in that order.
 
===Other useful stuff===
 
 
as.data.frame(as.table(array))
 
dataframe[is.na(dataframe[,ncol(dataframe)])==FALSE,]
 
 
*First line returns an array from a data frame.
 
*Second line returns rows without empty values.
 
 
dataframe[grep("location", dataframe$index, ignore.case = TRUE),]
 
 
*Returns all rows of a data frame where column "index" value is "location".
 
 
dataframe[order(dataframe[,"col1"],dataframe[,"col2"], ... ,dataframe[,"coln"]),]
 
 
*Returns dataframe ordered by col1, col2, ... , coln.
 
 
read.csv("table.csv", sep = ",")
 
 
*Returns a data.frame from a .csv file, sep is the separator used in the file.
 
  
 
==Uploading data==
 
==Uploading data==
  
*Varying numbers of iterations between cells not supported.
+
'''op_baseWrite
  
===Functions===
+
<rcode name="op_baseWrite">
 
+
op_baseWrite <- function(dsn, input, ident = NULL, name = NULL, unit = NULL, objtype_id = NULL, who = NULL, acttype = NULL,  
<nowiki>
+
rescol = NULL, n.obs.const = FALSE, maxrows = 50000, use.utf8 = TRUE, use.utf8.read = TRUE, latin1.2.utf8.conv.write = TRUE, utf8.2.latin1.conv.read = TRUE) {
op_baseWrite <- function(dsn, input, ident = NULL, name = NULL, unit = NULL, objtype_id = NULL, who = NULL, acttype = NULL, sort = TRUE,  
 
rescol = NULL, n.obs.const = FALSE, maxrows = 50000) {
 
 
 
 
# Coerce input into a data frame if it isn't one already; get rid of empty cells
 
# Coerce input into a data frame if it isn't one already; get rid of empty cells
Line 194: Line 182:
 
}}
 
}}
 
dataframe <- dataframe[is.na(dataframe[,rescol]) == FALSE,]
 
dataframe <- dataframe[is.na(dataframe[,rescol]) == FALSE,]
dataframe <- data.frame(dataframe)
+
ColNames <- colnames(dataframe)[!(colnames(dataframe)%in%c(rescol, "id", "obs"))]
 +
for (i in ColNames) {
 +
dataframe[,i] <- factor(dataframe[,i])
 +
levels(dataframe[,i]) <- gsub(" *$", "",gsub("^ *", "", levels(dataframe[,i])))
 +
if(latin1.2.utf8.conv.write) if(sum(Encoding(levels(dataframe[,i]))=="latin1")!=0) levels(dataframe[,i]) <- iconv(levels(dataframe[,i]), "latin1", "UTF-8")
 +
}
 +
 +
#if(!is.numeric(dataframe[,rescol]))  
 
 
 
# Open database connection
 
# Open database connection
db <- odbcConnect(dsn, DBMSencoding = "UTF-8")
+
if(use.utf8) db <- odbcConnect(dsn, DBMSencoding = "UTF-8") else db <- odbcConnect(dsn)
 +
if(!use.utf8.read) db2 <- odbcConnect(dsn)
 
 
 
# Add page to database (if it doesn't already exist)
 
# Add page to database (if it doesn't already exist)
Line 249: Line 245:
 
 
 
#Write indexes
 
#Write indexes
ColNames <- colnames(dataframe)[!(colnames(dataframe)%in%c(rescol, "id", "obs"))]
 
 
for (i in ColNames) {
 
for (i in ColNames) {
 
sqlQuery(db, paste('INSERT IGNORE INTO obj (ident, name, objtype_id) VALUES ("', gsub(' ', '_', i), '","',  
 
sqlQuery(db, paste('INSERT IGNORE INTO obj (ident, name, objtype_id) VALUES ("', gsub(' ', '_', i), '","',  
 
i, '", 6)', sep = ''))
 
i, '", 6)', sep = ''))
 
}
 
}
IndIds <- sqlQuery(db, paste('SELECT id, ident FROM obj WHERE ident IN("', paste(gsub(" ", "_", ColNames),  
+
IndIds <- sqlQuery((if(use.utf8.read) db else db2), paste('SELECT id, ident FROM obj WHERE ident IN("', paste(gsub(" ", "_", ColNames),  
 
collapse = '","'), '")', sep = ''))
 
collapse = '","'), '")', sep = ''))
 +
if(utf8.2.latin1.conv.read) levels(IndIds$ident) <- iconv(levels(IndIds$ident), "UTF-8", "latin1")
 
IndIdMap <- IndIds$id
 
IndIdMap <- IndIds$id
 
names(IndIdMap) <- tolower(IndIds$ident)
 
names(IndIdMap) <- tolower(IndIds$ident)
Line 268: Line 264:
 
}
 
}
 
}
 
}
LocIds <- sqlQuery(db, paste('SELECT id, obj_id_i, location FROM loc WHERE obj_id_i IN("', paste(ColIds, collapse = '","'),  
+
LocIds <- sqlQuery((if(use.utf8.read) db else db2), paste('SELECT id, obj_id_i, location FROM loc WHERE obj_id_i IN("', paste(ColIds, collapse = '","'),  
 
'")', sep = ''))
 
'")', sep = ''))
 
+
if(utf8.2.latin1.conv.read) levels(LocIds$location) <- iconv(levels(LocIds$location), "UTF-8", "latin1")
 +
 
for (i in ColIds) {
 
for (i in ColIds) {
 
LocIdMap <- LocIds[LocIds$obj_id_i == i, 1]
 
LocIdMap <- LocIds[LocIds$obj_id_i == i, 1]
names(LocIdMap) <- tolower(LocIds[LocIds$obj_id_i == i, 3])
+
names(LocIdMap) <- gsub(" *$", "",gsub("^ *", "", tolower(LocIds[LocIds$obj_id_i == i, 3])))
 
levels(dataframe[, i]) <- LocIdMap[tolower(levels(dataframe[, i]))]
 
levels(dataframe[, i]) <- LocIdMap[tolower(levels(dataframe[, i]))]
 +
if (sum(is.na(levels(dataframe[, i]))) != 0) stop("Faulty location matching. Usually caused by special characters.")
 
 
 
#Writing actloc
 
#Writing actloc
Line 282: Line 280:
 
 
 
#Writing cell
 
#Writing cell
if (is.numeric(dataframe[,rescol])) {
+
n <- tapply(dataframe[,rescol], dataframe[,ColIds], length)
if (sum(colnames(dataframe) == "obs") == 1) {
+
ncell <- sum(!is.na(n))
n <- length(levels(factor(dataframe$obs)))
 
if (n > 1) {
 
if (n.obs.const) {ncell <- nrow(dataframe)/n} else {
 
n <- tapply(dataframe[,rescol], dataframe[,ColIds], length)
 
ncell <- sum(n, na.rm = TRUE)
 
}
 
} else {ncell <- nrow(dataframe)}
 
} else {n <- 1; ncell <- nrow(dataframe)}
 
} else n <- tapply(dataframe[,rescol], dataframe[,ColIds], length) # for textual data, this is used for dimension finding
 
 
if (is.numeric(dataframe[,rescol])) means <- tapply(dataframe[,rescol], dataframe[,ColIds], mean) else means <- rep(0, ncell)
 
if (is.numeric(dataframe[,rescol])) means <- tapply(dataframe[,rescol], dataframe[,ColIds], mean) else means <- rep(0, ncell)
 
if (is.numeric(dataframe[,rescol])) {
 
if (is.numeric(dataframe[,rescol])) {
Line 300: Line 289:
 
i <- 1
 
i <- 1
 
while (length(cellQuery) >= (i + maxrows - 1)) {
 
while (length(cellQuery) >= (i + maxrows - 1)) {
sqlQuery(db, paste('INSERT INTO cell (actobj_id, mean, sd, n) VALUES (', paste(cellQuery[i:(i + maxrows)],  
+
sqlQuery(db, paste('INSERT INTO cell (actobj_id, mean, sd, n) VALUES (', paste(cellQuery[i:(i + maxrows - 1)],  
 
collapse = '),('), ')', sep = ''))
 
collapse = '),('), ')', sep = ''))
 
i <- i + maxrows
 
i <- i + maxrows
Line 314: Line 303:
 
if (is.numeric(dataframe[,rescol])) ids <- means else ids <- n
 
if (is.numeric(dataframe[,rescol])) ids <- means else ids <- n
 
ids[!is.na(ids)] <- cell_id
 
ids[!is.na(ids)] <- cell_id
dataframe[, ncol(dataframe) + 1] <- ids[as.matrix(dataframe[,names(dimnames(ids))])]
+
dataframe[, ncol(dataframe) + 1] <- ids[as.matrix(dataframe[,ColIds])]
 
colnames(dataframe)[ncol(dataframe)] <- "cell_id"
 
colnames(dataframe)[ncol(dataframe)] <- "cell_id"
 
 
Line 322: Line 311:
 
while (length(resQuery) >= (i + maxrows - 1)) {
 
while (length(resQuery) >= (i + maxrows - 1)) {
 
sqlQuery(db, paste('INSERT INTO res (cell_id, obs, ', ifelse(is.numeric(dataframe[,rescol]), "result", "restext"), ') VALUES (',  
 
sqlQuery(db, paste('INSERT INTO res (cell_id, obs, ', ifelse(is.numeric(dataframe[,rescol]), "result", "restext"), ') VALUES (',  
paste(resQuery[i:(i + maxrows)], collapse = '),('), ')', sep = ''))
+
paste(resQuery[i:(i + maxrows - 1)], collapse = '),('), ')', sep = ''))
 
i <- i + maxrows
 
i <- i + maxrows
 
}
 
}
Line 336: Line 325:
 
i <- 1
 
i <- 1
 
while (length(loccellQuery) >= (i + maxrows - 1)) {
 
while (length(loccellQuery) >= (i + maxrows - 1)) {
sqlQuery(db, paste('INSERT INTO loccell (cell_id, loc_id) VALUES (', paste(loccellQuery[i:(i + maxrows)], collapse = '),('), ')',  
+
sqlQuery(db, paste('INSERT INTO loccell (cell_id, loc_id) VALUES (', paste(loccellQuery[i:(i + maxrows - 1)], collapse = '),('), ')',  
 
sep = ''))
 
sep = ''))
 
i <- i + maxrows
 
i <- i + maxrows
Line 349: Line 338:
 
cat("Successful\n")
 
cat("Successful\n")
 
return(character())
 
return(character())
}</nowiki>
+
}
 +
</rcode>
  
====Usage====
+
===Usage===
  
  op_baseWrite(dsn, input, ident = NULL, name = NULL, unit = NULL, objtype_id = NULL, who = NULL, acttype = NULL, sort = TRUE)
+
  op_baseWrite(dsn, input, ident = NULL, name = NULL, unit = NULL, objtype_id = NULL, who = NULL, acttype = NULL, rescol = NULL)
  
 
*dsn and input must be defined, the rest of the object and act parameters if not defined are prompted for by the function as needed.
 
*dsn and input must be defined, the rest of the object and act parameters if not defined are prompted for by the function as needed.
 
**For uploading the DSN defined must have writers permissions.
 
**For uploading the DSN defined must have writers permissions.
*sort (default = TRUE), sorts the data in the required order, if necessary (probabilistic data)
+
*rescol defines the column from which the values are chosen from, both numerical and textual data are allowed, if left undefined the function will check column matches for "Freq", "Result" and "result" in that order.
  
=====Restrictions=====
+
====Restrictions====
  
 
*Input may only be given in either array or data.frame form.
 
*Input may only be given in either array or data.frame form.
 
**Indexes used may not exceed the character limit of 20.
 
**Indexes used may not exceed the character limit of 20.
 
***Indexes should preferably match an earlier entry: [[Special:OpasnetBaseIndices]].
 
***Indexes should preferably match an earlier entry: [[Special:OpasnetBaseIndices]].
***Indexes are treated as identifiers for indexes in the database, spaces in the indexes are converted to _. This ensures maximum compatibility and ease in operations in which data is downloaded and uplaoded again. Names and more specific details can be edited into the indexes separately.  
+
***Indexes are treated as identifiers for indexes in the database, spaces in the indexes are converted to _. This ensures maximum compatibility and ease in operations in which data is downloaded and uploaded again. Names and more specific details can be edited into the indexes separately.
**Input in data.frame form must contain a "Freq", "Result" or "result" column, this is where the numerical value is read.  
+
 
**Probabilistic input must contain a dimension (in case of array) or a column (in case of data.frame) named "obs". By default the function sorts the data as it requires, however, for some performance boost this may be disabled if the sorting order is taken into account elsewhere: indices first, obs last; for arrays this means that the first index must be "obs". Data downloaded by the op_baseGetData function is sorted correctly by default.
+
== Regarding special characters and character encoding ==
 +
 
 +
Using special characters like ä and ö when our database is encoded in latin1 while wiki is in UTF-8 is a bit complicated. New parameters -- for forcing the odbc connection to use UTF-8 and in-R conversion of one encoding to the other prior to writing or after reading -- have been created. The defaults have been made to work with opasnet_base. For use with heande_base, forcing UTF-8 on the odbc connection when reading should be disabled, meaning that ''use.utf8'' should be set to ''FALSE'' when using ''op_baseGetData'' and ''use.utf8.read'' should be set to ''FALSE'' when using ''op_baseWrite''.
 +
 
 +
== See also ==
 +
 
 +
{{Opasnet Base}}
 +
 
 +
*[[A Tutorial on R]]

Latest revision as of 20:29, 10 April 2015


--# : Should we merge this page with OpasnetBaseUtils? --Jouni 20:18, 28 December 2011 (EET)

Code for R for the purpose of interacting with the Opasnet Base is collected on this page. To use it, copy paste the code you need to the R console; this defines the functions, after which they can be called for in that R session. Or alternatively install the OpasnetBaseUtils package.

Package dependencies

These packages are required for most of the code to work. To install: from the top bar menu Packages>Install. To load: copy-paste.

library(RODBC)

Downloading data

op_baseGetData

+ Show code

Usage

variable <- op_baseGetData("opasnet_base", "page identifier", include = vector_of_loc_ids, exclude = vector_of_loc_ids)
  • Assuming "opasnet_base" is a correctly defined DSN (Data Service Name; in Windows XP: Control Panel\Administrative tools\Data Sources (ODBC)).
  • Include and exclude are optional.
    • Include picks all cells in the locations given.
      • The clearest case is when all the included locations belong to the same index: Any cells in the non-included locations of the index will be left out.
      • In case given locations are in multiple indices: The effect produced will be the same as picking separately for each index and removing duplicates.
    • Exclude unpicks any cells which are indexed by the locations given. Slower than include.
    • They can be used in unison.
  • Result will be in a table format with columns: id, obs, ind1, ind2 ... indn, Result.
  • series_id is an optional parameter, if it is not given the most current upload of the data will be downloaded.

Finding index data

op_baseGetLocs

+ Show code

  • Returns all indexes and locations and their ids in a table of format: ind, loc, loc_id.

Manipulating data

DataframeToArray

+ Show code

Usage

variable2 <- DataframeToArray(variable1, rescol = NULL)
  • variable1 must be in similar format as the result when downloading.
  • Columns named "id" and various versions of "Result" are ignored for dimension creation.
    • "obs" column will also be ignored if there's only one.
  • The column containing the values may be defined in the parameters, otherwise it is assumed to be either "Freq", "Result" or "result" in that order.

Uploading data

op_baseWrite

+ Show code

Usage

op_baseWrite(dsn, input, ident = NULL, name = NULL, unit = NULL, objtype_id = NULL, who = NULL, acttype = NULL, rescol = NULL)
  • dsn and input must be defined, the rest of the object and act parameters if not defined are prompted for by the function as needed.
    • For uploading the DSN defined must have writers permissions.
  • rescol defines the column from which the values are chosen from, both numerical and textual data are allowed, if left undefined the function will check column matches for "Freq", "Result" and "result" in that order.

Restrictions

  • Input may only be given in either array or data.frame form.
    • Indexes used may not exceed the character limit of 20.
      • Indexes should preferably match an earlier entry: Special:OpasnetBaseIndices.
      • Indexes are treated as identifiers for indexes in the database, spaces in the indexes are converted to _. This ensures maximum compatibility and ease in operations in which data is downloaded and uploaded again. Names and more specific details can be edited into the indexes separately.

Regarding special characters and character encoding

Using special characters like ä and ö when our database is encoded in latin1 while wiki is in UTF-8 is a bit complicated. New parameters -- for forcing the odbc connection to use UTF-8 and in-R conversion of one encoding to the other prior to writing or after reading -- have been created. The defaults have been made to work with opasnet_base. For use with heande_base, forcing UTF-8 on the odbc connection when reading should be disabled, meaning that use.utf8 should be set to FALSE when using op_baseGetData and use.utf8.read should be set to FALSE when using op_baseWrite.

See also

Pages related to Opasnet Base

Opasnet Base · Uploading to Opasnet Base · Data structures in Opasnet · Opasnet Base UI · Modelling in Opasnet · Special:Opasnet Base Import · Opasnet Base Connection for R (needs updating) · Converting KOPRA data into Opasnet Base · Poll

Pages related to the 2008-2011 version of Opasnet Base

Opasnet base connection for Analytica · Opasnet base structure · Related Analytica file (old version File:Transferring to result database.ANA) · Analytica Web Player · Removed pages and other links · Standard run · OpasnetBaseUtils