-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.r
131 lines (122 loc) · 4.17 KB
/
app.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(shiny)
library(tidyverse)
library(DBI)
library(RMariaDB)
library(DT)
ui <- fluidPage(
headerPanel(title = "SIRPI QUIZ BOT"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Upload the csv file"),
textInput("quiz_name","Quiz",placeholder = "type here the name of the quiz"),
textInput("qm_name","Quiz Master",placeholder = "type here the name of the quiz master"),
textInput("marking_scheme","Marking scheme (+a/-b)",placeholder = "+3/-1",value = "+3/-1"),
dateInput("date1", "Start Date", value = Sys.Date()),
dateInput("date2", "End Date", value = Sys.Date()+7),
actionButton(
"go",
"Upload Quiz",
class = "btn-primary"
)
),
mainPanel(
DT::dataTableOutput('quiz_info'),
DT::dataTableOutput('input_file')
)
)
)
server <- function(input, output) {
upload_date <- reactive({
Sys.Date()
})
start_date <- reactive({
input$date1
})
end_date <- reactive({
input$date2
})
marking <- reactive({
input$marking_scheme
})
positive_marks<- reactive({
marking() %>% strsplit("/") %>% .[[1]] %>% parse_integer() %>% .[[1]]
})
negative_marks<- reactive({
marking() %>% strsplit("/") %>% .[[1]] %>% parse_integer() %>% .[[2]]
})
quiz_name <- reactive({
input$quiz_name
})
qm_name <- reactive({
input$qm_name
})
quiz_code <- reactive({
sc <- format(Sys.Date(), format="%m %d") %>% str_replace_all(" ", "")
paste0(sc,
quiz_name() %>% substring(1, 1) %>% toupper(),
qm_name() %>% substring(1, 1) %>% toupper())
})
quiz_data <- reactive({
req(input$file$datapath) %>% read_csv() %>% as.tibble() -> a
colnames(a) <-(c("question", "A", "B", "C", "D","correct"))
a %>% mutate(quiz_code = quiz_code())->a
a
})
quiz_info <- reactive({
quiz_info <- data.frame(
quiz_code = quiz_code(),
quiz_name = quiz_name(),
qm_name = qm_name(),
total_question = quiz_data() %>% nrow(),
positive_marks = positive_marks(),
negative_marks = negative_marks(),
upload_date = upload_date(),
start_date = start_date(),
end_date = end_date()
)
quiz_info
})
output$input_file <- DT::renderDataTable({
quiz_data() %>% select(c("question","A","B","C","D","correct")) %>% datatable(options = list(pageLength = 15,
lengthChange = FALSE,
searching = FALSE))
})
output$quiz_info <- DT::renderDataTable({
c1 <- c("Quiz","Quiz Master","Quiz Code","Marking Scheme","Start on","Ends on")
c2 <- c(quiz_name(),qm_name(),quiz_code(),marking(),start_date() %>% as.character(),end_date() %>% as.character())
c1 %>%
as.tibble() %>%
cbind(c2 %>% as.tibble()) %>%
datatable( rownames = F,colnames = '',
options = list(dom = 't',
columnDefs = list(list(className = 'dt-center',
targets = "_all"))
))
})
observeEvent(input$go,{
if(is.null(qm_name())){
showNotification("please fill Quiz Master name before uploading")
return()
}
else if(is.null(quiz_name())){
showNotification("please fill Quiz name before uploading")
return()
}
else if(is.null(quiz_data())){
showNotification("please load the csv file before uploading")
return()
}
else{
con <- dbConnect(drv = RMariaDB::MariaDB(),
dbname = DB_NAME,
host = HOST_NAME,
username = USER_NAME,
password = PASSWORD)
dbAppendTable(conn = con, name = "quiz_data", value = as.data.frame(quiz_data()))
dbAppendTable(conn = con, name = "quiz_info", value = as.data.frame(quiz_info()))
dbDisconnect(con)
showNotification("file uploaded successfully")
}
})
}
shinyApp(ui, server)