1
votes

I'm trying to write a simple Shiny app that plots an exponential decay function where the user can input different values of lambda. Every variation I've tried results in "Error in rep(value, length.out = nrows) : attempt to replicate an object of type 'closure'." I've tried taking the advice from this thread, but haven't been able to resolve my issue.

library(shiny)
decay <- data.frame(days= seq(100, 0)) 

ui <- fluidPage( 
  sliderInput(inputId = "lambda",
              label = "Choose a number",
              value = 0.0, min = 0.0, max = 0.2),
  plotOutput("lplot")
)

server <- function(input, output){
  decay[[2]] <- reactive({
    exp(-input$lambda*decay[[1]])
  })
  output$lplot <- renderPlot({
    plot(decay())
  })

}

shinyApp(ui = ui, server = server)

I'm brand new to Shiny, so I could be overlooking something pretty basic. Thanks for your help.

1
What is decay[[1]] supposed to give?SymbolixAU

1 Answers

1
votes

The line

decay[[2]] <- reactive

is trying to overwrite the 2nd element of your data.frame decay. You probably don't mean to do this.

Also, I'm not sure what you want from

decay[[1]]

I suspect you want to use all the days from the decay data.frame?

In which case you should change your structure to something like

library(shiny)
decay <- data.frame(days= seq(100, 0)) 

ui <- fluidPage( 
  sliderInput(inputId = "lambda",
              label = "Choose a number",
              value = 0.0, min = 0.0, max = 0.2),
  plotOutput("lplot")
)

server <- function(input, output){

  r_decay <- reactive({
    exp(-input$lambda*decay$days)
  })
  output$lplot <- renderPlot({
    plot(r_decay())
  })

}

shinyApp(ui = ui, server = server)

enter image description here