-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
224 lines (189 loc) · 10.4 KB
/
Copy pathserver.R
File metadata and controls
224 lines (189 loc) · 10.4 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
require("googlesheets")
require("tidyr")
require("zoo")
require("shiny")
require("shinydashboard")
require("scales")
require("devtools")
require("Cairo")
require("plotly")
require("ggplot2")
require("DT")
ST <- gs_url("https://docs.google.com/spreadsheets/d/18CmRPdJjxcaH7_FmKBdUYlW2-Y5DZJZdE2xvZepX1dk/edit#gid=0")
give.n <- function(x){
return(c(y = 0, label = length(x)))
}
shinyServer(function(input, output) {
st_info <- gs_read(ss=ST, ws="Sheet1", skip=12)
st_data_sheet <- as.data.frame(st_info)
st_data_sheet <- na.locf(st_data_sheet)
st_data_sheet[st_data_sheet == "-"] = NA
st_data_sheet[,1] <- as.character(as.Date(st_data_sheet[,1], "%y/%m/%d"))
#Don't require complete rows at the moment
#st_data_sheet <- st_data_sheet[complete.cases(st_data_sheet),]
observeEvent(input$refresh_data, {
st_info <<- gs_read(ss=ST, ws="Sheet1", skip=12)
st_data_sheet <<- as.data.frame(st_info)
st_data_sheet <<- na.locf(st_data_sheet)
st_data_sheet[,1] <<- as.character(as.Date(st_data_sheet[,1], "%y/%m/%d"))
output$raw_data <<- DT::renderDataTable({st_data_sheet})
})
#Input menus for subsetting
output$subset_ddmenu <- renderUI({
selectInput("subset_ddmenu", label = "Choose column to subset on:", names(st_data_sheet), selected = "Person")
})
output$subset_checkboxes <- renderUI({
checkboxGroupInput("subset_checkboxes", label = "Choose elements to subset on:", choices = sort(unique(as.character(unlist(subset(st_data_sheet, select = input$subset_ddmenu))))))
})
#Create reactive data frame based on subsetting (if any)
st_data_subset <- reactive({
if (length(input$subset_checkboxes) > 0 ){
match_strings <- paste(input$subset_checkboxes, collapse = "|")
st_data_sheet[grep(match_strings, as.character(unlist(subset(st_data_sheet, select = input$subset_ddmenu)))),]
} else {
st_data_sheet
}
})
output$subset_st_data <- DT::renderDataTable({st_data_subset()})
#Download the current subset as a csv file
output$dl_csv_data <- downloadHandler(
filename = function() {
paste(input$dl_filename, ".csv", sep="")
},
content = function(file) {
write.csv(st_data_subset(), file)
}
)
#Write raw information out to table
output$raw_data <- DT::renderDataTable({st_data_sheet})
#Dropdown menus for scatter plot
output$splot_y <- renderUI({
selectInput("sctplot_y", label = "Choose first value to plot:", names(st_data_subset()), selected = "Number of Unique Genes")
})
output$splot_x <- renderUI({
selectInput("sctplot_x", "Choose second value to plot:", names(st_data_subset()), selected = "Number of Unique Transcripts")
})
output$splot_col <- renderUI({
selectInput("sctplot_col", "Choose how to colour the data:", names(st_data_subset()), selected = "Tissue Type")
})
output$splot_shape <- renderUI({
selectInput("sctplot_shape", "Choose how to shape the points:", names(st_data_subset()), selected = "Organism")
})
output$splot_norm_check <- renderUI({
radioButtons("splot_norm_check", "Do you want to normalise the data?", choices = c("No normalisation" = "no", "X Axis" = "x", "Y Axis" = "y", "Both Axes" = "x_y") , selected = "no")
})
output$splot_norm <- renderUI({
selectInput("splot_norm", "Choose how to normalise the data:", names(st_data_subset()), selected = "Number of Raw Reads")
})
output$splot_norm_scale <- renderUI({
textInput("splot_norm_scale", "Input a scaling factor:", value = 1)
})
#Scatterplot
output$scatterplot <- renderPlotly({
value1 <- as.numeric(unlist(subset(st_data_subset(), select = input$sctplot_y)))
value2 <- as.numeric(unlist(subset(st_data_subset(), select = input$sctplot_x)))
colour <- as.factor(unlist(subset(st_data_subset(), select = input$sctplot_col)))
shape_ <- as.factor(unlist(subset(st_data_subset(), select = input$sctplot_shape)))
norm_ <- as.numeric(unlist(subset(st_data_subset(), select = input$splot_norm)))
norm_scale <- as.numeric(input$splot_norm_scale)
splot_title <- paste("Scatterplot of ", input$sctplot_x, " against ", input$sctplot_y, ".", sep="")
sploty_value <- switch(input$splot_norm_check,
no = value1,
x = value1,
y = (value1/norm_)*norm_scale,
x_y = (value1/norm_)*norm_scale)
splotx_value <- switch(input$splot_norm_check,
no = value2,
x = (value2/norm_)*norm_scale,
y = value2,
x_y = (value2/norm_)*norm_scale)
splot_data <- aes(x=splotx_value,y=sploty_value, shape = shape_, color = colour,
text = paste("Array Batch: ", `Array Batch`, "\nArray: ", `Array Lot Number`, "\nWell: ", `Well Position`,
"\nTissue Type: ", `Tissue Type`, "\nPerson: ", `Person` , "\n", input$sctplot_y, ": ", sploty_value,
"\n", input$sctplot_x, ": ", splotx_value, sep = ""))
scatterplot <- ggplot(st_data_subset(), splot_data) + geom_point() + labs(colour = input$sctplot_col, shape = input$sctplot_shape) +
xlab(input$sctplot_x) + ylab(input$sctplot_y) + ggtitle(splot_title) + theme(plot.title = element_text(face = "bold")) +
scale_x_continuous(labels = comma) + scale_y_continuous(labels = comma) + theme(plot.margin = unit(c(1,4,1,1), "cm"))
ggplotly(scatterplot, tooltip = "text") %>% config(modeBarButtonsToRemove = c("zoom2d", "pan2d", "zoomIn2d", "zoomOut2d", "autoScale2d", "sendDataToCloud"), collaborate = FALSE, displaylogo = FALSE)
})
#Filetype to save plot as
output$splot_ft <- renderUI({
radioButtons("file_type", label = "Choose the output filetype", choiceNames = list("png", "pdf"), choiceValues = list("png", "pdf"), inline = TRUE)
})
#Download the current plot
output$splot_dl <- downloadHandler(
filename = function() {
paste("name", as.character(input$splot_ft), sep=".")
},
content = function(file) {
#ggsave(file, plot = scatterplot(), device = input$splot_ft)
#savePlot(file, type = input$splot_ft)
export(p = scatterplot(), file = "test.png")
}
)
#Dropdown menues for Box and Whisker plot
output$bandwplot_factor <- renderUI({
selectInput("bandwplot_factor", "Choose the group factor:", names(st_data_subset()), selected = "Organism")
})
output$bandwplot_data <- renderUI({
selectInput("bandwplot_data", "Select the data for the plot:", names(st_data_subset()), selected = "Number of Unique Transcripts")
})
output$bandwplot_col <- renderUI({
selectInput("bandwplot_col", "Select the group to colour on:", names(st_data_subset()), selected = "Well Position")
})
output$bandwplot_usecol <- renderUI({
checkboxInput("bandwplot_usecol", "Do you want to colour the data?", value = TRUE)
})
#Box and Whisker plot
output$bandwplot <- renderPlotly({
bandwplot_factor <- as.factor(unlist(subset(st_data_subset(), select = input$bandwplot_factor)))
bandwplot_data <- switch(input$anova_norm_check,
"No" = as.numeric(unlist(subset(st_data_subset(), select = input$bandwplot_data))),
"Yes" = as.numeric(unlist(subset(st_data_subset(), select = input$bandwplot_data)))/as.numeric(unlist(subset(st_data_subset(), select = input$anova_norm)))
)
bandwplot_lab <- switch(as.character(input$bandwplot_usecol),
"FALSE" = input$bandwplot_factor,
"TRUE" = input$bandwplot_col
)
bandwplot_title <- paste("Box and Whisker Plot Showing ", input$bandwplot_data, " for ", input$bandwplot_factor, ".", sep="")
bandwplot_colour <- switch(as.character(input$bandwplot_usecol),
"FALSE" = as.factor(unlist(subset(st_data_subset(), select = input$bandwplot_factor))),
"TRUE" = as.factor(unlist(subset(st_data_subset(), select = input$bandwplot_col)))
)
bandwplot_xlab <- paste(levels(bandwplot_factor), "\nN=",table(bandwplot_factor, useNA = "no"), sep="")
bandwplot <- aes(bandwplot_factor, bandwplot_data, color = bandwplot_colour)
bandwplot <- ggplot(st_data_subset(), bandwplot) + geom_boxplot() + ggtitle(bandwplot_title) + xlab(input$bandwplot_factor) + scale_x_discrete(labels=bandwplot_xlab) +
ylab(input$bandwplot_data) + theme(plot.title = element_text(face = "bold"), text = element_text(margin = margin())) + scale_y_continuous(labels = comma) +
labs(colour = bandwplot_lab) + theme(plot.margin = unit(c(1,4,1,1), "cm")) +
stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.70))
ggplotly(bandwplot) %>% config(modeBarButtonsToRemove = c("zoom2d", "pan2d", "zoomIn2d", "zoomOut2d", "autoScale2d", "sendDataToCloud"), collaborate = FALSE, displaylogo = FALSE) %>% layout(boxmode = "group")
})
#Normalisation for ANOVA
output$anova_norm_check <- renderUI({
radioButtons("anova_norm_check", "Do you want to normalise the data for the ANOVA?", choices = c("Yes", "No"), selected = "No", inline = TRUE)
})
output$anova_norm <- renderUI({
selectInput("anova_norm", "Choose how to normalise the data:", names(st_data_subset()), selected = "Number of Raw Reads")
})
#Table that shows number of observations in each factor
output$factor_table <- renderTable({table(as.factor(unlist(subset(st_data_subset(), select = input$bandwplot_factor))))})
output$factor_anova <- DT::renderDataTable({
anova_data <- switch(input$anova_norm_check,
"No" = as.numeric(unlist(subset(st_data_subset(), select = input$bandwplot_data))),
"Yes" = as.numeric(unlist(subset(st_data_subset(), select = input$bandwplot_data)))/as.numeric(unlist(subset(st_data_subset(), select = input$anova_norm)))
)
anova_factor <- as.factor(unlist(subset(st_data_subset(), select = input$bandwplot_factor)))
anova_aov <- aov(anova_data ~ anova_factor)
anova_posthoc <- TukeyHSD(anova_aov)
anova_posthoc_data <- as.data.frame(anova_posthoc$anova_factor)
datatable(anova_posthoc_data) %>% formatStyle("p adj", backgroundColor = styleInterval(0.05, c("yellow", "white")))
})
})