Ошибка блестящего ползунка ввода "Операция не разрешена без активного реактивного контекста" - PullRequest
1 голос
/ 06 августа 2020

Я пытаюсь создать свое первое блестящее приложение, используя модель отсеков. Однако когда я запускаю свой код, я получаю сообщение об ошибке «Операция не разрешена без активного реактивного контекста». Я поместил реактивную ({}) вокруг своей функции ode, но все еще получаю ту же ошибку. Любая помощь была бы большой, спасибо.

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)


пропущенный код функции CCHFModelCombined

1 Ответ

2 голосов
/ 06 августа 2020

Попробуйте это

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)

вывод

...