简体   繁体   中英

Reactive elements and ggvis

I am having a problem getting a ggvis graph to display using reactive elements. Here is the error I am getting: 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.)

I looked at other posts so I think I need to use observe({}) somewhere, but I am not sure where. I tried

observe({ df <- CreateDF(input$PMNT, input$Periods, input$Rate) )}

When I did that, the graph displayed, but when I changed the input values, the graph did not update.

Thanks for any insight you may be able to provide.

Here is the relevant code:

server.R:

library(ggvis)
library(dplyr)
source("functions.R")

shinyServer(function(input, output) {

  input_PMNT <- reactive(input$PMNT)
  input_Periods <- reactive(input$Periods)
  input_Rate <- reactive(input$Rate)

observe(
df <- CreateDF(input$PMNT, input$Periods, input$Rate)
)
df %>% ggvis(x = ~time, y = ~pv) %>% layer_bars(width=1, fill := "#fff8dc") %>%
  add_axis("x", title = "Period") %>%
  add_axis("y", title = "Value") %>% 
  bind_shiny("AnPlot", "AnPlot_ui")

})

ui.R:

library(shiny)
library(ggvis)
library(dplyr)

shinyUI(fluidPage(

  titlePanel("Annuity Calculator"),

  sidebarLayout(
    sidebarPanel(
       radioButtons("AnType", 
                    "Annuity Type:",
                    list("Level", "Geometric", "Arithmetic"),
                    selected="Level")
    ),
    mainPanel(
      numericInput("PMNT", "Enter the regular payment:", min=0, value=100),
      numericInput("Periods", "Enter the number of periods:", min=0, value=10),
      numericInput("Rate", "Enter the interest rate, as a decimal:", value=0.07),
      ggvisOutput("AnPlot"),
      uiOutput("AnPlot_ui")

    )
  )
))

The expression observe({ df <- CreateDF(input$PMNT, input$Periods, input$Rate) )} does not make much sense to me since df is visible only inside the observer, and observers don't return anything. Instead, you can try df <- reactive( CreateDF(input$PMNT, input$Periods, input$Rate) ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM