0

I want to implement into my code the following function:

calibration_pnbd <- reactive({
        cal.cbs <- d()
        params <- params.pnbd()
        censor <- 5
        tryCatch(x <- cal.cbs[, "x"], error = function(e) stop("Error in pnbd.PlotFrequencyInCalibration: cal.cbs must have a frequency column labelled \"x\""))
        tryCatch(T.cal <- cal.cbs[, "T.cal"], error = function(e) stop("Error in pnbd.PlotFrequencyInCalibration: cal.cbs must have a column for length of time observed labelled \"T.cal\""))
        if (censor > max(x)) 
            stop("censor too big (> max freq) in PlotFrequencyInCalibration.")
        n.x <- rep(0, max(x) + 1)
        custs = nrow(cal.cbs)
        for (ii in unique(x)) {
            n.x[ii + 1] <- sum(ii == x)
        }
        n.x.censor <- sum(n.x[(censor + 1):length(n.x)])
        n.x.actual <- c(n.x[1:censor], n.x.censor)
        T.value.counts <- table(T.cal)
        T.values <- as.numeric(names(T.value.counts))
        n.T.values <- length(T.values)
        total.probability <- 0
        n.x.expected <- rep(0, length(n.x.actual))
        for (ii in 1:(censor)) {
            this.x.expected <- 0
            for (T.idx in 1:n.T.values) {
                T <- T.values[T.idx]
                if (T == 0) 
                    next
                n.T <- T.value.counts[T.idx]
                expected.given.x.and.T <- n.T * pnbd.pmf(params, 
                                                         T, ii - 1)
                this.x.expected <- this.x.expected + expected.given.x.and.T
                total.probability <- total.probability + expected.given.x.and.T/custs
            }
            n.x.expected[ii] <- this.x.expected
        }
        n.x.expected[censor + 1] <- custs * (1 - total.probability)
        col.names <- paste(rep("freq", length(censor + 1)), (0:censor), 
                           sep = ".")
        col.names[censor + 1] <- paste(col.names[censor + 1], "+", 
                                       sep = "")
        censored.freq.comparison <- rbind(n.x.actual, n.x.expected)
        colnames(censored.freq.comparison) <- col.names
        return(censored.freq.comparison)
    })

and to use this reactive function in this part of code:

output$a_e_cust_pnbd <- plotOutput({
        data <- calibration_pnbd()
        barplot(data, beside = TRUE, 
                main = "Frequency of Repeat Transactions", xlab ="Calibration period transactions", ylab = "Customers", col = 1:2)
        legend("topright", legend = c("Actual", "Model"), col = 1:2, 
               lwd = 2)

    })

But my shiny doesn't work and it throws me an error:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    48: .getReactiveEnvironment()$currentContext
    47: .dependents$register
    46: calibration_pnbd
    45: imageOutput [#252]
    44: plotOutput
    43: server [#251]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

the functions d() and params.pnbd() works fine and the app without the 'calibration' function works also ok. I am sure, that the problem is in 'calibration' function and i missed something very important. How can i fix this problem, because I don't know the solution: i put my function into the reactive context.

Daniel Yefimov
  • 860
  • 1
  • 10
  • 24
  • 1
    why `plotOutput`? may be you need `renderPlot` ? P.S its unreal to find problem try to minimaze your code – Batanichek Aug 23 '16 at 13:02
  • You should create a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The error message seems to be more about where you are calling these functions from and that's not clear from the incomplete code you've provided. – MrFlick Aug 23 '16 at 14:02
  • @Batanichek but you did it:)Thank you, that you helped me. And i appologize for such large code. – Daniel Yefimov Aug 23 '16 at 14:32

1 Answers1

0

You surely meant renderPlot() instead of plotOutput() in your second code box ;)

Elie Ker Arno
  • 346
  • 1
  • 11