18

I've seen some cool uses of shiny with R to make web applications and wanted to try to learn how to use it myself. I am doing the tutorial right now, but when I get to the Inputs and Outputs part of the tutorial (http://rstudio.github.io/shiny/tutorial/#inputs-and-outputs) I run in to a problem.

Specifically, I am getting an error that says:

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

I've tried a bunch of different things and searched everywhere online but can't figure out what the problem is. I am running R version 2.15.2 on OS X Version 10.8.3. My default browser is Chrome.

Thanks for the help.

Ford
  • 2,439
  • 4
  • 17
  • 15
  • 3
    Please help us help you by providing us with a reproducible example (i.e. code and example data), see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details. – Paul Hiemstra Jun 08 '13 at 17:50
  • 1
    I really recommend updating to R 3.0.1. – Roland Jun 08 '13 at 17:56

2 Answers2

63

I know this question is a bit dated, but responding for those who might come searching when faced with the same error message.

Since you haven't included your code, let's look at why this error message happens in general.

When the error message says "Operation not allowed without an active reactive context." what it is saying is that you are accessing a "reactive" element inside the ShinyServer function, but outside any of the reactive functions such as renderTable or renderPlot() etc.

This won't work inside ShinyServer()

shinyServer(function(input, output) {
    abc <- input$some.input.option   

  #other reactives here

})

Fix: Wrap it inside a reactive

This will work:

shinyServer(function(input, output) {
  abc <- reactive({
   abc <- input$some.input.option    
  })

  #other reactives here

})

And now, from inside the ShinyServer function, you can access that Input parameter by calling abc() Note the parenthesis since it is a reactive function.

starball
  • 20,030
  • 7
  • 43
  • 238
Ram Narasimhan
  • 22,341
  • 5
  • 49
  • 55
  • Thanks, this helps, but can you define "reactive element"? – pdb Jul 11 '17 at 22:10
  • 4
    Could you elaborate on why you've repeated the use of the name `abc`? – MichaelChirico Feb 10 '18 at 18:56
  • 1
    @MichaelChirico (and anyone else wondering): The name `abc` is repeated simply for clarity's sake. The reactive object `abc()`, accessible throughout the server function, contains the same data as object `abc` within the `reactive({})` function. Giving both the same name makes that visually immediately clear. – D3SL Oct 20 '20 at 08:02
2

For me, I had this issue when I forgot about using renderPrint, which is easy to forget when you're just starting up.

For example:

shinyServer(function(input,output) {
  output$outputString <- input$something
  }
)

When what I really needed to do was

shinyServer(function(input,output) {
  output$outputString <- renderPrint({input$something})
  }
)
Joe
  • 62,789
  • 6
  • 49
  • 67