how can I build a reactive app for choosing the starting values for exponential fit equation?

85 Views Asked by At

model equation:

y = H0_1 * (1 - exp(- (Tmax / beta) ^ theta)) + c

Hello, What I'm trying to do is plot an exponential best-fit curve to the data. I'm trying to make my first shiny app with a slider input for a number of bins to get the starting values. However, when I run the code it doesn't show any graph. The code:

x <- Tmax <- 443, 454, 451, 438, 451, 452, 453, 454, 453, 445, 449, 449, 454
y <- HI <- 43, 62, 63, 95, 105, 117.51, 119.07, 122, 122, 125, 131.8, 137, 139

#save this script as app.R
library(shiny)
library(ggplot2)

ui <- fluidPage(

Application title
titlePanel("Exponential Equation App"),

Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "H0_1",label = "H0_1:",
min = 100,max = 1000,value = 100, step = 0.5),
sliderInput(inputId = "beta",label = "beta",
min = 300,max = 600,value = 300, step = 0.5),
sliderInput(inputId = "theta",label = "theta",
min = -200,max = -1,value = -5, step = 1),
sliderInput(inputId = "c",label = "c",
min = 0,max = 100,value = 0, step = 1)

    ),


mainPanel(
plotOutput("lineplot")
    )
  )
)


server <- function(input, output) {

output$lineplot <- renderPlot({
x <- seq(from = 427, to = 458, by = 1)
y <-  H0_1 * (1 - exp(- (Tmax / beta) ^ theta)) + c 

plot(x,y, col="red", lwd = 3, type = "l")
lines(y~For_RStudio$Tmax, col="blue", lwd=3)
legend("topleft",c("real data","constructed"), 
col=c("blue","red"), lwd=3)


  })
}
shinyApp(ui = ui, server = server)
1

There are 1 best solutions below

7
uke On

it seems to me that you are not using any of your defined slider inputs in your server function. Try using them with input$H0_1 instead of just writing H0_1.

This means, this line in your server logic

y <-  H0_1 * (1 - exp(- (Tmax / beta) ^ theta)) + c 

should be

y <-  input$H0_1 * (1 - exp(- (Tmax / input$beta) ^ input$theta)) + input$c 

Edit 1

Here is an app that is at least running. I am clueless of the goal that your app should achieve, aka I don't know how you choose the start values for exp. fit equation. But from the technical side, this is a running app and you can modify it in a way, that makes it work.

what I did:

  • created distinction between x_real, y_real for observed data and x_constr, y_constr for constructed data.
  • moved the definition of the real data inside the server function, but not inside the plot output reactive context.
  • put the constructed data inside the reactive plot output context, as it should be changing when input changes - at least I am assuming this.
  • added spaces after comma , and around = for legibility and because it is good tone
library(shiny)
library(ggplot2)

ui <- fluidPage(
  
  titlePanel("Exponential Equation App"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "H0_1",label = "H0_1:",
                  min = 100,max = 1000,value = 100, step = 0.5),
      sliderInput(inputId = "beta",label = "beta",
                  min = 300,max = 600,value = 300, step = 0.5),
      sliderInput(inputId = "theta",label = "theta",
                  min = -200,max = -1,value = -5, step = 1),
      sliderInput(inputId = "c",label = "c",
                  min = 0,max = 100,value = 0, step = 1)
    ),
    mainPanel(
      plotOutput("lineplot")
    )
  )
)


server <- function(input, output) {
  
  x_real <- c(443, 454, 451, 438, 451, 452, 453, 454, 453, 445, 449, 449, 454)
  y_real <- c(43, 62, 63, 95, 105, 117.51, 119.07, 122, 122, 125, 131.8, 137, 139)
  
  output$lineplot <- renderPlot({
    x_constr <- seq(from = 427, to = 458, by = 1)
    y_constr <-  input$H0_1 * (1 - exp(- (x_constr / input$beta) ^ input$theta)) + input$c 
    #y_real <-  input$H0_1 * (1 - exp(- (x_real / input$beta) ^ input$theta)) + input$c 
    
    plot(x_constr, y_constr, col = "red", lwd = 3, type = "l")
    lines(y_real ~ x_real, col = "blue", lwd = 3)
    legend("topleft", c("real data", "constructed"),
           col = c("blue", "red"), lwd = 3)
    
    
  })
}
shinyApp(ui = ui, server = server)