-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobweb.R
91 lines (78 loc) · 2.37 KB
/
cobweb.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
library(shiny)
library(RColorBrewer)
default_next_p_str <- 'p + 1.8*p*(1-p/10)'
next_p_fun <- function(next_p_str=default_next_p_str) {
next_p <- parse(text=next_p_str)
function(p) {
eval(next_p)
}
}
# Define UI for application
ui <- basicPage(
pageWithSidebar(
headerPanel("Cobweb plot for one-pop difference equation model"),
sidebarPanel(
h4("Population model"),
helpText("Enter a formula defining the population model, using \"p\"
to denote the size of the population"),
textInput('next_p_str', "next_p = ", default_next_p_str),
actionButton('updateButton', "Update model"),
h4("Graph parameters"),
sliderInput('plimit', label = "Population range to show on graph",
min = 0, max = 100, value = c(0, 20))
),
mainPanel(
plotOutput('cobweb_plot', click = 'plot_click')
)
)
)
# Define server logic
server <- function(input, output) {
data <- reactiveValues(
next_p = next_p_fun(),
init_p = NULL
)
observeEvent(input$updateButton, {
data$next_p <- next_p_fun(input$next_p_str)
})
observeEvent(input$plimit, {
})
observeEvent(input$plot_click, {
data$init_p <- input$plot_click$x
})
output$cobweb_plot <- renderPlot({
plot.new()
plot.window(xlim = input$plimit, ylim = input$plimit,
xaxs = 'i', yaxs = 'i')
box()
axis(1, col.axis = 'grey30',
at=seq(input$plimit[1], input$plimit[2], length.out=11))
axis(2, col.axis = 'grey30',
at=seq(input$plimit[1], input$plimit[2], length.out=11))
grid(10, 10)
title(col.main = 'green4', col.sub = 'green4',
xlab = expression('P'['t']),
ylab = expression('P'['t'+1]),
col.lab = 'blue', font.lab = 3)
# plot the identity line
abline(a = 0, b = 1, col = 'red')
# plot the difference equation
x <- seq(input$plimit[1], input$plimit[2], length.out=50)
y <- sapply(x, data$next_p)
lines(x, y, col = 'blue')
# plot the cobweb
if(!is.null(data$init_p)) {
p2 <- data$init_p
for(i in 1:50) {
p1 <- p2
p2 <- data$next_p(p1)
lines(c(p1, p1), c(p1, p2)) # vertical cobweb step
if(p2 < 0) { break }
lines(c(p1, p2), c(p2, p2)) # horizontal cobweb step
}
}
},
height=600)
}
# Run the application
shinyApp(ui = ui, server = server)