title: Reactivity in R shiny with toy example
body: <p>I am altering this example (<a href="https://gist.github.com/wch/5436415/" rel="nofollow">https://gist.github.com/wch/5436415/</a>). Below is a toy model. Bascially I need the selectInput to be reactive and each time the selectInput is changed the value selected is passes to a function in global.r. Then I need to be able to use the result.</p>
  
  <p>Basically:
  (1) When the app first appears there should be 1 plot</p>
  
  <p>(2) When the user changes the slider input there is a reactive "input$n" in the max_plots function in server.r. This input$n is passed to the "NumberOfPlots" function in global.r</p>
  
  <p>(3) "numberOfPlots" will return a number. Ex. If the user changes the select input to 5. 5 gets passed to "NumberOfPlots" and 5 is returned from "NumberOfPlots"</p>
  
  <p>(4) now that the user made his selection I am using "maxplots()" in the shiny server function to access the number of plots</p>
  
  <p>I am getting the error:</p>
  
  <pre><code>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.)
  </code></pre>
  
  <p>Below is my CODE which you can run if you create an "runshiny.r" file and call the 4 lines below:</p>
  
  <pre><code>install.packages("shiny")
  library(rJava)
  library(shiny)
  runApp("C://Users/me/multi")#change this to the path of your correct folder
  </code></pre>
  
  <p>Here is my server.r</p>
  
  <pre><code>shinyServer(function(input, output,session) {
  
  #get the number of plots from reactive input and pass to global function
  max_plots&lt;- reactive({
    print("IN reactive function")
    NumberOfPlots(input$n)
  })
  
  
    # Insert the right number of plot output objects into the web page
    output$plots &lt;- renderUI({
      #plot_output_list &lt;- lapply(1:input$n, function(i) {
      plot_output_list &lt;- lapply(1:max_plots(), function(i) {
        plotname &lt;- paste("plot", i, sep="")
        plotOutput(plotname, height = 280, width = 700)
      })
      # Convert the list to a tagList - this is necessary for the list of items
      # to display properly.
      do.call(tagList, plot_output_list)
    }) #end of output$plots
  
    # Call renderPlot for each one. Plots are only actually generated when they
    # are visible on the web page.
    for (i in 1:max_plots()) {
      # Need local so that each item gets its own number. Without it, the value
      # of i in the renderPlot() will be the same across all instances, because
      # of when the expression is evaluated.
      local({
        my_i &lt;- i
        plotname &lt;- paste("plot", my_i, sep="")
  
        output[[plotname]] &lt;- renderPlot({
  
          plot(1:my_i, 1:my_i,
               xlim = c(1, max_plots()),
               ylim = c(1, max_plots()),
               main = paste("1:", my_i, ".  n is ", input$n, sep = "")
          )
        })#end of renderPlot
      })#end of local
    }#end of loop over max_plots
  
    })#end of server
  </code></pre>
  
  <p>Here is my global.r</p>
  
  <pre><code>NumberOfPlots&lt;-function(n)
  {
    print("in global") 
    print(n)
    length(seq(from=1 , to=n, by = 1))
  }
  </code></pre>
  
  <p>Here is my ui.r</p>
  
  <pre><code>shinyUI(pageWithSidebar(
    headerPanel("Dynamic number of plots"),
    sidebarPanel(
      sliderInput("n", "Number of plots", value=1, min=1, max=7)
    ),
    mainPanel(
      # This is the dynamic UI for the plots
      uiOutput("plots")
    )
  ))
  </code></pre>