R Shiny: I don't know why ggplot doesn't work

Brilliant newbie here.

I'm trying to write an R shiny script, and one of the things I want to do is create a histogram of the number of times my ads were viewed for a given day and for this advertiser in different regions.

My table has the following columns (with example 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 result on the user interface tab looks something like this: ggplot

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

and should look like this

enter image description here

However, I get an error

Error: object 'DMA' not found

when I basically paste 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 tabs. But this plot does not appear.

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

+5
2

df2 - , . df2() ggplot , .

, , "DMA ", print(str(df2)) .

+8

ggplot print()

+5

All Articles