title: Plot renders just before being hidden using conditionalPanel
body: <p>I have a graph that I only want to display if it wouldn't take too long to render, otherwise I want to have a button the user can press to render it.
  My problem is that the graph renders before being hidden in some circumstances.</p>
  
  <p>Open this minimally reproduced example up and try the following steps</p>
  
  <p>UI.r</p>
  
  <pre><code>shinyUI(fluidPage(
  
  
  titlePanel("when to render"),
    fluidRow(
      column(2,radioButtons("TheChoice", label= "choices",
                            choices = list("only one" = 1,"just two" = 2,"three" = 3,"all four" = 4),selected = 1))
    ),
    fluidRow(
      mainPanel(tabsetPanel(tabPanel("Blank page",NULL),
                            tabPanel("plot output",
                                     verbatimTextOutput("validRows"),
                                     conditionalPanel(
                                       condition = "output.validRows  &lt; 3",
                                       plotOutput("thePlot")
                                     ))
                            )
               )
    )
  ))
  </code></pre>
  
  <p>Server.r</p>
  
  <pre><code>library(shiny)
  library(ggplot)
  library(reshape2)
  `[` &lt;- function(...) base::`[`(...,drop=FALSE)
  myTable &lt;- matrix(1:20,ncol=4)
  colnames(myTable) &lt;- c("Index","catone","cattwo","catthree")
  rownames(myTable) &lt;- c("first","second","third","fourth","fifth")
  shinyServer(function(input, output,session) {
    TableIndex &lt;- reactiveValues(X = NULL)
    observe({
      print("updating TableIndex")
      if(input$TheChoice == 1) TableIndex$X &lt;- 1 else TableIndex$X &lt;- 1:input$TheChoice
    })
    graphedTableData &lt;- reactive({
      return(myTable[TableIndex$X,2])
    })
    dataSubset &lt;- 
      reactive({
        print(paste0("calculating subset with ", length(TableIndex$X)," rows"))
        renderedData &lt;- graphedTableData()
        renderedData2 &lt;- melt(renderedData,id=rownames(renderedData))
        colnames(renderedData2)&lt;-c("catone","cattwo","catthree")
        return(renderedData2)
      })
    output$thePlot &lt;- renderPlot({
      print(paste0("Rendering Plot with ", length(TableIndex$X)," rows"))
      theSubset &lt;- dataSubset()
      ggplot(data=theSubset,aes(x=catone,y=cattwo,colour=catthree)) + 
        geom_line()
    })
    output$validRows &lt;- reactive({print("updating validRows");length(TableIndex$X)})
  
  })
  </code></pre>
  
  <ol>
  <li>click on the plot output tab: a plot renders and is then output (with one data row used)</li>
  <li>select choice 2: a plot renders and is then output (with two data rows used)</li>
  <li>select choice 3: a plot renders and is THEN hidden :(</li>
  <li>select choice 4: nothing is rendered/output (good)</li>
  </ol>
  
  <p>now going between 3 and 4, nothing happens (good)</p>
  
  <p>By looking at the console output, you can see that "TableIndex" is updated first, so it seems like this should never render with choice 3, since the plot would immediately disappear before it was updated. Presumably, that updating already triggered the reactive() and renderPlot() though.</p>
  
  <p>How do I prevent these functions from executing in this order? I know I could just short circuit the reactive() and renderPlot() by checking TableIndex$X in the first line, but that seems hacky and I'm just learning shiny so I'm hoping for a cleaner solution</p>
  
  <p>Bonus points if you implement the appearance of a render button instead of displaying nothing for choices 3 and 4. I've yet to attempt that but I believe it would be something with renderUI() ?</p>
  
  <p>(TableIndex is calculated separately and then accessed in this way because in real life, it is finding the relevant indices from a large table and then applying those same indices to other tables. Assume that the index finding is fast)</p>