Shiny input slider error "Operation not allowed without an active reactive context"

Nicole

I am trying to make my first shiny application using a compartmental model. However when I run my code I get an error that says "Operation not allowed without an active reactive context". I have put reactive({}) around my ode function and I am still getting the same error. Any help would be great thank you.

library(deSolve)
library(plotly)
library(tidyverse)
library(scales)
library(shiny)
library(ggplot2)
library(gganimate)

CCHFModelCombined = function(t,x,params)
{
  # get SIR values
  SH <- x[1]
  EH <- x[2]
  IH <- x[3]
  RH <- x[4]
  ST <- x[5]
  ET <- x[6]
  IT <- x[7]
  SC <- x[8]
  EC <- x[9]
  IC <- x[10]
  RC <- x[11]
  
  # params----
  # January - march
  if(t <= 91){
    params <- c(betaHH = 0.000000001,  # ticks are not active
                betaTH = 0.000000001, 
                betaCH = 0.0000000001, 
                betaTC = 0.0000000001, 
                betaCT = 0.000001,
                betaTTV = 0.0000000001, 
                betaTTH = 1/36500000,
                gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                muH = (1/(365 * 79)), 
                muT = (25/(365* 2)), # increased tick mortality, live 14.6 days
                muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                piT = 1/36000000,  # ticks are not giving birth
                piC = 7/(8 * 365), # sheep produce 7 babies in their life (give birth Winter - Spring)
                deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
                deltaT = 1/1.5, 
                deltaC = 1/2,
                alpha = 1/17, # recovery after 15 days
                alpha2 = 1/7) }
  # April
  else if( t <= 121) {
    params <-c(betaHH = 0.00000001, 
               betaTH = 0.00000001, 
               betaCH = 0.00000001, 
               betaTC = (1/365), # One tick attaches to one carrier per year
               betaCT = 59/365, # One cattle infects 59 ticks per year (assuming 60 ticks on cattle)
               betaTTV = 0.0001, 
               betaTTH = 1/365, 
               gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
               muH = (1/(365 * 79)), 
               muT = (3/(365* 2)) + 0.0035,
               muC = (1/(8 * 365)), #sheep/deer live 6-11 years
               piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
               piT =  0.00001,  
               piC = 7/(8 * 365), # sheep produce 7 babies in their life
               deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
               deltaT = 1/1.5, 
               deltaC = 1/2,
               alpha = 1/17, # recovery after 15 days
               alpha2 = 1/7) }
  # May - June
  else if(t <= 182) {
    params <-c(betaHH = .0000022,  
               betaTH = .000018, 
               betaCH = .0000045, 
               betaTC = (1/365), # One tick attaches to one carrier per year
               betaCT = 59/365, # One cattle infects 59 ticks per year (assuming 60 ticks on cattle)
               betaTTV = 0.0001, # ticks not giving birth
               betaTTH = 59/365,
               gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
               muH = (1/(365 * 79)), 
               muT = (1/(365* 2)) + 0.0035,
               muC = (1/(8 * 365)), #sheep/deer live 6-11 years
               piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
               piT =  0.00001, # ticks not giving birth
               piC = 3/(8 * 365), # sheep produce 7 babies in their life
               deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
               deltaT = 1/1.5, 
               deltaC = 1/2,
               alpha = 1/17, # recovery after 15 days
               alpha2 = 1/7) }
  #July - August
  else if(t <= 244) {
    params <-c(betaHH = .0000022,  
               betaTH = .000018, 
               betaCH = .0000045, 
               betaTC = (1/365), # One tick attaches to one carrier per year
               betaCT = 59/365, # One cattle infects 59 ticks per year (assuming 60 ticks on cattle)
               betaTTV = 0.0001, # ticks not giving birth
               betaTTH = 59/365,
               gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
               muH = (1/(365 * 79)), 
               muT = (1/(365* 2)) + 0.0035,
               muC = (1/(8 * 365)), #sheep/deer live 6-11 years
               piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
               piT =  1 /3650000, # ticks not giving birth 
               piC = .5/(8 * 365), # sheep give birth winter - summer
               deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
               deltaT = 1/1.5, 
               deltaC = 1/2,
               alpha = 1/17, # recovery after 15 days
               alpha2 = 1/7) }
  #September - Mid November
  else if(t <= 325) {
    params <-c(betaHH = .0000005,  
               betaTH = .0000025, 
               betaCH = .000001, 
               betaTC = (1/365), # One tick attaches to one carrier per year
               betaCT = 1/365, 
               betaTTV = (((7000/(365 * 2)) * .44)/30) * .04, # ticks giving birth 4% of ticks get CCHFV
               betaTTH = 59/365,
               gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
               muH = (1/(365 * 79)), 
               muT = (1/(365* 2)) + 0.092, # more deaths due to mothers death after giving birth
               muC = (1/(8 * 365)), #sheep/deer live 6-11 years
               piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
               piT =  ((7000/(365 * 2)) * .44)/30, # 4% of eggs survive, this is when ticks give birth, 44% of female adult ticks, 20-40 days to hatch
               piC = .5/(8 * 365), # sheep give birth winter - summer
               deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
               deltaT = 1/1.5, 
               deltaC = 1/2,
               alpha = 1/17, # recovery after 15 days
               alpha2 = 1/7) }
  # Mid November to December
  else if( t <= 365)
  {
    params <- c(betaHH = 0.000000001,  # ticks not active
                betaTH = 0.000000001, 
                betaCH = 0.0000000001, 
                betaTC = 0.0000000001, 
                betaCT = 0.000001,
                betaTTV = 0.000000001, 
                betaTTH = 1/36500000,
                gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                muH = (1/(365 * 79)), 
                muT = (1/(365* 2) + .0035),
                muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                piT = 1/36000000, 
                piC = 1/(8 * 365), # sheep produce 7 babies in their life
                deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
                deltaT = 1/1.5, 
                deltaC = 1/2,
                alpha = 1/17, # recovery after 15 days
                alpha2 = 1/7) }
  # Load values ----
  
  # Beta values
  betaHH = params["betaHH"]
  betaTH = params["betaTH"]
  betaCH = params["betaCH"]
  betaTC = params["betaTC"]
  betaCT = params["betaCT"]
  betaTTV = params["betaTTV"] # vertical transmission
  betaTTH = params["betaTTH"]
  
  # Gamma value
  gamma = params["gamma"]
  
  # death rates
  muH = params["muH"]
  muT = params["muT"]
  muC = params["muC"]
  
  # birth rates
  piH  = params["piH"]
  piT = params["piT"]
  piC = params["piC"]
  
  # incubation
  deltaH1 = params["deltaH1"]
  deltaT = params["deltaT"]
  deltaC = params["deltaC"]
  
  # recovery rate
  alpha = params["alpha"]
  alpha2 = params["alpha2"]
  
  # total population
  NH = (SH + IH + EH + RH) + (piH * SH) - (muH * SH)
  NT = (ST + IT + ET) +  (piT * ST) - (muT * ST)
  NC = (SC + IC + EC + RC) +  (piC * SC) - (muH * SC)
  
  # tick carrying Capacity 
  # KT = NC * 130 # 130 ticks per carrier max
  
  #computations ----
  
  dSHdt <- (piH * NH) - (betaHH * (SH/NH) * IH) - (betaCH * (SH/NH) * IC) - (betaTH * (SH/NH)* IT) - (muH * SH)
  dEHdt <- (betaHH * (SH/NH) * IH) + (betaCH * SH/NH * IC) + (betaTH * (SH/NH) * IT) - ((deltaH1 + muH)*EH)
  dIHdt <- (deltaH1 * EH) - ((alpha + gamma + muH)* IH)
  dRHdt <- ((alpha + gamma)* IH) - (muH * RH)
  dSTdt <- (piT * NT) - (betaTTV * (ST/NT) * IT) - (betaCT * (ST/NT) * IC) - (betaTTH * (ST/NT) * IT) - (muT * ST)
  dETdt <- (betaTTV * (ST/NT) * IT) + (betaCT * (ST/NT) * IC) + (betaTTH * (ST/NT) * IT) - ((deltaT + muT)* ET)
  dITdt <- (deltaT * ET) - (muT * IT)
  dSCdt <- (piC * NC) - (betaTC * (SC/NC) * IT) - (muC * SC)
  dECdt <- (betaTC * (SC/NC) * IT) - ((deltaC + muC)* EC)
  dICdt <- (deltaC * EC) - (muC * IC)
  dRCdt <- (alpha2 * IC) - (muC * RC)
  
  
  # return results
  list(c(dSHdt, dEHdt, dIHdt, dRHdt, dSTdt, dETdt, dITdt, dSCdt, dECdt, dICdt, dRCdt))
}


