Skip to content

Commit 558766c

Browse files
add shiny app on exponential models
1 parent 43dde41 commit 558766c

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

inst/shiny/04b_exponent/server.R

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#
2+
# This is the server logic of a Shiny web application. You can run the
3+
# application by clicking 'Run App' above.
4+
#
5+
# Find out more about building applications with Shiny here:
6+
#
7+
# http://shiny.rstudio.com/
8+
#
9+
10+
library(shiny)
11+
SciViews::R()
12+
13+
# Define server logic required to draw a histogram
14+
shinyServer(function(input, output) {
15+
16+
output$exo_plot <- renderPlot({
17+
18+
set.seed(42)
19+
20+
exponent <- function(x, y0, k)
21+
y0 * exp(k * x)
22+
23+
24+
expo_exo <- tibble(
25+
time = seq(0, 2.5, by = 0.1),
26+
popu = exponent(time, y0 = 8, k = 1.25) + rnorm(n = length(time), sd = 4),
27+
popu_predit = exponent(time, y0 = input$y0_ui, k = input$k_ui)
28+
)
29+
30+
chart::chart(expo_exo, popu ~ time) +
31+
ggplot2::geom_point() +
32+
geom_line(f_aes(popu_predit ~ time), color = "red") +
33+
xlab("Temps") +
34+
ylab("Population")
35+
})
36+
37+
output$expo_model <- renderUI({
38+
withMathJax(
39+
sprintf("Ton modèle : $$population \\ = %.02f \\ e^{%.02f \\ temps}$$",
40+
input$y0_ui, input$k_ui))
41+
})
42+
43+
44+
output$expo_resid <- renderUI({
45+
set.seed(42)
46+
47+
set.seed(42)
48+
49+
exponent <- function(x, y0, k)
50+
y0 * exp(k * x)
51+
52+
53+
expo_exo <- tibble(
54+
time = seq(0, 2.5, by = 0.1),
55+
popu = exponent(time, y0 = 8, k = 1.25) + rnorm(n = length(time), sd = 4),
56+
popu_predit = exponent(time, y0 = input$y0_ui, k = input$k_ui),
57+
distance2 = (popu_predit - popu)^2
58+
)
59+
60+
value <- sum(expo_exo$distance2)
61+
62+
withMathJax(
63+
sprintf("La valeur de la somme des résidus au carré de ton modèle : $$%.05f$$",
64+
value))
65+
})
66+
67+
68+
output$expo_theo <- renderPlot({
69+
70+
exponent <- function(x, y0, k)
71+
y0 * exp(k * x)
72+
73+
exponent_data <- tibble(
74+
t = seq(0, 3, by = 0.1),
75+
y = exponent(t, y0 = 1.5, k = 0.9)
76+
)
77+
78+
chart(data = exponent_data, y ~ t) +
79+
geom_line() +
80+
geom_vline(xintercept = 0, col = "darkgray") +
81+
geom_hline(yintercept = 1.5, col = "gray", linetype = "dashed") +
82+
annotate("text", label = "y0", x = -0.05, y = 1.5)
83+
})
84+
85+
})

inst/shiny/04b_exponent/ui.R

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
library(shiny)
2+
3+
titlePanel_h4 <- function(title, windowTitle = title) {
4+
tagList(tags$head(tags$title(windowTitle)), h4(title))
5+
}
6+
7+
# Define UI for application that draws a histogram
8+
shinyUI(
9+
navbarPage(
10+
title = titlePanel_h4("Courbe exponentielle"),
11+
tabPanel(
12+
"Un peu de théorie",
13+
sidebarLayout(
14+
sidebarPanel(
15+
withMathJax(),
16+
h4("Le Contexte"),
17+
p("En 1798, Thomas Malthus a décrit un modèle de croissance avec une
18+
des caractéristiques les plus fondamentales de la croissance :
19+
son caractère exponentiel (positive ou négative)."),
20+
h4("L'équation mathématique :"),
21+
p("$$y(t) = y_0 \\ e^{k \\ t}$$"),
22+
h4("Eléments importants :"),
23+
p("- une croissance exponentielle"),
24+
hr(),
25+
h4("La fonction dans R : "),
26+
p("$$exponent(x, y0, k)$$"),
27+
p("Note: la fonction exponent() n'est pas implémenté dans R.
28+
Vous devez l'écrire vous même $$exponent <- function(x, y0, k)$$
29+
$$y0 * exp(k * x)$$"),
30+
h5("Arguments de la fonction : "),
31+
p("x: vecteur de nombre représentant le temps"),
32+
p("y0 : la taille initiale de la population au temps t0"),
33+
p("k : est le taux de croissance"),
34+
width = 5
35+
),
36+
mainPanel(
37+
p("Le graphique ci-dessous représente un modèle de croissance
38+
exponentiel avec y0 = 1.5 et k = 0.9 "),
39+
plotOutput("expo_theo"),
40+
width = 7
41+
)
42+
)
43+
),
44+
tabPanel(
45+
title = "A toi de jouer !",
46+
sidebarLayout(
47+
sidebarPanel(
48+
withMathJax(),
49+
p("La courbe exponentielle permet de modéliser des croissances
50+
exponentielles. ce modèle n’est plus guère utilisé
51+
actuellement, mais son importance historique ne doit pas
52+
être négligée"),
53+
p("L'équation mathématique de la fonction est la
54+
suivante: $$y(t) = y_0 \\ e^{k \\ t}$$"),
55+
numericInput(inputId = "y0_ui", label = "Valeur de y0",
56+
value = 1.00, min = 0.50, max = 10.00, step = 0.5),
57+
p("Valeur par défaut : 1 "),
58+
numericInput(inputId = "k_ui", label = "Valeur de k",
59+
value = 1.00, min = 0.50, max = 5.00, step = 0.25),
60+
p("Valeur par défaut : 1 ")
61+
),
62+
mainPanel(
63+
h4("Ajustez le meilleur modèle exponentiel"),
64+
p("Vous devez ajuster votre modèle en faisant varier les
65+
paramètres du modèle."),
66+
plotOutput("exo_plot"),
67+
hr(),
68+
withMathJax(),
69+
uiOutput("expo_model"),
70+
hr(),
71+
uiOutput("expo_resid"),
72+
hr()
73+
)
74+
)
75+
)
76+
)
77+
)

0 commit comments

Comments
 (0)