-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
142 lines (106 loc) · 4.37 KB
/
server.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
132
133
134
135
136
137
138
139
140
141
142
# This is the server script, it takes a file upload (myData) and performs automated runchart data wrangling using function "stable".
# It then plots the resulting dataframe into a chart and outputs the chart to the UI script. It also takes
# a number of user inputs (e.g. annotations) to customise the output. It closes with a download tool so that
# an image of the chart (named with the upload filename) can be downloaded.
shinyServer <- function(input, output) {
myData <- reactive({
inFile <- input$datafile
if (is.null(inFile)) return(NULL)
if (endsWith(inFile$name, '.xlsx')){
data <- read_xlsx(inFile$datapath)
}
else if (endsWith(inFile$name, '.xls')){
data <- read_xls(inFile$datapath)
}
else if (endsWith(inFile$name, '.csv')){
data <- read_csv(inFile$datapath)%>%
mutate_at(1, .funs = funs(dmy(.)))
}
###WORK NEEDED: currently configured to Excel dates - needs work to accept different formats
data <- data%>%
mutate(date = as_date(as.POSIXct(as.numeric(date),origin = "1970-01-01", tz = "GMT")))%>%
arrange(date)%>%
mutate(value = as.numeric(value))
})
#trigger <- reactive({
# trigger <- input$trigger
#})
#baseline <- reactive({
# baseline <- input$baseline
#})
shiftsens <- reactive({
shiftsens <- input$shiftsens
})
heading <- eventReactive(input$allGo,{
heading <- input$heading
}, ignoreNULL = FALSE)
xaxis <- eventReactive(input$allGo,{
xaxis <- input$xaxis
}, ignoreNULL = FALSE)
yaxis <- eventReactive(input$allGo,{
yaxis <- input$yaxis
}, ignoreNULL = FALSE)
percentage <- reactive({
percentage <- input$percentage
})
anno1 <- eventReactive(input$allGo,{
# if(is.null(input$anno1))
# {return()}
#
# else {
anno1 <- input$anno1
#}
}, ignoreNULL = FALSE)
event1 <- reactive({
event1 <- input$event1
})
rundata <- reactive({
if (is.null(myData()))
{return()}
else {
RunChart(myData()[[2]], myData()[[1]], shiftsens(), percentage())
}
})
runplot <- reactive ({
rundata <- rundata()
ggplot(rundata, aes(x = subgroup)) +
geom_line(aes(y=measure, group = 1), colour = "blue", size = 1) +
geom_point(aes(y=measure, group = 1), colour = "#00a2e5", size = 2) +
geom_line(aes(y=median, group = base_n), linetype = "longdash", colour = "#ffcd04") +
geom_line(aes(y=baselines, group = base_n), linetype = "solid", colour = "#ffcd04", size = 1) +
geom_point(aes(y=highlight, group = 1), colour = "#ffcd04", size = 2) +
geom_text(aes(y = median, label = base_label), vjust = 1, hjust = 0) +
geom_point(aes(y=as.numeric(trendind), group = 1), shape = 1, size = 5, colour = "#007db3") +
theme(axis.text.x=element_text(angle = 90, hjust = 0), panel.background = element_rect(fill = "transparent")) +
geom_vline(xintercept = event1(), linetype = "dashed")+
geom_text(x = event1(), label = stringr::str_wrap(anno1(),30), y = max(as.numeric(rundata$measure))*0.1, vjust = 1)+
scale_y_continuous(limits=c(0, max(as.numeric(rundata$measure))+0.1*max(as.numeric(rundata$measure))), expand = c(0, 0)) +
#scale_x_continuous(breaks=pretty(subgroup, n=30)) +
#scale_x_discrete(breaks = xbreaks) +
xlab(xaxis()) + ylab(yaxis()) +
scale_x_date(breaks = seq(min(rundata$subgroup), max(rundata$subgroup), length.out = 9),
limits = c(min(rundata$subgroup), max(rundata$subgroup)),
date_labels = "%d %b\n%Y")+
ggtitle(heading())+
theme_classic()+
theme(plot.title = element_text(size = 14, face = "bold"),
axis.title.x = element_text(size = 11, face = "bold"),
#axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.y = element_text(size = 11, face = "bold"))
}) # ggplot chart
output$runchart <- renderPlot({
if (is.null(myData()))
{return()}
else {runplot()}
})
#output$rundata <- renderTable({rundata()})
#Download handler produces image of chart named by filename of data upload.
output$pullchart <- downloadHandler(
filename = function() {
paste0("Runchart of ", sub(pattern = "(.*)\\..*$", replacement = "\\1", input$datafile$name), ".png", sep = "")},
content = function(file) {
ggsave(file, plot = runplot(), device = "png")
}
)
}
## END