Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Tutorial Error in R

Tags:

r

shiny

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.

like image 582
Ford Avatar asked Jun 08 '13 17:06

Ford


People also ask

Is R Shiny difficult to learn?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.

Do you need R to run Shiny app?

But the simplest way to run a Shiny app is to run it locally. You only need the shiny R package installed, and you can run the app in your browser.

Is R Shiny easy?

Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.


2 Answers

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.

like image 79
Ram Narasimhan Avatar answered Oct 13 '22 11:10

Ram Narasimhan


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})
  }
)
like image 28
Joe Avatar answered Oct 13 '22 11:10

Joe