|
| 1 | +learndown::learndownShinyVersion("1.0.0") |
| 2 | +conf <- BioDataScience::config() |
| 3 | + |
| 4 | +library(shiny) |
| 5 | +library(learndown) |
| 6 | +library(BioDataScience2) |
| 7 | + |
| 8 | +b_init <- 8 |
| 9 | +a_init <- 2 |
| 10 | +xmid_init <- 4 |
| 11 | +scal_init <- 0.5 |
| 12 | +error_sd <- 0.1 |
| 13 | +set.seed(42) |
| 14 | + |
| 15 | + |
| 16 | +model_data <- tibble::tibble( |
| 17 | + x = seq(0, 8, by = 0.1), |
| 18 | + y = SSfpl(x, A = a_init, B = b_init, xmid = xmid_init, scal = scal_init) + |
| 19 | + rnorm(n = length(x), sd = error_sd)) |
| 20 | + |
| 21 | +ui <- fluidPage( |
| 22 | + learndownShiny("Ajustement manuel d'un modèle : modèle logistique généralisé"), |
| 23 | + |
| 24 | + sidebarLayout( |
| 25 | + sidebarPanel( |
| 26 | + withMathJax(), |
| 27 | + p("$$y(x) = \\frac{A + (B-A) }{1 + e^{\\frac{xmid - x}{scal}}}$$"), |
| 28 | + |
| 29 | + sliderInput("a", label = "A : Asymptote horizontale basse", |
| 30 | + value = 1.00, min = 0.50, max = 10.00, step = 0.5), |
| 31 | + sliderInput("b", label = "B : Asymptote horizontale Haute", |
| 32 | + value = 1.00, min = 0.50, max = 10.00, step = 0.5), |
| 33 | + sliderInput("xmid", label = "Xmid", |
| 34 | + value = 1.00, min = 0.25, max = 10.00, step = 0.25), |
| 35 | + sliderInput("scal", label = "Scal", |
| 36 | + value = 1.00, min = 0.25, max = 10.00, step = 0.25), |
| 37 | + hr(), |
| 38 | + submitQuitButtons() |
| 39 | + ), |
| 40 | + |
| 41 | + mainPanel( |
| 42 | + plotOutput("model_plot"), |
| 43 | + |
| 44 | + hr(), |
| 45 | + |
| 46 | + withMathJax(), |
| 47 | + fluidRow( |
| 48 | + column(width = 6, |
| 49 | + p("Modèle paramétré :"), |
| 50 | + uiOutput("model_equation")), |
| 51 | + |
| 52 | + column(width = 6, |
| 53 | + p("Somme des carrés des résidus (valeur à minimiser) :"), |
| 54 | + uiOutput("model_resid")) |
| 55 | + ) |
| 56 | + ) |
| 57 | + ) |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +server <- function(input, output, session) { |
| 62 | + |
| 63 | + model_predict <- reactive({ |
| 64 | + dplyr::mutate(model_data, |
| 65 | + y_predit = SSfpl(x, A = input$a, B = input$b, |
| 66 | + xmid = input$xmid, scal = input$scal), |
| 67 | + distance2 = (y_predit - y)^2 |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + output$model_equation <- renderUI({ |
| 72 | + withMathJax( |
| 73 | + #sprintf("$$y(x) = \\frac{%.02f}{1 + e^{\\frac{%.02f - x}{%.02f}}}$$", input$asym, input$xmid, input$scal) |
| 74 | + sprintf("$$y(x) = \\frac{ %.02f + (%.02f - %.02f) }{1 + e^{\\frac{%.02f - x}{%.02f}}}$$", input$a, input$b, input$a,input$xmid, input$scal) |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + output$model_resid <- renderUI({ |
| 79 | + data <- model_predict() |
| 80 | + withMathJax(sprintf("$$ \\ %.02f \\ $$", sum(data$distance2))) |
| 81 | + }) |
| 82 | + |
| 83 | + output$model_plot <- renderPlot({ |
| 84 | + data <- model_predict() |
| 85 | + |
| 86 | + chart::chart(data, y ~ x) + |
| 87 | + ggplot2::geom_point() + |
| 88 | + ggplot2::geom_line(chart::f_aes(y_predit ~ x), color = "red") + |
| 89 | + ggplot2::xlab("x") + |
| 90 | + ggplot2::ylab("y") |
| 91 | + }) |
| 92 | + |
| 93 | + trackEvents(session, input, output, |
| 94 | + sign_in.fun = BioDataScience::sign_in, config = conf) |
| 95 | + trackSubmit(session, input, output, max_score = 4, solution = |
| 96 | + list(b = b_init, a = a_init, xmid = xmid_init, scal = scal_init), |
| 97 | + comment = "y(x) = (A+(B-A))/(1+e^((xmid-x)/scal))", |
| 98 | + message.success = "Correct, c'est le meilleur modèle.", |
| 99 | + message.error = "Incorrect, un modèle mieux ajusté existe.") |
| 100 | + trackQuit(session, input, output, delay = 20) |
| 101 | +} |
| 102 | + |
| 103 | +shinyApp(ui, server) |
0 commit comments