-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
138 lines (107 loc) · 3.71 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
132
133
134
135
136
137
138
# Check for availability of required packages, install if not available
list.of.packages <- c("shiny", "ggplot2")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#initialize
library(shiny)
library(ggplot2)
# UI for app
ui<-(pageWithSidebar(
# title
headerPanel("Select Options"),
#input
sidebarPanel
(
# Input: Select a file ----
selectInput("experiment",
label = "Choose an experiment",
choices = c("ips_c9_sorted",
"ips_c9_mixed",
"mouse_m337v",
"ips_tdp",
"ips_sma23",
"ips_sma1"),
selected = "ips_c9_mixed"),
selectInput("gene",
label = "Choose a gene",
choices = NULL),
selectInput("contrast",
label = "Choose a contrast",
choices = NULL),
# Horizontal line ----
tags$hr(),
),
# output
mainPanel(
#h3(htmlOutput("caption")),
uiOutput("plot"), # depends on input
h3(textOutput("caption"))
)
))
# shiny server side code for each call
server<-(function(input, output, session){
output$plot <- renderUI({
plotOutput("p")
})
#plotting function using ggplot2
output$p <- renderPlot({
results <- results[!is.na(results$symbol),]
ensemblgene <- results[results$symbol == input$gene, 1]
dftemp <- as.data.frame(t(tpm[which(tpm[,1]==ensemblgene),-1]), rownames = "X")
colnames(dftemp) <-"value"
rownames(design) <- design$track
dftemp$group <- design[rownames(dftemp),][,input$contrast]
p<- ggplot(dftemp, aes(x = group, y = value, color = group)) +
geom_boxplot(outlier.shape = NA) + theme_bw() +
geom_point(aes(color=group), alpha = 0.5, position = position_jitter(w = 0.2, h = 0)) +
labs(color = input$contrast) + ggtitle(input$gene) +
ylab("TPM") + xlab (input$contrast)
print(p)
})
output$caption<-renderText({
results <- results[!is.na(results$symbol),]
padj <- signif(results[results$symbol == input$gene, "padj"],digits = 3)
if (is.na(padj)){
paste0(input$gene," was not tested in the model (abundance likely too low) (p =", padj,")")
} else if (padj <.05){
paste0(input$gene," is differentially expressed (p =", padj,")")
} else {
paste0(input$gene," is not differentially expressed (p =", padj,")")
}
})
# set uploaded file
upload_design<- reactive({
design <- paste0("./data/",input$experiment, "/design.tsv")
if (is.null(design))
return(NULL)
#could also store in a reactiveValues
read.csv(design,sep = "\t",header = TRUE)
})
upload_tpm<- reactive({
tpm <- paste0("./data/",input$experiment, "/tpm.tsv")
if (is.null(tpm))
return(NULL)
#could also store in a reactiveValues
read.csv(tpm,sep = "\t",header = TRUE, stringsAsFactors = FALSE, check.names = FALSE)
})
upload_results<- reactive({
results <- paste0("./data/",input$experiment, "/results.tsv")
if (is.null(results))
return(NULL)
#could also store in a reactiveValues
read.csv(results,sep = "\t",header = TRUE, stringsAsFactors = FALSE, check.names = FALSE)
})
observeEvent(input$experiment,{
design<<-upload_design()
updateSelectInput(session, "contrast", choices = colnames(design))
})
observeEvent(input$experiment,{
results<<-upload_results()
updateSelectInput(session, "gene", choices = results$symbol)
})
observeEvent(input$experiment,{
tpm<<-upload_tpm()
})
})
# Create Shiny app ----
shinyApp(ui, server)