【发布时间】:2013-03-25 06:49:43
【问题描述】:
这里是闪亮的新手。
我正在尝试编写一个闪亮的 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()
应该是这样的
但是,我收到一个错误
Error: object 'DMA' not found
当我基本上将相同的公式粘贴到 R Shiny 中时。我的代码如下
服务器.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() 语句包裹在它周围来工作。但是,在无法找到变量的情况下出现了一个新问题。
【问题讨论】:
-
你是说剧情正常,但在Shiny中不行?试着把你的情节放在
print()例如p <- ggplot(...) + geom_histogram(...); print(p). -
谢谢。 ggplot 现在可以工作,但由于“找不到对象 DMA”而无法绘图
-
DMA绝对是df2中的一列吗? -
是的,df2 占据了 df 的所有列
-
不要这么随便地忽略错误信息。如果 R 说它找不到 DMA,那么它真的 找不到 DMA。环境/评估可能会发生一些奇怪的事情,但在你走这条路之前,请绝对确保变量确实在数据框中。使用一些调试工具并在 ggplot 调用之前检查
df2。