ui <- fluidPage(titlePanel("Effects of Tick to Carrier Interaction on Human CCHF Cases Per Year"),sliderInput("betaTC","Tick to Carrier Contact", min=0, max=2, step=0.1, value=0),plotOutput("plotIH"))

server <- function(input, output, session){
  # time to start solution 
  timeCombined =  seq(from = 0, to = 365, by = 0.1)
  
  #initialize initial conditions
  initialXCombined =  c(SH = 82000, EH = 0, IH = 1, RH = 0, ST = 870000, ET = 0, IT = 107010, SC = 145000, EC = 0, IC = 35, RC = 0)
  
  
  defaultParams <-  observeEvent(c(betaHH = .0000022,  
                    betaTH = .000018, 
                    betaCH = .0000045, 
                    betaTC = input$betaTC, # One tick attaches to one carrier per year
                    betaCT = 59/365, # One cattle infects 59 ticks per year (assuming 60 ticks on cattle)
                    betaTTV = 0.0001, # ticks not giving birth
                    betaTTH = 59/365,
                    gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                    muH = (1/(365 * 79)), 
                    muT = (1/(365* 2)) + 0.0035,
                    muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                    piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                    piT =  0.00001, # ticks not giving birth
                    piC = 3/(8 * 365), # sheep produce 7 babies in their life
                    deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
                    deltaT = 1/1.5, 
                    deltaC = 1/2,
                    alpha = 1/17, # recovery after 15 days
                    alpha2 = 1/7))
  
  
  dataSetCombined <- reactive ({ df(ode(y = initialXCombined, times = timeCombined, func = CCHFModelCombined, parms = defaultParms,
                         method = "ode45")) 
  })

  output$plotIH <- renderPlot({
   ggplot(dataSetCombined(), aes(x=time, y = IH)) + geom_line(color = '#00CED1') + ggtitle("Crimean-Congo haemorrhagic fever") + scale_x_continuous(name = "Time(days)") +
    scale_y_continuous(name = "Infected Humans") 
    })
}

