Skip to content

Instantly share code, notes, and snippets.

@ariliso
Created March 14, 2022 02:39
Show Gist options
  • Select an option

  • Save ariliso/747bf47b6578ad20a4167e9ab6a1ce75 to your computer and use it in GitHub Desktop.

Select an option

Save ariliso/747bf47b6578ad20a4167e9ab6a1ce75 to your computer and use it in GitHub Desktop.
Shiny Solve Diffeq
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("r",
"r",
min = 0,
max = 2,
value = 1.1,
step = 0.05),
sliderInput("a",
"a",
min = 0,
max = 1,
value = .6),
sliderInput("c",
"c",
min = 0,
max = 1,
value = .6),
sliderInput("e",
"e",
min = 0,
max = 1,
value = .5),
sliderInput("d",
"d",
min = 0,
max = 1,
value = .2),
sliderInput("K",
"K",
min = 0,
max = 100,
value = 80),
sliderInput("R_max",
"(x) Prey Axis Max:",
min=20,
max=200,
value=100
),
sliderInput("P_max",
"(y) Predator Axis Max:",
min=20,
max=200,
value=100
),
sliderInput("Row_len",
"Arrow Length",
min=0.5,
max=20,
value=2
),
sliderInput("Row_wid",
"Arrow width",
min=0.01,
max=2,
value=0.5
),
sliderInput("steps",
"steps",
min=1,
max=100,
value=100
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
mk_diffFn <-
function(r=1.1, a=0.6, c=0.6, e=0.5, d=0.2, K=80){
dR<-\(R,P) (r*R*(1-(R/K))-P*((c*R)/(a+R)))
dP<-\(R,P) (((e*c*R)/(a+R))*P-(d*P))
list_out <- list(
dydx = (\(R,P) dR(R,P)/dP(R,P)),
dx = dR,
dy = dP
)
return(list_out)
}
dif_fns <- mk_diffFn(
r = input$r,
a = input$a,
c = input$c,
e = input$e,
d = input$d,
K = input$K
)
secX <- seq(1,input$R_max,length.out = input$steps)
secY <- seq(1,input$P_max,length.out = input$steps)
dx_i <- outer(secX,secY,dif_fns[[2]])
dy_i <- outer(secX,secY,dif_fns[[3]])
mag_nitude <- sqrt(as.vector(dx_i)^2 + as.vector(dy_i)^2)
shrink_fac <- max(input$R_max,input$P_max) / input$steps / max(mag_nitude) * input$Row_len
x_i0 <- rep(secX, each=length(secY))
x_i1 <- x_i0 + (dx_i * shrink_fac)
y_i0 <- rep(secY, length(secX))
y_i1 <- y_i0 + (dy_i * shrink_fac)
plot(x_i0,y_i0)
arrows(
x_i0,y_i0,
x_i1,y_i1,
length = input$Row_wid,
col = mag_nitude
)
},height = 1000)
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment