-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.R
48 lines (38 loc) · 1.25 KB
/
database.R
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
39
40
41
42
43
44
45
46
47
'
Script : database
Created : February, 2014
Author(s) : iHub Research
Version : v1.0
License : Apache License, Version 2.0
'
# extract tweets from database
GetText <- function(keyword, number, db.table='tweet_data') {
# fetch text column from a db query result
sql <-paste("SELECT * FROM ",db.table," WHERE [text] like '%",keyword,"%' LIMIT ", number,"", sep="")
return(GetQuery(sql)['text'])
}
# create temporary table
CreateTempTable <- function(data, table.name='intermediate', conn=db.conn) {
'if(dbExistsTable(conn, table.name)) {
dbRemoveTable(conn, table.name)
} else {'
dbWriteTable(conn, table.name, data)
#}
return(table.name)
}
# subset data from database
SubsetDb <- function(assoc_words, event, table.name='intermediate') {
df = data.frame()
for (rn in rownames(assoc_words)) {
qry_str = paste("SELECT * FROM ", table.name, " WHERE [text] like '%",
event, "%'","AND [text] like '% ", rn, "%'" ,sep="")
df_tmp <- GetQuery(qry_str)
df <-rbind(df,df_tmp)
}
return(df)
}
GetQuery <- function(query, conn=db.conn) {
# run a query on a database connection and return the result
# Args: query : sql statement, conn : database connection
return(dbGetQuery(conn, query))
}