18
votes

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.

2
Please help us help you by providing us with a reproducible example (i.e. code and example data), see stackoverflow.com/questions/5963269/… for details. - Paul Hiemstra
I really recommend updating to R 3.0.1. - Roland

2 Answers

59
votes

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.

Hope that helps.

2
votes

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