-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.R
More file actions
85 lines (67 loc) · 2.26 KB
/
Copy pathtest.R
File metadata and controls
85 lines (67 loc) · 2.26 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Install package
#install.packages('RMySQL', repos = "http://cran.us.r-project.org")
#install.packages('servr', repos = "http://cran.us.r-project.org")
myString <- "Loading..."
print(myString)
# Loading library
library("RMySQL")
# Создание подключения ...
mysqlconn = dbConnect(MySQL(), user = 'root', password = '', dbname = 'test', host = 'localhost', port = 3306)
# формирование строк таблицы ...
ViewSelect <- function() {
s_out <- list()
# заголовок таблицы.
res = dbSendQuery(mysqlconn, "SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'myarttable' AND TABLE_SCHEMA = 'test'")
df <- dbFetch(res)
for(sRow in df){
s_out = c(s_out, "<tr>")
s_out = c(s_out, paste("<td>", sRow, "</td>"))
s_out = c(s_out, "</tr>")
}
dbClearResult(res)
# строки таблицы.
res = dbSendQuery(mysqlconn, "SELECT * FROM myarttable WHERE id>14 ORDER BY id DESC")
df <- dbFetch(res)
# транспонирование строк и столбцов.
df <- as.data.frame(t(df))
for(sRow in df){
s_out = c(s_out, "<tr>")
s_out = c(s_out, paste("<td>", sRow, "</td>"))
s_out = c(s_out, "</tr>")
}
dbClearResult(res)
return(s_out)
}
# вервия базы данных ...
ViewVer <- function() {
res = dbSendQuery(mysqlconn, "SELECT VERSION() AS ver")
s_ver <- dbFetch(res)
dbClearResult(res)
return(s_ver)
}
GetHTML <- function() {
list_out <- list()
# построчное чтение файла.
con = file("select.html", "r")
while ( TRUE ) {
line = readLines(con, n = 1)
if (length(line) == 0) {break}
#print(line)
if ((!grepl("@tr", line)) && (!grepl("@ver", line))) {
list_out = c(list_out, line)
}
if (grepl("@tr", line)) {
list_out = c(list_out, ViewSelect())
print("This is TR.")
}
if (grepl("@ver", line)) {
list_out = c(list_out, ViewVer())
print("This is VER.")
}
}
close(con)
return(list_out)
}
# запуск сервера и ожидание подключений ...
print("Listening for connections at http://127.0.0.1:4321/ ...")
servr::create_server(handler = function(req) {list(status = 200L, body = paste(unlist(GetHTML()), collapse=''))})