|
a |
|
b/archives/RadETL/Examples/DBMSio_Ex.R |
|
|
1 |
############################# Example Code: Data frame transfer RDBMS.. ############################# |
|
|
2 |
|
|
|
3 |
# First, connect RDBMS |
|
|
4 |
# I'm not recommend to input server information in programming code (Security issues) |
|
|
5 |
# So, I recommend to use environment variables in operating system |
|
|
6 |
dbms <- "sql server" |
|
|
7 |
user <- Sys.getenv("user") |
|
|
8 |
pw <- Sys.getenv("pw") |
|
|
9 |
server <- Sys.getenv("dbServer") |
|
|
10 |
|
|
|
11 |
# Choose Database name |
|
|
12 |
databaseSchema <- 'Radiology_CDM.dbo' |
|
|
13 |
|
|
|
14 |
# Using DatabaseConnector package |
|
|
15 |
io <- DBMSIO$new(server = server, user = user, pw = pw, dbms = dbms) |
|
|
16 |
|
|
|
17 |
# df is radiology Data frame,, |
|
|
18 |
# The insertDB method checks the row in the data frame and automatically inserts the table name. |
|
|
19 |
# If the data frame does not fit the R-CDM, an error occurs. |
|
|
20 |
# You can also inherit the insertTable from the DatabaseConnector package to accept the options for that function. |
|
|
21 |
# e.q dropTableIfExists etc. |
|
|
22 |
io$insertDB(dbS = databaseSchema, df = df) |
|
|
23 |
|
|
|
24 |
################################## Example Code: Get data for RDBMS.... ############################# |
|
|
25 |
|
|
|
26 |
# Using DBMSIO class in RadETL package |
|
|
27 |
# table schema names are Radiology_Image and Radiology_Occurrence |
|
|
28 |
tbSchema <- "Radiology_Occurrence" |
|
|
29 |
df <- io$dbGetdtS(dbS = databaseSchema, tbS = tbSchema) |
|
|
30 |
|
|
|
31 |
# include conditions.... |
|
|
32 |
df <- io$dbGetdtS(dbS = databaseSchema, tbS = tbSchema, condition = "Image_total_count < 20") |
|
|
33 |
df$RADIOLOGY_OCCURRENCE_ID |
|
|
34 |
|
|
|
35 |
# if want disconnect (required) |
|
|
36 |
io$finalize() |
|
|
37 |
|
|
|
38 |
######################################## Example Code: END ########################################## |