闪亮的直方图使用ggplot2无法正确过滤

时间:2016-09-12 17:32:23

标签: r ggplot2 shiny

我正在创建一个闪亮的网络应用程序,其中将根据两组过滤标准(Regions =“Americas”,“EMEA”等)和Metrics =“ElapsedTime”,“LabourTime”等制作直方图。 。我首先在基础R中创建了一个函数,并确保我想要做的工作(确实如此)。然后我将它转换为一个闪亮的网络应用程序,我现在在运行应用程序时收到以下警告:

警告:stat_bin()中的计算失败: 尝试应用非功能性

我尝试了各种各样的事情无济于事,非常感谢任何帮助!我认为该错误与reg.filt()的值(在服务器部分下面)

有关

这里是我创建的app.R文件:

library(dplyr)
library(ggplot2)
library(zoo)
library(xtable)
library(shiny)
library(RColorBrewer)



   CallClosure1 = read.table("C:\\Users\\jcopelan\\Documents\\Ad Hoc Presentations_Analysis\\Call Closure\\callclosure01012016.txt", header = T, sep = "")
    CallClosure2 = read.table("C:\\Users\\jcopelan\\Documents\\Ad Hoc Presentations_Analysis\\Call Closure\\callclosure05012016.txt", header = T, sep = "")
    CallClosure = rbind(CallClosure1, CallClosure2)

    names(CallClosure) = c("Region","ConfirmationNumber","ContractNumber",  "TransType",    "HCompletionDate",      "CancelledIntern",  "HeaderStatus", "ASPName",  "AcctIndicator",    "HShipCompletionDate",  "HShipCompetionTime",   "HShipCreateDate","HShipCreateTime","IShipEndDate", "IShipEndTime", "IShipCompetionTime", "IShipCompletionDate", "WarrantyUpdFld", "SoldTo", "ShipTo", "HSystemization", "HCreationDate", "NumVisits", "TravelTime", "ReportedTime", "LabourTime", "ElapsedTime", "ActualTime" )

    rm(CallClosure1)
    rm(CallClosure2)

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

  mainPanel(
    fluidRow(
      wellPanel(
        column(8,selectInput('Region',
                             'Region',
                             choices = c("Americas", "EMEA", "Greater China", "India",
                                         "Japan", "SAPK"), selected = "Americas"), offset = 4)),
      wellPanel(
        column(8,selectInput('Metric',
                             'Metric',
                             choices = c("TravelTime", "ReportedTime", "LabourTime", "ElapsedTime",
                                         "ActualTime"), selected = "ReportedTime"), offset = 4)),

      column(8, plotOutput("distPlot"))))))
      #column(8, verbatimTextOutput("stats"))))))

# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {

  reg.filt = reactive({as.data.frame(CallClosure[CallClosure$Region == input$Region, input$Metric])})


  #Make the histogram plot with ggplot2

  title.hist = reactive({paste("Time distribution for",input$Metric,"in the", input$Region, "Region")})


  output$distPlot <- renderPlot({
    regplot = ggplot(reg.filt(), aes = reg.filt()) +
      geom_histogram(aes(fill = ..count..)) +
      xlab("Metric") +
      ylab("Frequency") +
      ggtitle(title.hist()) + 
      #fte_theme()

    print(regplot)
  })



# Run the application 
shinyApp(ui = ui, server = server)

以下是数据框中的一些示例代码,我尝试使用200个观察值进行分段(使用dput):

