library(shiny)
library(shinydashboard)
## 
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(shinyjs)
## 
## Attaching package: 'shinyjs'
## The following object is masked from 'package:shiny':
## 
##     runExample
## The following objects are masked from 'package:methods':
## 
##     removeClass, show
library(glue)
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(shinyauthr)
library(ggradar)
library(scales)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(reshape2)
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dplyr)
library(purrr)
## 
## Attaching package: 'purrr'
## The following object is masked from 'package:scales':
## 
##     discard
library(emojifont)
library(haven)
library(DT)
## 
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
library(crosstalk)
## 
## Attaching package: 'crosstalk'
## The following object is masked from 'package:shiny':
## 
##     getDefaultReactiveDomain
# https://stackoverflow.com/questions/40833809/add-legend-to-geom-line-graph-in-r
#https://stackoverflow.com/questions/7744379/elegant-way-to-select-the-color-for-a-particular-segment-of-a-line-plot

path <- "//na.jnj.com/rndusdfsroot/all/bnp/JNJ-73763989_JNJ-56136379/DATACENTER_EU/HPB2001/DBR_Primary_analysis/RE_Primary_analysis/Pdev/EChen13/R/DynamicPlot/AlldataForPlot/"
# data1<-read_sas(paste0(path,"hbsag_200mg.sas7bdat"), NULL) 
data1<-read_sas(paste0(path,"hbsag_visit_2.sas7bdat"), NULL) 

data1<-data1[complete.cases(data1),]
data1<-data1[which(data1$AVAL<=1000),]
# data1<-data1[which(data1$USUBJID=="73763989HPB2001-100183"),]
# data1<-data1[which(data1$USUBJID=="73763989HPB2001-100071"),]
data1<-data1[which(data1$USUBJID %in% c("73763989HPB2001-100071","73763989HPB2001-100183")),]
#create rest line segament including off and restart
offtrt<-data1[which(data1$TRTFL %in% c("Off treatment","Restarted NA")),]
NArst<-data1[which(data1$TRTFL =="Restarted NA"),]
spagtiplotT_T<-data1

breakvalue1<-c(10,100,1000)

spagtiplotT_T<-spagtiplotT_T[,c("USUBJID",'visit',"AVAL","TRTFL" )]
d <- crosstalk::SharedData$new(spagtiplotT_T, ~USUBJID)
ggplot(d, aes(visit,AVAL, group = USUBJID)) + geom_point(size=0.1)+geom_line(size=0.2)+
  geom_line(data=offtrt,size=0.3)+
  geom_line(data=NArst,size=0.3)

e<-crosstalk::SharedData$new(offtrt, ~USUBJID)

p<- ggplot(d, aes(visit,AVAL, group = USUBJID)) + 
  scale_y_continuous("",breaks=breakvalue1,labels=c("<10","<100","<1000"))+
  geom_line(size=0.1)+
  geom_line(data=e,size=0.5,aes(visit,AVAL, group = USUBJID,colour="Off treatment"))+
  geom_line(data=NArst,size=0.5,aes(colour="Restarted NA"))+
   labs(y="HBsAg Value", x="Avisitn", alpha=0.3)+
  scale_color_manual(name="Treatment Status", values=c("Off treatment"="blue","Restarted NA"="red"))+
  theme(legend.title = element_blank())
  



ggplotly(p, tooltip =c("visit","USUBJID","AVAL" )) %>%layout(legend=list(title=list(text="Treatments"),x=100, y=0.9))%>%highlight(on = "plotly_hover", off="plotly_relayout", color = "red")
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
data1<-read_sas(paste0(path,"hbsag_visit_2.sas7bdat"), NULL) 

data1<-data1[complete.cases(data1),]
data1<-data1[which(data1$AVAL<=1000),]
#create rest line segament including off and restart
offtrt<-data1[which(data1$TRTFL %in% c("Off treatment","Restarted NA")),]
NArst<-data1[which(data1$TRTFL =="Restarted NA"),]
spagtiplotT_T<-data1

breakvalue1<-c(10,100,1000)


spagtiplotT_T<-spagtiplotT_T[,c("USUBJID",'visit',"AVAL","TRTFL" )]
d <- crosstalk::SharedData$new(spagtiplotT_T, ~USUBJID)

e<-crosstalk::SharedData$new(offtrt, ~USUBJID)

p<- ggplot(d, aes(visit,AVAL, group = USUBJID)) + 
  scale_y_continuous("",breaks=breakvalue1,labels=c("<10","<100","<1000"))+
  geom_line(size=0.1)+
  geom_line(data=e,size=0.5,aes(colour="Off treatment"))+
  geom_line(data=NArst,size=0.5,aes(colour="Restarted NA"))+
   labs(y="HBsAg Value", x="Avisitn", alpha=0.3)+
  scale_color_manual(name="Treatment Status", values=c("Off treatment"="blue","Restarted NA"="red"))+
  theme(legend.title = element_blank())
  
  
  


# p<- ggplot(d, aes(visit,AVAL, group = USUBJID)) + 
#   scale_y_continuous("",breaks=breakvalue1,labels=c("<10","<100","<1000"))+
#   annotate("rect",ymin=-Inf,ymax=10,xmin = -Inf, xmax = Inf,alpha=0.2, fill="#e0e0e0")+
#   annotate("rect",ymin=10.001,ymax=100, xmin = -Inf, xmax = Inf,alpha=0.2, fill="#c7c7c7")+
#   annotate("rect",ymin=100.001,ymax=Inf,xmin = -Inf, xmax = Inf,alpha=0.2, fill="#3333FF")+
#   geom_line(aes(colour=TRTFL),size=0.1)+labs(y="HBsAg Value", x="Avisitn", alpha=0.3)
#   # geom_rect(aes(ymin=-Inf,ymax=10,xmin = -Inf, xmax = Inf), fill="#e0e0e0",alpha=0.2)+
#   # geom_rect(aes(ymin=10.001,ymax=100, xmin = -Inf, xmax = Inf), fill="#c7c7c7", alpha=0.2)+
#   # geom_rect(aes(ymin=100.001,ymax=Inf,xmin = -Inf, xmax = Inf), fill="#bababa", alpha=0.2)+
ggplotly(p, tooltip =c("visit","USUBJID","AVAL" )) %>%layout(legend=list(title=list(text="Treatments"),x=100, y=0.9))%>%highlight(on = "plotly_hover", off="plotly_relayout", color = "#ff00ff")
ggplotly(p, tooltip =c("visit","USUBJID","AVAL" )) %>%layout(legend=list(title=list(text="Treatments"),x=100, y=0.9))%>%highlight(on = "plotly_hover", off="plotly_relayout", color = "#ff00ff")
  p<-p+geom_point(size=0.05)
ggplotly(p, tooltip =c("visit","USUBJID","AVAL" )) %>%layout(legend=list(title=list(text="Treatments"),x=100, y=0.9))%>%highlight(on = "plotly_hover", off="plotly_relayout", color = "#ff00ff")