Skip to content

Commit 65da273

Browse files
committed
importing files
1 parent 3e40f5f commit 65da273

15 files changed

+525
-0
lines changed

NoacStudy/.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

NoacStudy/NAMESPACE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Generated by roxygen2 (4.1.1): do not edit by hand
2+
3+
export(email)
4+
export(execute)
5+
export(loadOhdsiStudy)
6+
export(saveOhdsiStudy)
7+
import(DBI)
8+
import(DatabaseConnector)
9+
import(SqlRender)
10+
importFrom(DBI,dbDisconnect)

NoacStudy/NoacStudy.Rproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 4
10+
Encoding: UTF-8
11+
12+
RnwWeave: knitr
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageInstallArgs: --no-multiarch --with-keep.source
20+
PackageRoxygenize: rd,namespace

NoacStudy/R/NoacStudy-package.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#' SkeletonStudy.
2+
#'
3+
#' @name SkeletonStudy
4+
#' @docType package
5+
#' @import DBI
6+
#' @import SqlRender
7+
#' @import DatabaseConnector
8+
NULL

NoacStudy/R/StudyInvariant.R

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#' @title Load OHSDI study
2+
#'
3+
#' @details
4+
#' This function loads an OHDSI study results from disk file.
5+
#'
6+
#' @param file (Optional) Name of local file to place results; makre sure to use forward slashes (/)
7+
#' @param verbose Logical: print R object names that are loaded
8+
#'
9+
#' @return
10+
#' A list of class type \code{OhsiStudy} that contains all saved study objects
11+
#'
12+
#' @export
13+
loadOhdsiStudy <- function(file,
14+
verbose = FALSE) {
15+
16+
if (missing(file)) file <- getDefaultStudyFileName()
17+
18+
# Return list of results
19+
tmp <- new.env()
20+
load(file, envir = tmp, verbose = verbose)
21+
result <- mget(ls(tmp), envir = tmp)
22+
class(result) <- "OhdsiStudy"
23+
return (result)
24+
}
25+
26+
#' @title Save OHDSI study
27+
#'
28+
#' @details
29+
#' This function saves an OHDSI study to disk file. All objects are written using \code{\link{save}}
30+
#' format and can be read back from file at a later time by using the function \code{\link{loadOhdsiStudy}}.
31+
#'
32+
#' @param list A list of R objects to save to disk file.
33+
#' @param file (Optional) Name of local file to place results; makre sure to use forward slashes (/)
34+
#' @param compress Logical or character string specifying the use of compression. See \code{\link{save}}
35+
#' @param includeMetadata Logical: include metadata about user and system in saved file
36+
#'
37+
#' @export
38+
saveOhdsiStudy <- function(list,
39+
file,
40+
compress = "xz",
41+
includeMetadata = TRUE) {
42+
43+
if (missing(list)) {
44+
stop("Must provide object list to save")
45+
}
46+
47+
if (missing(file)) file <- getDefaultStudyFileName()
48+
49+
if (includeMetadata) {
50+
metadata <- list()
51+
52+
metadata$r.version <- R.Version()$version.string
53+
info <- Sys.info()
54+
metadata$sysname <- info[["sysname"]]
55+
metadata$user <- info[["user"]]
56+
metadata$nodename <- info[["nodename"]]
57+
metadata$time <- Sys.time()
58+
assign("metadata", metadata, envir = parent.frame()) # place in same environment as named objects
59+
list <- c(list, "metadata")
60+
}
61+
62+
save(list = list,
63+
file = file,
64+
envir = parent.frame(1),
65+
compress = compress)
66+
}
67+
68+
#' @keywords internal
69+
invokeSql <- function(fileName, dbms, conn, text, use.ffdf = FALSE, quiet = TRUE) {
70+
71+
parameterizedSql <- SqlRender::readSql(system.file(paste("sql/","sql_server",sep=""),
72+
fileName,
73+
package="PGxDrugStudy"))
74+
renderedSql <- SqlRender::renderSql(parameterizedSql)$sql
75+
translatedSql <- SqlRender::translateSql(renderedSql,
76+
sourceDialect = "sql server",
77+
targetDialect = dbms)$sql
78+
writeLines(text)
79+
if (use.ffdf) {
80+
return (DatabaseConnector::dbGetQuery.ffdf(conn, translatedSql,
81+
quiet = quiet))
82+
} else {
83+
return (DBI::dbGetQuery(conn, translatedSql))
84+
}
85+
}
86+
87+
#' @title Email results
88+
#'
89+
#' @details
90+
#' This function emails the result CSV files to the study coordinator.
91+
#'
92+
#' @return
93+
#' A list of files that were emailed.
94+
#'
95+
#' @param from Return email address
96+
#' @param to (Optional) Delivery email address (must be a gmail.com acccount)
97+
#' @param subject (Optional) Subject line of email
98+
#' @param dataDescription A short description of the database
99+
#' @param file (Optional) Name of local file with results; makee sure to use forward slashes (/)
100+
#'
101+
#' @export
102+
email <- function(from,
103+
to,
104+
subject,
105+
dataDescription,
106+
file) {
107+
108+
if (missing(from)) stop("Must provide return address")
109+
if (missing(dataDescription)) stop("Must provide a data description")
110+
111+
if (missing(to)) to <- getDestinationAddress()
112+
if (missing(subject)) subject <- getDefaultStudyEmailSubject()
113+
if (missing(file)) file <- getDefaultStudyFileName()
114+
115+
if(!file.exists(file)) stop(paste(c("No results file named '",file,"' exists"),sep = ""))
116+
117+
tryCatch({
118+
result <- mailR::send.mail(from = from,
119+
to = to,
120+
subject = subject,
121+
body = paste("\n", dataDescription, "\n",
122+
sep = ""),
123+
smtp = list(host.name = "aspmx.l.google.com",
124+
port = 25),
125+
attach.files = file,
126+
authenticate = FALSE,
127+
send = TRUE)
128+
if (result$isSendPartial()) {
129+
stop("Unknown error in sending email")
130+
} else {
131+
writeLines(c(
132+
"Sucessfully emailed the following file:",
133+
paste("\t", file, sep = ""),
134+
paste("to:", to)
135+
))
136+
}
137+
}, error = function(e) {
138+
writeLines(c(
139+
"Error in automatically emailing results, most likely due to security settings.",
140+
"Please manually email the following file:",
141+
paste("\t", file, sep = ""),
142+
paste("to:", to)
143+
))
144+
})
145+
}

