1

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

    )
  )
))
Ripon
  • 23
  • 5
  • From this SO answer (http://stackoverflow.com/questions/24935452/rstudio-shiny-not-able-to-use-ggvis) I got idea that you might need to put `uiOutput("AnPlot_ui")` somewhere in your `ui.R`... – Marat Talipov Jan 16 '15 at 03:46
  • @Marat, I forgot to include `uiOutput("AnPlot_ui")` in the code provided in the question. I have updated the code provided. – Ripon Jan 16 '15 at 03:49

1 Answers1

1

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

Marat Talipov
  • 13,064
  • 5
  • 34
  • 53