-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClock_driven_gene.R
executable file
·79 lines (66 loc) · 2.48 KB
/
Clock_driven_gene.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
library("deSolve")
library(tidyr)
library(dplyr)
library(magrittr)
library(ggplot2)
library(gridExtra)
ccg <- function(t, y, p) {
with(as.list(c(y, p)), {
dX <- a + A*cos(2*pi*t/24) - d*X
list(X=dX)
})
}
## prepare data structures to create UI programmatically
y0 <- c(X=1)
parms <- c(a=2, A=0, d=0.5)
aspect <- 0.8
makelist <- function(i, obj, min=NA, max=NA, step=NA, width=NULL) {
list(inputId=names(obj[i]), label=names(obj[i]),
value=unname(obj[i]), min=min, max=max, step=step,
width=width)
}
## two lists of lists
L_parms <- lapply(1:length(parms), makelist, obj=parms, min=0, max=2, step=0.05, width=200)
L_y0 <- lapply(1:length(y0), makelist, obj=y0, min=0, max=3, step=0.1, width=200)
server <- function(input, output, session) {
output$ccg <- renderPlot({
L_input <- reactiveValuesToList(input) # to enable width
y0 <- with(L_input, c(X=X))
parms <- with(L_input, c(a=a, A=A, d=d))
times <- seq(0, 150, .1)
out <- ode(y0, times, ccg, parms)
df1 <- data.frame(out) %>%
mutate(input = with(L_input, a + A*cos(2*pi*time/24))) %>%
gather(var, value,-time) %>%
group_by(var) %>%
mutate(norm_value = value/mean(value[-(1:100)]))
f1 <- ggplot(df1, aes(x=time, y=value, color=var)) + geom_line(size=1) + theme_bw() + xlab("Time") +
ylab("Species") + theme(aspect.ratio = 1/4, legend.position = "top", legend.title = element_blank()) +
ggtitle("time course")
f2 <- ggplot(df1, aes(x=time, y=norm_value, color=var)) + geom_line(size=1) + theme_bw() + xlab("Time") +
ylab("Species") + theme(aspect.ratio = 1/4, legend.position = "top", legend.title = element_blank()) +
ggtitle("time course normalized by mean (excluding initial transient)") + ylim(c(0,2.5))
grid.arrange(f1, f2, ncol=1)
}, height = function() {
aspect * session$clientData$output_ccg_width
})
}
ui <- fluidPage(
headerPanel("Output of a clock-driven gene"),
withMathJax("$$\\frac{dX}{dt} = a + A \\cos \\left(\\frac{2\\pi t}{24}\\right) - dX$$"),
sidebarLayout(
sidebarPanel(
## generic creation of UI elements
h4("Initial values"),
lapply(L_y0, function(x) do.call("sliderInput", x)), # <--------
h4("Parameters"),
lapply(L_parms, function(x) do.call("sliderInput", x)), # <--------
width = 4
),
mainPanel(
h4("Simulation results"),
plotOutput("ccg")
)
)
)
shinyApp(ui = ui, server = server)