R闪亮:不知道为什么ggplot失败 [英] R Shiny: not sure why ggplot is failing
问题描述
我想写一个R闪亮的脚本,我想要做的事情之一是生成给定日期和给定日期的广告视图数的直方图广告客户跨不同地区。
我的表格包含以下列(带有示例数据):
日期印象广告客户因子1 DMA
2/19 22789麦当劳洛杉矶
1517富豪电影豪华纽约
2/20 12345麦当劳便宜DC
我在UI界面上的所需输出与ggplot一样。
ggplot(df2,aes(x = DMA,y = Impressions,fill = DMA))+ geom_histogram()
pre>
,应该看起来像这样
然而,我收到一个错误
错误:找不到对象'DMA'
when我基本上将相同的公式粘贴到R Shiny中。我的代码如下所示:
server.R
库)
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为什么这个图不打印
输出$ plot1< - renderPlot({
print(ggplot(df2,aes(x = DMA,y = Impressions,fill = DMA) )+ geom_histogram())
})
#服务器括号
})
$
ui.R库(闪亮)
df < - na。省略(read.csv(data.csv,fill = TRUE,nrows = 3000000))
daterange < - unique(df $ Date)
名称< - unique(df $ Advertiser)
shinyUI(pageWithSidebar(
)应用程序
headerPanel(Advertisement),
sidebarPanel(
selectInput(date,Date:,
choices = daterange),
selectInput(name,Partner,
choices = names)
$ b $,
$ b mainPanel(
tabsetPanel(
tabPanel(Plot1,plotOutput(plot1))
)
)
#用户界面括号
))
其他工作,包括标签。但是这个情节没有出现。
UPDATE:THANKS,GGplot现在通过在它周围包装print()语句来工作。但是,如果找不到变量,则会出现新问题。
解决方案
df2
不是数据,而是一个反应函数。在ggplot
中使用df2()
,并且不要忘记按上面所述打印。
如果发生这种情况,不要假设DMA在那里,但插入
print(str(df2))
在临界点。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
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.
解决方案
df2
is not data, but a reactive function. Usedf2()
inggplot
, 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.这篇关于R闪亮:不知道为什么ggplot失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文