反应功能i r(有光泽)不起作用

时间:2016-08-23 12:55:05

标签: r shiny

我想在我的代码中实现以下功能:

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)
    })

并在此部分代码中使用此反应函数:

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)

    })

但是我的闪亮不起作用,它给我一个错误:

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.)

函数d()和params.pnbd()工作正常,应用程序没有&#39;校准&#39;功能也可以。我确信,问题出在&#39;校准&#39;功能,我错过了非常重要的事情。我如何解决这个问题,因为我不知道解决方案:我将我的函数放入reactive上下文。

1 个答案:

答案 0 :(得分:0)

您肯定是在第二个代码框中表示的是renderPlot()而不是plotOutput();)