5

Shiny newbie here.

I am trying to write a R shiny script, and one of things I want to do is generate a histogram of the number of ad views for a given day and a given advertiser across different regions.

My table is has the following columns (with sample data):

Date    Impressions Advertiser  Factor 1         DMA

2/19       22789     McDonalds   Cheap           Los Angeles
2/17       15002    Regal Cinem  Luxury          New York
2/20       12345     McDonalds   Cheap           D.C.

My desired output on the UI tab is something like this with ggplot

ggplot(df2, aes(x=DMA, y=Impressions, fill=DMA)) +geom_histogram()

and should looks like this

enter image description here

However, I am getting an error

Error: object 'DMA' not found

when I am basically pasting the same formula into R Shiny. My code is as follows

server.R

library(shiny)
library(ggplot2)

df<- na.omit(read.csv("data.csv", fill= TRUE, nrows= 3000000))

shinyServer(function(input, output){

df2<- reactive({df[df$Date==input$date & df$Advertiser==input$name, ]})

#FIXME why is this plot not printing
output$plot1<- renderPlot({
  print(ggplot(df2, aes(x=DMA, y=Impressions, fill=DMA)) +geom_histogram())

})
#end of server brackets
})

ui.R

library(shiny)
df<- na.omit(read.csv("data.csv", fill= TRUE, nrows= 3000000))
daterange<- unique(df$Date)
names <- unique(df$Advertiser)

shinyUI(pageWithSidebar(

  #Title of Application
  headerPanel("Advertisement"), 

  sidebarPanel( 

    selectInput("date", "Date:", 
                choices= daterange),

    selectInput("name", "Partner", 
                choices= names)

  ),

  mainPanel(
    tabsetPanel(
      tabPanel("Plot1", plotOutput("plot1"))

      )
    )

  #end of UI brackets
  ))

Everything else works, including the tabs. But this plot is not showing up.

UPDATE: THANKS, GGplot now works by wrapping the print() statement around it. However, a new issue arises where a variable cannot be found.

Green Demon
  • 4,078
  • 6
  • 24
  • 32
  • 5
    Are you saying that the plot works normally, but not when in Shiny? Try putting your plot in `print()` e.g. `p <- ggplot(...) + geom_histogram(...); print(p)`. – Ciarán Tobin Apr 03 '13 at 14:54
  • thanks. ggplot works now, but its not plotting due to "Object DMA not found" – Green Demon Apr 03 '13 at 15:02
  • 1
    Is `DMA` definitely a column in `df2`? – Ciarán Tobin Apr 03 '13 at 15:05
  • yes, df2 took all the columns of df – Green Demon Apr 03 '13 at 15:06
  • 1
    Don't dismiss the error message so casually. If R says that it can't find DMA, then it _really_ can't find DMA. It's possible that there is something weird going on with environments/evaluation but before you go down that road, make _absolutely_ sure that the variable really is in the data frame. Use some debugging tools and examine `df2` just prior to the ggplot call. – joran Apr 03 '13 at 15:16
  • since head(data.frame(df2())) returned the DMA column, i assumed it was fine. however, it did not occur to me that df2 is not of the right type until someone pointed out to me. – Green Demon Apr 03 '13 at 15:25

2 Answers2

10

df2 is not data, but a reactive function. Use df2() in ggplot, and do not forget to print as said above.

If things like this happen, do no assume that "DMA is there", but insert a print(str(df2)) at the critical point.

Dieter Menne
  • 10,076
  • 44
  • 67
  • thanks, that solved it. it is still not intuitive to me that df now representing a reactive function, will take a while to get used to – Green Demon Apr 03 '13 at 15:24
5

Try wrapping the ggplot object with print()

Carson
  • 2,617
  • 1
  • 21
  • 24