TakeCareApp/app/profil_module.R

415 lines
12 KiB
R
Raw Normal View History

2020-12-04 18:06:31 +01:00
library(shiny)
library(magrittr)
library(ggplot2)
library(plotly)
library(DT)
profilUI <- function(id) {
ns <- NS(id)
fluidPage(
2020-12-14 13:47:03 +01:00
useShinyjs(),
2021-01-19 23:04:09 +01:00
tags$head(
tags$script(src="js.cookie.js"),
tags$script('Shiny.addCustomMessageHandler("tokenHandlerUpdate",
2020-12-28 16:04:02 +01:00
function(token) {
sessionStorage.setItem(\'token\', token);
Shiny.onInputChange("token", token);
}
);'),
2021-01-19 23:04:09 +01:00
tags$script('Shiny.addCustomMessageHandler("profileActiveTabHandler",
2021-01-09 11:00:50 +01:00
function(arg) {
Shiny.onInputChange("profileActiveTab", 1);
}
);'),
2021-01-19 23:04:09 +01:00
tags$script('Shiny.addCustomMessageHandler("viewPage",
function(page,token,auth) {
Shiny.onInputChange("token", token);
Shiny.onInputChange("auth", auth);
window.location.href=page;
}
);'),
2021-01-19 23:04:09 +01:00
tags$style(HTML("
2020-12-04 18:06:31 +01:00
@import url('//fonts.googleapis.com/css?family=Lobster|Cabin:400,700');
@import url('//fonts.googleapis.com/css2?family=Fjalla+One');
2020-12-20 15:46:02 +01:00
")),
2021-01-19 23:04:09 +01:00
tags$link(rel = "stylesheet", type = "text/css", href = "profile.css")
2020-12-20 15:46:02 +01:00
),
2020-12-04 18:06:31 +01:00
2021-01-19 23:04:09 +01:00
# theme = "style.css",
# App title ----
uiOutput("afterLogin"),
fluidRow(
column(12,
tags$span("Copyright Wszystkie prawa zastrzeżone."))%>% tagAppendAttributes(id = 'column-copyright'),
)%>% tagAppendAttributes(id = 'row-footer')
)}
2020-12-04 18:06:31 +01:00
2020-12-14 13:47:03 +01:00
profilServer <- function(input, output,session) {
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(id, ...))
}
inputs
}
2020-12-14 13:47:03 +01:00
2020-12-20 15:46:02 +01:00
getEditStatus <- eventReactive(input$editSubmit, {
editedPersonalData<-data.frame(name<-input$editName,
2021-01-19 23:04:09 +01:00
surname<-input$editSurname,
mail<-input$editMail,
datebirth<-input$editAge,
gender<-input$editGender)
2020-12-20 15:46:02 +01:00
2021-01-19 23:04:09 +01:00
# reg<-c(grepl("^[A-Z][a-zA-ZĄąĆćĘꣳŃńÄ“ÄĹŚśŹźŻż]{2,15}$",editedPersonalData$name),
# grepl("^[A-Z][a-zA-ZĄąĆćĘꣳŃńÄ“ÄĹŚśŹźŻż]{2,20}$",editedPersonalData$surname),
# grepl("^[a-z]+[0-9]*@([a-z]{2,10}\\.)+[a-z]{2,5}$",editedPersonalData$mail))
2021-01-19 23:04:09 +01:00
reg<-c(grepl("^[A-Z][a-zA-ZĄąĆćĘęŁĹŃńÓóŚĹŹźŻż]{2,15}$",editedPersonalData$name),
grepl("^[A-Z][a-zA-ZĄąĆćĘęŁĹŃńÓóŚĹŹźŻż]{2,20}$",editedPersonalData$surname),
2020-12-20 15:46:02 +01:00
grepl("^[a-z]+[0-9]*@([a-z]{2,10}\\.)+[a-z]{2,5}$",editedPersonalData$mail))
if(all(reg)){
2021-01-19 23:04:09 +01:00
personalData = list(
name = editedPersonalData$name,
surname = editedPersonalData$surname,
email = editedPersonalData$mail,
datebirth = editedPersonalData$datebirth,
gender =editedPersonalData$gender)
2020-12-20 15:46:02 +01:00
to_send = list(
personalDataDTO = personalData)
2020-12-20 15:46:02 +01:00
2021-01-19 23:04:09 +01:00
2020-12-20 15:46:02 +01:00
2021-01-22 17:48:59 +01:00
r<-httr::PUT("https://syi-back.herokuapp.com/api/profile",add_headers(Authorization=paste("Bearer",input$token,sep=" ")),body=to_send,encode = 'json')
2020-12-20 15:46:02 +01:00
if (r$status_code==200){
TRUE
}else{
FALSE
}
}else{
FALSE
}
2021-01-19 23:04:09 +01:00
2020-12-20 15:46:02 +01:00
})
2021-01-19 23:04:09 +01:00
2021-01-19 17:56:28 +01:00
personalDataVector <- reactiveVal()
historyDataVector <- reactiveVal()
2021-01-22 13:42:15 +01:00
activeTab<-reactiveVal()
2021-01-19 17:56:28 +01:00
downloadPersonalData<-reactive({
2021-01-22 17:48:59 +01:00
r<-httr::GET("https://syi-back.herokuapp.com/api/profile",add_headers(Authorization=paste("Bearer",input$token,sep=" ")))
2021-01-19 23:04:09 +01:00
r
2021-01-19 17:56:28 +01:00
})
downloadHistoryData<-reactive({
2021-01-22 13:42:15 +01:00
2021-01-22 17:48:59 +01:00
r<-httr::GET("https://syi-back.herokuapp.com/api/prediction/usersPredictions/ind",add_headers(Authorization=paste("Bearer",input$token,sep=" ")),encode = 'json')
2021-01-22 13:42:15 +01:00
print("Reactive hist")
2021-01-19 17:56:28 +01:00
r
})
2020-12-14 13:47:03 +01:00
observe({
currPage = get_page()
2021-01-22 13:42:15 +01:00
if(currPage=="profil" | currPage=="calculator" | currPage=="iota"){
2021-01-19 17:56:28 +01:00
session$sendCustomMessage(type='tokenHandlerAccess',(session$clientData)$url_hash)
}
2021-01-19 23:04:09 +01:00
2020-12-14 13:47:03 +01:00
})
2020-12-20 15:46:02 +01:00
2021-01-22 13:42:15 +01:00
2021-01-19 17:56:28 +01:00
observeEvent(input$profileTabs,{
2021-01-22 13:42:15 +01:00
2020-12-20 15:46:02 +01:00
2021-01-19 17:56:28 +01:00
if(input$profileTabs=="data"){
2021-01-22 13:42:15 +01:00
2021-01-19 17:56:28 +01:00
personalDataVector(downloadPersonalData())
}else if(input$profileTabs=="history"){
2021-01-22 13:42:15 +01:00
2021-01-19 17:56:28 +01:00
historyDataVector(downloadHistoryData())
2021-01-09 11:00:50 +01:00
}
2021-01-19 17:56:28 +01:00
})
personalDataReturn <-reactive({
personalDataVector$values
})
historyDataReturn <-reactive({
historyDataVector()
})
observeEvent(input$del_button,{
2021-01-22 17:48:59 +01:00
r<-httr::DELETE(paste("https://syi-back.herokuapp.com/api/prediction/delete/",input$del_button,sep = ""),add_headers(Authorization=paste("Bearer",input$token,sep=" ")),encode = 'json')
2021-01-19 17:56:28 +01:00
if(r$status_code==200){
2021-01-22 17:48:59 +01:00
rr<-httr::GET("https://syi-back.herokuapp.com/api/prediction/usersPredictions/ind",add_headers(Authorization=paste("Bearer",input$token,sep=" ")),encode = 'json')
2021-01-09 11:00:50 +01:00
2021-01-19 17:56:28 +01:00
historyDataVector(rr)
2021-01-19 23:04:09 +01:00
2021-01-19 17:56:28 +01:00
}
})
output$profileData<-renderUI({
r <- personalDataVector()
2021-01-19 23:04:09 +01:00
2021-01-19 17:56:28 +01:00
if(length(r)!=0 & !is.null(r)){
2020-12-20 15:46:02 +01:00
2021-01-19 23:04:09 +01:00
if(r$status_code==200){
response<-(content(r))
session$sendCustomMessage(type='tokenHandlerUpdate', response$token)
fluidRow(column(12,
wellPanel(
textInput("editName", label = strong("Imię"),value=response$profil$personalDataDTO$name),
uiOutput("editName"),
textInput("editSurname", label = strong("Nazwisko"),value=response$profil$personalDataDTO$surname),
uiOutput("editSurname"),
textInput("editMail", label = strong("Adres email"),value=response$profil$personalDataDTO$email),
uiOutput("editMail"),
dateInput("editAge", label = strong("Data urodzenia") ,value=response$profil$personalDataDTO$datebirth),
selectInput("editGender", label = strong("Płeć"),
2021-01-25 13:31:00 +01:00
choices = list("Żeńska" = 0, "Męska" = 1),
2021-01-19 23:04:09 +01:00
selected = as.numeric(response$profil$personalDataDTO$gender)),
actionButton("editSubmit","Zapisz"),
uiOutput("btnEditProfile",style="color:red;")
2021-01-19 17:56:28 +01:00
),
2021-01-19 23:04:09 +01:00
))
}
2020-12-20 15:46:02 +01:00
}
2021-01-19 17:56:28 +01:00
2020-12-20 15:46:02 +01:00
})
output$btnEditProfile<-renderUI({
if (getEditStatus()==TRUE){
p("OK",style="color:green;text-align:center;")
}else{
2021-01-19 23:04:09 +01:00
p("Użytkownik istnieje lub wprowadzono błędne dane",style="color:red;text-align:center;")
2020-12-20 15:46:02 +01:00
}
})
2020-12-14 13:47:03 +01:00
output$afterLogin<-renderUI({
2021-01-22 13:42:15 +01:00
if(get_page()=="profil"){
if(!is.null(input$auth) & length(input$auth)>0 ){
2020-12-14 13:47:03 +01:00
fluidRow(
2021-01-19 23:04:09 +01:00
2021-01-19 17:56:28 +01:00
column(12,
tabsetPanel(id="profileTabs",type = "tabs",
tabPanel("Dane profilowe",value="data", tags$div(uiOutput("profileData")
) %>% tagAppendAttributes(id = 'content-personal')),
2021-01-19 23:04:09 +01:00
2021-01-19 17:56:28 +01:00
tabPanel("Historia pomiarów",value='history', tags$div(DT::dataTableOutput("historyTable",height = "auto"))%>% tagAppendAttributes(id="profileTabs",class = 'content-wrapper'))
2020-12-14 13:47:03 +01:00
2021-01-19 17:56:28 +01:00
))%>% tagAppendAttributes(id = 'column-profile')
2021-01-19 23:04:09 +01:00
2020-12-14 13:47:03 +01:00
) %>% tagAppendAttributes(id = 'row-content')
}else{
2021-01-22 13:53:58 +01:00
}}
2021-01-19 23:04:09 +01:00
2020-12-14 13:47:03 +01:00
})
2020-12-04 18:06:31 +01:00
2021-01-09 11:00:50 +01:00
2020-12-04 18:06:31 +01:00
output$plot1 <- renderPlotly({
2021-01-09 11:00:50 +01:00
2020-12-04 18:06:31 +01:00
g<-ggplot(mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
gg<-ggplotly(g)
gg
})
observeEvent(input$view_button,{
change_page(paste("?id=",input$view_button,"#!/iota",sep=""), session = session, mode = "push")
})
2021-01-19 23:04:09 +01:00
2021-01-09 11:00:50 +01:00
output$historyTable <- DT::renderDataTable({
2021-01-19 17:56:28 +01:00
r <- historyDataVector()
2021-01-09 11:29:22 +01:00
if(is.null(content(r)$predictions)){
DT::datatable(data.frame(Nazwa=character(),Wynik=numeric(),Data=character()),options = list(scrollX = TRUE,language=pl))
}else{
2021-01-19 23:04:09 +01:00
df_historyTable<-as.data.frame(do.call(rbind, (content(r)$predictions)))
2021-01-19 17:56:28 +01:00
2021-01-22 13:53:58 +01:00
historyTableButtons = list()
for(rowNumber in 1:nrow(df_historyTable)){
2021-01-19 17:56:28 +01:00
historyTableButtons[[rowNumber]] <-list(shinyInput(actionButton, 1, as.character(df_historyTable[rowNumber,]$id[1]), label = "Pokaż", onclick = 'Shiny.onInputChange(\"view_button\", this.id)' ),
shinyInput(actionButton, 1, as.character(df_historyTable[rowNumber,]$id[1]), label = "Usuń", onclick = 'Shiny.onInputChange(\"del_button\", this.id)'))
2021-01-22 13:53:58 +01:00
}
}
2021-01-09 11:00:50 +01:00
df_historyTable<-df_historyTable %>%
mutate(Akcja=historyTableButtons)%>%
select(name,resultValue,localDateTime,Akcja) %>%
2021-01-22 13:42:15 +01:00
mutate(localDateTime = (str_replace(strptime(str_replace(localDateTime,"T"," "),"%Y-%m-%d %H:%M:%S")," CET","")))%>%
2021-01-09 11:00:50 +01:00
rename(
Nazwa = name,
Wynik = resultValue,
Data = localDateTime
)
2021-01-22 13:42:15 +01:00
2021-01-09 11:00:50 +01:00
df_historyTable$Nazwa<-do.call(c, df_historyTable$Nazwa)
df_historyTable$Wynik<-do.call(c, df_historyTable$Wynik)
2021-01-22 13:53:58 +01:00
DT::datatable(df_historyTable,selection="none",options = list(scrollX = TRUE,language=pl))
2021-01-19 17:56:28 +01:00
# }
2021-01-09 11:00:50 +01:00
2021-01-19 23:04:09 +01:00
})
2021-01-09 11:00:50 +01:00
2020-12-04 18:06:31 +01:00
output$plot3 <- renderPlotly({
g<-ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(se = FALSE)
gg<-ggplotly(g)
2021-01-09 11:00:50 +01:00
2020-12-04 18:06:31 +01:00
gg
})
output$plot4 <- renderPlotly({
g<-ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color=drv)) +
geom_smooth(se = FALSE)
gg<-ggplotly(g)
gg
})
pl <- list(
2021-01-09 11:29:22 +01:00
emptyTable="Tabela jest pusta",
2020-12-04 18:06:31 +01:00
sSearch = "Szukaj",
2021-01-19 23:04:09 +01:00
sInfo="Wyniki od _START_ do _END_ z _TOTAL_ rekordów",
sZeroRecords="Brak rekordów",
2020-12-04 18:06:31 +01:00
sEmptyTable="Pusta tabela",
oPaginate= list(
2021-01-19 23:04:09 +01:00
sFirst="Pierwsza", sPrevious="Poprzednia",sLast="Ostatnia", sNext="Następna"
2020-12-04 18:06:31 +01:00
),
2021-01-19 23:04:09 +01:00
sLengthMenu = "Pokaż _MENU_ rekordów na stronie"
2020-12-04 18:06:31 +01:00
)
output$table1 <- DT::renderDataTable(iris,options = list(scrollX = TRUE,language=pl))
2020-12-20 15:46:02 +01:00
output$btnResponse<-renderUI({
if (getStatus()==TRUE){
p("OK",style="color:white;text-align:center;")
}else{
2021-01-19 23:04:09 +01:00
p("Użytkownik istnieje lub wprowadzono błędne dane",style="color:yellow;text-align:center;")
2020-12-20 15:46:02 +01:00
}
})
output$editName<-renderUI({
s<-toString(input$editName)
2021-01-19 23:04:09 +01:00
if (s=="" | grepl("^[A-Z][a-zA-ZĄąĆćĘꣳŃńÄ“ÄĹŚśŹźŻż]{2,15}$",s)==TRUE){
2020-12-20 15:46:02 +01:00
return()
}else{
2021-01-19 23:04:09 +01:00
p("Bład: Imię powinno zaczynać się od wielkiej litery, zawierać jedynie litery i mieć długość od 3 do 15 znaków",style="color:yellow")
2020-12-20 15:46:02 +01:00
}
})
output$editSurname<-renderUI({
s<-toString(input$editSurname)
2021-01-19 23:04:09 +01:00
if (s=="" | grepl("^[A-Z][a-zA-ZĄąĆćĘꣳŃńÄ“ÄĹŚśŹźŻż]{2,20}$",s)==TRUE){
2020-12-20 15:46:02 +01:00
return()
}else{
2021-01-25 13:31:00 +01:00
p("Bład: Nazwisko powinno zaczynać sie od wielkiej litery, zawierać jedynie litery i mieć długość od 3 do 15 znaków",style="color:yellow")
2020-12-20 15:46:02 +01:00
}
})
output$editMail<-renderUI({
s<-toString(input$editMail)
if (s=="" | grepl("^[a-z]+[0-9]*@([a-z]{2,10}\\.)+[a-z]{2,5}$",s)==TRUE){
return()
}else{
2021-01-19 23:04:09 +01:00
p("Bład: Mail powinien mieć budowę adres@nazwa.domena",style="color:yellow")
2020-12-20 15:46:02 +01:00
}
})
2020-12-04 18:06:31 +01:00
}