structure(list(Region = structure(c(3L, 4L, 2L, 5L, 3L, 3L, 2L, 
7L, 2L, 2L, 7L, 2L, 2L, 5L, 4L, 3L, 3L, 4L, 3L, 2L, 3L, 3L, 4L, 
3L, 4L, 5L, 2L, 4L, 6L, 3L, 3L, 4L, 2L, 5L, 5L, 2L, 3L, 4L, 4L, 
4L, 5L, 3L, 5L, 4L, 3L, 4L, 5L, 3L, 4L, 3L, 6L, 3L, 3L, 6L, 4L, 
7L, 3L, 6L, 3L, 3L, 2L, 2L, 4L, 3L, 4L, 5L, 5L, 4L, 2L, 3L, 5L, 
5L, 4L, 5L, 4L, 2L, 2L, 3L, 6L, 3L, 4L, 7L, 7L, 4L, 3L, 2L, 2L, 
3L, 5L, 2L, 2L, 6L, 2L, 3L, 3L, 6L, 2L, 3L, 6L, 2L, 3L, 5L, 3L, 
5L, 4L, 2L, 4L, 6L, 2L, 2L, 7L, 3L, 4L, 2L, 3L, 3L, 4L, 4L, 3L, 
2L, 3L, 2L, 5L, 7L, 2L, 3L, 4L, 2L, 3L, 2L, 3L, 4L, 2L, 3L, 3L, 
2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 5L, 2L, 3L, 3L, 7L, 2L, 
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, 99, 0, 0.5, 3.5, 
2.5, 0, 4, 5, 8, 8, 0, 10.5, 3.5, 6, 2, 17, 0, 3, 3, 3, 2, 3, 
0, 0, 4.5, 1, 15, 6, 0, 20, 3, 6, 2.5, 2, 0, 15, 5, 5, 0, 6, 
1.5, 0, 0.5, 3.3, 9.5, 9, 10, 5.5, 0, 8, 0, 10, 3.5, 11, 6, 7, 
0, 2, 6, 7, 6.5, 0, 4, 6, 0, 1, 3.5, 0, 2, 3, 2, 6, 0, 0, 2, 
4, 13, 0, 1.5, 0, 8, 8.5, 3, 2.5, 2, 8, 0, 6, 0, 1, 0, 28, 3, 
0, 8, 1.6, 0, 4, 5, 0, 0, 0, 4, 3, 3, 9.5, 14, 0.2, 0, 0, 0, 
0, 7.5, 3, 0, 18, 5.5, 8, 12, 6.5, 10, 5, 18, 4.5, 8, 4, 1, 5, 
3, 2, 5, 5.5, 0, 7.5, 0, 4.5, 2, 6, 2.5, 1.5, 5, 26, 0, 2, 7, 
0, 0, 9.5, 7, 6, 3, 0.1, 0)), .Names = c("Region", "TravelTime", 
"ReportedTime", "LabourTime", "ElapsedTime", "ActualTime"), row.names = c(82215L, 
407772L, 44572L, 466640L, 122019L, 90516L, 47667L, 503759L, 60391L, 
263859L, 493756L, 40597L, 10316L, 446066L, 145723L, 333731L, 
341599L, 421196L, 386466L, 250863L, 127299L, 95730L, 406939L, 
71982L, 157870L, 204903L, 265764L, 404955L, 222993L, 330266L, 
352683L, 165999L, 14686L, 187423L, 460413L, 247723L, 340521L, 
145914L, 438967L, 168249L, 188863L, 331323L, 440999L, 402691L, 
357055L, 414330L, 206783L, 68011L, 180498L, 331768L, 214334L, 
98301L, 127944L, 224303L, 175800L, 494371L, 116513L, 469616L, 
341709L, 121979L, 301423L, 19097L, 148710L, 317021L, 177943L, 
204320L, 450730L, 159494L, 54901L, 342012L, 464071L, 453757L, 
411293L, 205756L, 398801L, 14689L, 44028L, 379562L, 479223L, 
335202L, 412250L, 238856L, 498725L, 436054L, 354748L, 53071L, 
49133L, 388381L, 208698L, 260061L, 268861L, 223557L, 13405L, 
106303L, 332177L, 467183L, 265828L, 333123L, 218655L, 24187L, 
379323L, 201369L, 82561L, 185473L, 393645L, 266783L, 437822L, 
477560L, 1278L, 49187L, 236224L, 358768L, 415573L, 25752L, 73041L, 
82535L, 405435L, 438122L, 388557L, 248764L, 339696L, 266418L, 
193505L, 233092L, 34685L, 389995L, 397442L, 269752L, 112644L, 
310222L, 333027L, 422015L, 275572L, 119027L, 347827L, 35915L, 
392172L, 396316L, 5859L, 36044L, 290117L, 57852L, 61721L, 399536L, 
169369L, 440853L, 271420L, 335392L, 136437L, 500524L, 298664L, 
65503L, 281546L, 55441L, 75320L, 322259L, 394201L, 349323L, 213314L, 
211453L, 415185L, 92206L, 21344L, 450634L, 354241L, 419694L, 
368254L, 223310L, 397900L, 500673L, 117273L, 228553L, 247630L, 
38170L, 454525L, 59833L, 196492L, 138756L, 296021L, 430978L, 
231106L, 200695L, 348712L, 175561L, 72695L, 3343L, 391568L, 227506L, 
447990L, 16912L, 334677L, 297176L, 27532L, 254530L, 10743L, 131410L, 
362958L, 429976L, 399552L, 477920L), class = "data.frame")

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我想有一些更好的解决方案,但几乎没有任何更改,您的代码应该工作。尝试用以下代码替换定义reg.filt的代码:

reg.filt = reactive({
            df <- as.data.frame(CallClosure[CallClosure$Region == input$Region, input$Metric])
            names(df <- input$Metric)
            df
            })

比情节部分:

output$distPlot <- renderPlot({
    regplot <- ggplot(reg.filt(), aes_string(x = input$Metric)) +
            geom_histogram(aes(fill = ..count..)) +
            xlab("Metric") +
            ylab("Frequency") +
            ggtitle(title.hist()) 
    #fte_theme()

    regplot
})

让我知道这有助于......