title: Shiny + ggplot: How to subset reactive data object?
body: <p>I am making a shiny app, that shows the user a ggplot after he selects the daterange he is interested in (= the range for the x-axis). So I guess I need to define a reactive data object (correct?).</p>
  
  <p>The ggplot has some subsetting in it. R tells me that <em>reactive data object is not subsettable</em>. In my rookie understanding of ggplot, the subsetting has to be done inside the geom_bar(), geom_line() statements in order to obtain the graph that I want. </p>
  
  <ol>
  <li>Can anyone suggest me how to proceed with the subsetting? </li>
  <li><p>And how to reference the factor category in generating colors for the graph?
  Thanks!</p>
  
  <h1>sample data</h1>
  
  <pre><code>A = c(3, 4, 3, 5)
  B = c(2, 2, 1, 4)
  Z = c(1, 2, 1, 2)
  R = c(-2, -1, -3, 0)
  S = c(7,7,7,9)
  mydata = data.frame(cbind(A,B,Z,R,S))
  dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
  mydata$date = as.Date(dates)
  mydata.m = melt(mydata,id="date")
  names(mydata.m) = c("variable", "category","value")
  </code></pre>
  
  <h1>shiny server: select observations as per user input (dateRangeInput)</h1>
  
  <pre><code>data.r = reactive({
    a = subset(mydata.m, variable %in% input$daterange)
    return(a)
  })
  </code></pre>
  
  <h1>shiny server: make the plot</h1>
  
  <pre><code>output$myplot = renderPlot({
  
    # ggplot with proper reference to reactive function &lt;&lt;data.r()&gt;&gt;
    s = ggplot(data.r(), aes(x=variable, fill=category)) +   
  
      # bars for categories A, B, Z: needs subsetting the data... but how?
      + geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack")
      + geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity")
  
      # lines for categories R, S: same.
      + geom_line(subset=.(category=="R"), aes(y=value)) 
      + geom_line(subset=.(category=="S"), aes(y=value))
  
      # how to reference the factor &lt;&lt;category&gt;&gt; in reactive function &lt;&lt;data.r()&gt;&gt;?
      + scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))
  
    print(s)
  
  })
  </code></pre></li>
  </ol>
  
  <h1>UI.R</h1>
  
  <pre><code># INPUT PART
  
  library(shiny)
  
  shinyUI(pageWithSidebar(
    # Application title
    headerPanel("My App"),
  
    sidebarPanel( 
  
      dateRangeInput("daterange", "Date range:",
                 start  = "2014-01-01",
                 end    = "2014-04-01",
                 min    = "2014-01-01",
                 max    = "2014-04-01",
                 format = "dd/mm/yyyy",
                 separator = "-"),
  
      submitButton(text="Update!")
    ),
  # -----------------------------------------------
  
  # OUTPUT PART
  
    mainPanel(
      tabsetPanel(
        tabPanel("Tab 1", h4("Head 1"),plotOutput("myplot"))
      )
    )
  ))
  </code></pre>
  
  <h1>SERVER.R</h1>
  
  <pre><code>library(reshape)
  library(shiny)
  library(ggplot2)
  
  
  
  # GEN DATA -----------------------------------------------
  
  A = c(3, 4, 3, 5)
  B = c(2, 2, 1, 4)
  Z = c(1, 2, 1, 2)
  R = c(-2, -1, -3, 0)
  S = c(7,7,7,9)
  mydata = data.frame(cbind(A,B,Z,R,S))
  dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
  mydata$date = as.Date(dates)
  mydata.m = melt(mydata,id="date")
  names(mydata.m) = c("variable", "category","value")
  
  
  
  
  
  
  
  # SERVER -----------------------------------------------
  shinyServer(function (input, output) {
  
  
  # DATA
  
  data.r = reactive({
    a = subset(mydata.m, variable %in% input$daterange)
    return(a)
  })
  
  
  
  # GGPLOT
  
  mycolorgenerator = colorRampPalette(c('sienna','light grey')) 
  
  
  output$myplot = renderPlot({
  
    # ggplot with proper reference to reactive function &lt;&lt;data.r()&gt;&gt;
    s = ggplot(data.r(), aes(x=variable, fill=category))  +  
  
      # bars for categories A, B, Z: needs subsetting the data... but how?
       geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack") +
       geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity") +
  
      # lines for categories R, S: same.
       geom_line(subset=.(category=="R"), aes(y=value)) +
       geom_line(subset=.(category=="S"), aes(y=value)) +
  
      # how to reference the factor &lt;&lt;category&gt;&gt; in reactive function &lt;&lt;data.r()&gt;&gt;?
       scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))
  
    print(s)
  
  
  })
  })
  </code></pre>