R Shiny:不确定为什么ggplot会失败

Gre*_*mon 5 r ggplot2 shiny

闪亮的新手在这里.

我正在尝试编写一个R闪亮的脚本,我想要做的一件事是生成给定日期和不同地区的给定广告客户的广告观看次数的直方图.

我的表有以下列(带有示例数据):

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.
Run Code Online (Sandbox Code Playgroud)

UI选项卡上的所需输出与ggplot类似

ggplot(df2, aes(x=DMA, y=Impressions, fill=DMA)) +geom_histogram()
Run Code Online (Sandbox Code Playgroud)

并且应该看起来像这样

在此输入图像描述

但是,我收到了一个错误

Error: object 'DMA' not found
Run Code Online (Sandbox Code Playgroud)

当我基本上将同样的公式粘贴到R Shiny时.我的代码如下

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
})
Run Code Online (Sandbox Code Playgroud)

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
  ))
Run Code Online (Sandbox Code Playgroud)

其他一切都有效,包括标签.但这个情节并未出现.

更新:感谢,GGplot现在通过围绕它的print()语句的工作.但是,在无法找到变量的情况下会出现一个新问题.

Die*_*nne 10

df2不是数据,而是反应函数.使用df2()ggplot,不要忘了打印如上说.

如果发生这样的事情,不要假设"DMA在那里",而是print(str(df2))在临界点插入一个.


Car*_*son 5

尝试用print()包装ggplot对象