[58c332]: / archives / RadETL / Examples / DBMSio_Ex.R

Download this file

39 lines (29 with data), 1.5 kB

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