NoacStudy/R/StudySpecific.R

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#' @title Execute OHDSI Study (FILL IN NAME)
2+
#'
3+
#' @details
4+
#' This function executes OHDSI Study (FILL IN NAME).
5+
#' This is a study of (GIVE SOME DETAILS).
6+
#' Detailed information and protocol are available on the OHDSI Wiki.
7+
#'
8+
#' @return
9+
#' Study results are placed in CSV format files in specified local folder and returned
10+
#' as an R object class \code{OhdsiStudy} when sufficiently small. The properties of an
11+
#' \code{OhdsiStudy} may differ from study to study.
12+
13+
#' @param dbms The type of DBMS running on the server. Valid values are
14+
#' \itemize{
15+
#' \item{"mysql" for MySQL}
16+
#' \item{"oracle" for Oracle}
17+
#' \item{"postgresql" for PostgreSQL}
18+
#' \item{"redshift" for Amazon Redshift}
19+
#' \item{"sql server" for Microsoft SQL Server}
20+
#' \item{"pdw" for Microsoft Parallel Data Warehouse (PDW)}
21+
#' \item{"netezza" for IBM Netezza}
22+
#' }
23+
#' @param user The user name used to access the server. If the user is not specified for SQL Server,
24+
#' Windows Integrated Security will be used, which requires the SQL Server JDBC drivers
25+
#' to be installed.
26+
#' @param domain (optional) The Windows domain for SQL Server only.
27+
#' @param password The password for that user
28+
#' @param server The name of the server
29+
#' @param port (optional) The port on the server to connect to
30+
#' @param cdmSchema Schema name where your patient-level data in OMOP CDM format resides
31+
#' @param resultsSchema (Optional) Schema where you'd like the results tables to be created (requires user to have create/write access)
32+
#' @param file (Optional) Name of local file to place results; makre sure to use forward slashes (/)
33+
#' @param ... (FILL IN) Additional properties for this specific study.
34+
#'
35+
#' @examples \dontrun{
36+
#' # Run study
37+
#' execute(dbms = "postgresql",
38+
#' user = "joebruin",
39+
#' password = "supersecret",
40+
#' server = "myserver",
41+
#' cdmSchema = "cdm_schema",
42+
#' resultsSchema = "results_schema")
43+
#'
44+
#' # Email result file
45+
#' email(from = "collaborator@@ohdsi.org",
46+
#' dataDescription = "CDM4 Simulated Data")
47+
#' }
48+
#'
49+
#' @importFrom DBI dbDisconnect
50+
#' @export
51+
execute <- function(dbms, user = NULL, domain = NULL, password = NULL, server,
52+
port = NULL,
53+
cdmSchema, resultsSchema,
54+
file = getDefaultStudyFileName(),
55+
...) {
56+
# Open DB connection
57+
connectionDetails <- DatabaseConnector::createConnectionDetails(dbms=dbms,
58+
server=server,
59+
user=user,
60+
domain=domain,
61+
password=password,
62+
schema=cdmSchema,
63+
port = port)
64+
conn <- DatabaseConnector::connect(connectionDetails)
65+
if (is.null(conn)) {
66+
stop("Failed to connect to db server.")
67+
}
68+
69+
# Record start time
70+
start <- Sys.time()
71+
72+
# Place execution code here
73+
result <- 42
74+
75+
# Execution duration
76+
executionTime <- Sys.time() - start
77+
78+
# List of R objects to save
79+
objectsToSave <- c(
80+
"result",
81+
"executionTime"
82+
)
83+
84+
# Save results to disk
85+
saveOhdsiStudy(list = objectsToSave, file = file)
86+
87+
# Clean up
88+
DBI::dbDisconnect(conn)
89+
90+
# Package and return result if return value is used
91+
result <- mget(objectsToSave)
92+
class(result) <- "OhdsiStudy"
93+
invisible(result)
94+
}
95+
96+
# Package must provide a default gmail address to receive result files
97+
#' @keywords internal
98+
getDestinationAddress <- function() { return("[email protected]") }
99+
100+
# Package must provide a default result file name
101+
#' @keywords internal
102+
getDefaultStudyFileName <- function() { return("OhdsiStudy.rda") }
103+
104+
# Packge must provide default email subject
105+
#' @keywords internal
106+
getDefaultStudyEmailSubject <- function() { return("OHDSI Study Results") }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*********************************************************************************
2+
# Copyright 2014-2015 Observational Health Data Sciences and Informatics
3+
#
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
********************************************************************************/
17+
18+
-- Place parameterized SQL statements in this file
19+
20+
{DEFAULT @tableName = 'drug_era'}
21+
22+
SELECT COUNT(DISTINCT person_id) FROM @tableName;

NoacStudy/man/SkeletonStudy.Rd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/NoacStudy-package.R
3+
\docType{package}
4+
\name{SkeletonStudy}
5+
\alias{SkeletonStudy}
6+
\alias{SkeletonStudy-package}
7+
\title{SkeletonStudy.}
8+
\description{
9+
SkeletonStudy.
10+
}
11+

NoacStudy/man/email.Rd

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/StudyInvariant.R
3+
\name{email}
4+
\alias{email}
5+
\title{Email results}
6+
\usage{
7+
email(from, to, subject, dataDescription, file)
8+
}
9+
\arguments{
10+
\item{from}{Return email address}
11+
12+
\item{to}{(Optional) Delivery email address (must be a gmail.com acccount)}
13+
14+
\item{subject}{(Optional) Subject line of email}
15+
16+
\item{dataDescription}{A short description of the database}
17+
18+
\item{file}{(Optional) Name of local file with results; makee sure to use forward slashes (/)}
19+
}
20+
\value{
21+
A list of files that were emailed.
22+
}
23+
\description{
24+
Email results
25+
}
26+
\details{
27+
This function emails the result CSV files to the study coordinator.
28+
}
29+

0 commit comments

Comments
 (0)