Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

travis test #77

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Depends:
reshape2,
data.table,
yaml,
pander
pander,
RPostgreSQL,
sqldf
License: GPL-2.0
Suggests:
testthat
Expand Down Expand Up @@ -47,6 +49,7 @@ Collate:
'record2table.r'
'selectIndex.r'
'select_data.r'
'sql_demographic.r'
'stdid.r'
'unique.patient.r'
'unique.spell.R'
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test:

clean:
rm -rf src/*.o src/*.so
rm -rf man
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export(reindexRecord)
export(selectTable)
export(select_data)
export(setValue)
export(sql.add.demographic)
export(sql.demographic.table)
export(sql.init.postgres)
export(sql.load_file)
export(sql.newdb)
export(unique.spell)
export(uniquePatients)
export(xml2Data)
Expand Down
9 changes: 8 additions & 1 deletion R/selectIndex.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ selectIndex<- function(ids, type){
return(ccdata.env$checklist[[id]])
}

#'
#'
#' @export .which.datatype
.which.datatype <- function(id) {
# List with the convertion operations to do for each datatype
Expand All @@ -53,5 +53,12 @@ selectIndex<- function(ids, type){
}
# accounts for not listed or null (eg. when working with dt labels)
return(as.character)
}


#' Convert item data to its corresponding data type.
#' @param id NHIC code of the data
#' @param vals The vector of values of the item.
#' @return vector values in its corresponding data type
which.datatype <- function(id, vals) {
}
78 changes: 78 additions & 0 deletions R/sql_demographic.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Create demographic SQL tables. The data type of each column is in its
#' corresponding data type.
#' @export sql.demographic.table
sql.demographic.table <- function(record, dtype=TRUE) {
env <- environment()
demogls <- list()
all.demcode <- all.nhic.code("Demographic")
for_each_episode(record,
function(x){
demog.data <- rep("NULL", length(all.demcode))
names(demog.data) <- all.demcode
demog.data <- as.list(demog.data)
for(item in names(x@data)) {
if (length(x@data[[item]]) == 1) {
demog.data[[item]] <- x@data[[item]]
}
}
env$demogls[[length(env$demogls) + 1]] <- .simple.data.frame(demog.data)
})
demogt <- rbindlist(demogls)
setnames(demogt, code2stname(names(demogt)))

if (dtype) {
for (i in seq(ncol(demogt))){
demogt[[i]] <-
.which.datatype(stname2code(names(demogt)[i]))(demogt[[i]])
}
}
return(demogt)
}


#' Initialise the connection of between sqldf and postgres.
#' @export sql.init.postgres
sql.init.postgres <- function() {
options(sqldf.RPostgreSQL.user ="sinan",
sqldf.RPostgreSQL.dbname ="postgres",
sqldf.RPostgreSQL.host ="localhost",
sqldf.RPostgreSQL.port =5432)
}

#' Load SQL command from a file in which the SQL commands are recorded.
#' @param path the full path of the SQL file.
#' @return a string of command.
#' @export sql.load_file
sql.load_file <- function(path) {
sqlfile <- file(path)
cmd <- paste(readLines(path), collapse="")
close(sqlfile)
return(cmd)
}

#' Remove all the existing tables from the database and load the new schema
#' file.
#' @param schema is the full path of the selected schema file.
#' @export sql.newdb
sql.newdb <- function(schema=NULL) {
if (is.null(schema))
schema <- paste(find.package("ccdata"), "sql/create_table.sql", sep="/")

sql.init.postgres()
cmd <- sql.load_file(schema)
sqldf(cmd)
}

#' Add demographic table to the postgreSQL table.
#' @param record
#' @export sql.add.demographic
sql.add.demographic <- function(record) {
sql.init.postgres()
sqldf('DROP TABLE IF EXISTS demographic;')
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="sinan", dbname="postgres")
demogt <- sql.demographic.table(record, dtype=TRUE)
dbWriteTable(con, "demographic", demogt)
dbDisconnect(con)
invisible(demogt)
}
20 changes: 20 additions & 0 deletions R/stdid.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ as.number <- function(obj) {
.as.number <- function(code) {
return(as.numeric(strsplit(code, "NIHR_HIC_ICU_")[[1]][2]))
}


all.nhic.code <- function(cls) {
data.checklist[data.checklist$Classification1 == cls,"NHICcode"]
}

code2stname <- function(code) {
ccdata.env$code2stname.dict[code]
}

stname2code <- function(stname) {
ccdata.env$stname2code.dict[stname]
}

short2longname <- function(stname) {
longname <- array("NULL", length(stname))
for (i in seq_along(stname))
longname[i] <- ccdata.env$ITEM_REF[[stname2code(stname[i])]]$dataItem
return(longname)
}
17 changes: 15 additions & 2 deletions R/zzz.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
.onLoad <- function(libname = find.package("ccdata"), pkgname = "ccdata") {
ccdata.env <<- new.env()
path <- find.package("ccdata")

reverse.name.value <- function(vec) {
new <- names(vec)
names(new) <- vec
return(new)
}
path <- find.package("ccdata")
data("data.checklist", package="ccdata")
ccdata.env$ITEM_REF <- yaml.load_file(paste(path, "data", "ITEM_REF.yaml", sep=.Platform$file.sep))

ITEM_REF <- yaml.load_file(paste(path, "data", "ITEM_REF.yaml", sep=.Platform$file.sep))
code2stname.dict <- sapply(ITEM_REF, function(x) x$shortName)
stname2code.dict <- reverse.name.value(code2stname.dict)

ccdata.env$ITEM_REF <- ITEM_REF
ccdata.env$code2stname.dict <- code2stname.dict
ccdata.env$stname2code.dict <- stname2code.dict


assign('code_pas_number', getItemInfo("PAS number")["NHIC_code"], envir=ccdata.env)
assign('code_nhs_number', getItemInfo("NHS number")["NHIC_code"], envir=ccdata.env)
Expand Down
Loading