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

时间:2013-04-03 14:28:02

标签: 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.

我在UI选项卡上的所需输出与ggplot

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

应该看起来像这样

enter image description here

然而,我收到错误

Error: object 'DMA' not found

当我基本上将相同的公式粘贴到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
})

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
  ))

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

更新:谢谢,GGplot现在可以通过在其周围包装print()语句来实现。但是,在无法找到变量的情况下会出现一个新问题。

2 个答案:

答案 0 :(得分:9)

df2不是数据,而是反应函数。在df2()中使用ggplot,不要忘记如上所述进行打印。

如果发生这种情况,请不要假设“DMA存在”,但在关键点插入print(str(df2))

答案 1 :(得分:5)

尝试使用print()

包装ggplot对象