public
Last active

First Shiny webapp - Lorentz equation

  • Download Gist
server.R
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
library(shiny)
library(deSolve)
library(ggplot2)
 
# Define server logic required to plot various variables
shinyServer(function(input, output) {
solveLorenz <- function(pars, times=tout) {
derivs <- function(t, state, pars) { # returns rate of change
with(as.list(c(state, pars)), {
dX <- A*X + Y*Z
dY <- B * (Y-Z)
dZ <- -X*Y +C*Y -Z
return(list(c(dX, dY, dZ)))
}
)
}
state <- c(X = 34.848292, Y = 14.331027983, Z = 14.327178391) # originally X = 1, Y = 1, Z = 1
## ode solves the model by integration...
return(ode(y = state, times = times, func = derivs, parms = pars))
}
output$guessPlot <- reactivePlot(function() {
tout<-seq(0, input$tmax, by = .01)
guess_pars<-c(A = input$A, B = input$B, C = input$C)
# alpha<-input$alpha
guess <- as.data.frame(solveLorenz(guess_pars, tout))
print(ggplot(as.data.frame(guess)) + geom_path(aes(X, Y, col=Z), alpha=input$alpha, lwd=.3))
# plot(guess$X, guess$Y)
})
})
ui.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
library(shiny)
 
# Define UI
shinyUI(pageWithSidebar(
# Application title
headerPanel("Test Lorentz's Equations"),
# Sidebar with controls
sidebarPanel(
numericInput("tmax", "T:", 100),
numericInput("alpha", "alpha:", 0.3),
numericInput("A", "A:", -8/3),
numericInput("B", "B:", -10),
numericInput("C", "C:", 28)
),
# Show the plot of the requested variable against mpg
mainPanel(
plotOutput("guessPlot")
)
))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.