shinyApp(ui = ui, server = server)


omitted code of CCHFModelCombined function

YBS

Try this

ui <- fluidPage(titlePanel("Effects of Tick to Carrier Interaction on Human CCHF Cases Per Year"),
                sliderInput("betaTC","Tick to Carrier Contact", min=0, max=2, step=0.1, value=0),
                #DT::dataTableOutput("data"),
                plotOutput("plotIH"))

server <- function(input, output, session){
  # time to start solution
  timeCombined =  seq(from = 0, to = 365, by = 0.1)

  #initialize initial conditions
  initialXCombined =  c(SH = 82000, EH = 0, IH = 1, RH = 0, ST = 870000, ET = 0, IT = 107010, SC = 145000, EC = 0, IC = 35, RC = 0)

  defaultParams <-  eventReactive(input$betaTC, {
    req(input$betaTC)
    params <-  c(betaHH = .0000022,
                 betaTH = .000018,
                 betaCH = .0000045,
                 betaTC = input$betaTC, # One tick attaches to one carrier per year
                 betaCT = 59/365, # One cattle infects 59 ticks per year (assuming 60 ticks on cattle)
                 betaTTV = 0.0001, # ticks not giving birth
                 betaTTH = 59/365,
                 gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                 muH = (1/(365 * 79)),
                 muT = (1/(365* 2)) + 0.0035,
                 muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                 piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                 piT =  0.00001, # ticks not giving birth
                 piC = 3/(8 * 365), # sheep produce 7 babies in their life
                 deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact
                 deltaT = 1/1.5,
                 deltaC = 1/2,
                 alpha = 1/17, # recovery after 15 days
                 alpha2 = 1/7)
    params
  })


  dataSetCombined <- eventReactive(defaultParams(),{ 
    ode(y = initialXCombined,
        times = timeCombined, 
        func = CCHFModelCombined, 
        parms = defaultParams(),
        method = "ode45"
    ) %>%       
      as.data.frame() -> out
  })
  
  output$data <- DT::renderDataTable({
    dataSetCombined()
  })

  output$plotIH <- renderPlot({
    ggplot(dataSetCombined(), aes(x=time, y = IH)) +
      geom_line(color = '#00CED1') +
      ggtitle("Crimean-Congo haemorrhagic fever") +
      scale_x_continuous(name = "Time(days)") +
      scale_y_continuous(name = "Infected Humans")
  })
}

shinyApp(ui = ui, server = server)

output

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error in Running R Shiny App: Operation not allowed without an active reactive context

r shiny slider input round

custom values in slider input shiny

Shiny Slider Input step by month

Slider input whit hour shiny

How to add conditional slider input in shiny app?

File Input Error in Shiny App

How to reset the slider input when it completes its task in shiny context

R shiny - Combine the slider bar with a text input to make the slider bar more user-friendly

Shiny: Reactive slider produces error in plot: "invalid 'xlim' value"

Shiny range slider- error when using filter in ggplot

R Shiny error: object input not found

Shiny Reactive Input Value - No Graph Shown, No Error

Shiny slider on logarithmic scale

Shiny Slider Customized Values

Get a linear date Shiny slider input to "stick" on only specified irregular dates

How to set an initial value for two dependent input values (Slider and Numeric) in shiny?

R Shiny: How to perform calculation based on values from a CSV and user input (slider)?

How do I animate my R Shiny plot's output based on the increments of slider input value?

Unexpected input error in Shiny, but unable to locate the source of error

Shiny R histogram not changing with slider

shiny slider with button for increment and decrement

Error when trying to place input$something in front of "=" sign in a shiny app

Trying to create a Shiny app. Getting an error specifiying a formula input

Select Input in ggplot Shiny dashboard - Error: object not found

Bubble as slider in input range

2 Slider Input Combination

Styling issues with a input slider

connect an input text to a slider