闪亮的应用直方图错误

时间:2016-08-30 14:25:33

标签: r ggplot2 shiny

修改 该应用程序现在启动,但我仍然有一个警告:

警告:+中的错误:一元运算符的参数无效 堆栈跟踪(最里面的第一个):     76:renderPlot [C:\ Users \ jcopelan \ Documents \ Contracts / app.R#107]     68:输出$ distPlot      1:runApp

我认为这与调用ggplot有关,但我无法弄清问题是什么。我还添加了样本数据,这是从整个数据集中随机化的100个观察结果。

我在R中创建了一个闪亮的应用程序,它从selectInput框中进行选择,然后基于此子集数据框。然后使用ggplot2将现在只是double类型的列的选择绘制为直方图。一切都在Base R中运行良好,但在我将这些部分解析为服务器和ui部分之后,我得到一个错误,即' x'必须是hist中的数字(我已经知道它有double类型)。任何建议都表示赞赏!

 library(shiny)
 library(ggplot2)

 #Read in the service contract data set (all zero values from ECC and missing IBASE deleted out)


NotUnique = read.table("C:\\Users\\jcopelan\\Documents\\InService.txt", header = F, sep = ",")

#Name the df columns
names(NotUnique) = c("Contract_Number", "Contract_Transaction","Region",    "IBaseModel_Number",    "IBase_Agilent_Model","Start_Date", "IBase_Parent", "End_Date", "Sold_To",  "Ship_To",  "Sell_Date", "Total_Net", "List_Price", "NotSure", "True_Net", "GrossVal")

#Make a new dataframe that consists of only the first entry in the contract price, this way I can make histograms for regions and such
InService = NotUnique[!duplicated(NotUnique$Contract_Number, decreasing = T),]

InService$Start_Date = as.Date(as.character(InService$Start_Date))
InService$End_Date = as.Date(as.character(InService$End_Date))


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

# Application title
titlePanel("Contract Summary Application"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
   sidebarPanel(
     selectInput('Region',
                'Region',
                choices = c("Americas", "EMEA", "Greater China", "India",
                            "Japan", "SAPK","WorldWide"), selected = "Americas")
  ),

   # Show a plot of the generated distribution
   mainPanel(
      plotOutput("distPlot")
   )
)
))

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

NetVal = reactive({as.data.frame(subset(InService, Region %in% input$Region)$True_Net)})
#Make the histogram plot with ggplot2
breaks = reactive({seq(min(NetVal()), max(Netval()),
           length.out = round(max(NetVal()), digits = 4)/500)})

hist = reactive({hist(NetVal(),breaks(), plot=FALSE)})
binmax = reactive({max(hist()$counts)})



output$distPlot <- renderPlot({
  regplot = ggplot(NetVal(), aes(NetVal, color = "Blue")) 
  + geom_histogram(binwidth = 500) 
  + ylim(c(0,binmax())) 
  + xlim(c(min(NetVal()),quantile(NetVal(),.95)))
  + xlab("Amount Spent ($)")
  + ylab("Frequency")
  + fte_theme()

print(regplot)
 })
 })

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

这是随机数据集

  structure(list(Region = structure(c(7L, 3L, 3L, 6L, 2L, 2L, 2L, 
3L, 3L, 7L, 1L, 2L, 1L, 6L, 5L, 3L, 7L, 3L, 2L, 1L, 3L, 7L, 7L, 
3L, 2L, 3L, 3L, 3L, 7L, 3L, 3L, 2L, 3L, 4L, 3L, 3L, 4L, 2L, 3L, 
3L, 2L, 3L, 2L, 3L, 2L, 3L, 4L, 3L, 3L, 2L, 6L, 3L, 5L, 3L, 3L, 
2L, 7L, 3L, 5L, 3L, 4L, 7L, 7L, 3L, 3L, 3L, 5L, 3L, 3L, 7L, 7L, 
2L, 2L, 5L, 2L, 3L, 3L, 3L, 3L, 3L, 7L, 3L, 3L, 3L, 5L, 2L, 2L, 
3L, 2L, 3L, 3L, 3L, 3L, 5L, 7L, 1L, 5L, 3L, 7L, 2L), .Label = c("", 
"Americas", "EMEA", "Greater China", "India", "Japan", "SAPK"
), class = "factor"), True_Net = c(10522.2, 3607.2, 33043.92, 
550800, 48242.37, 140040, 216607.2, 4278, 11880, 3737.48, 9830.4, 
215355, 7380000, 225000, 190440, 6825.6, 2000.05, 42982.21, 3528429.41, 
6456000, 16174.08, 205824, 103998, 2774.4, 14833.44, 42210, 3824.64, 
24942.34, 63036.23, 1392, 5522.4, 50443.2, 23086.8, 124698.23, 
140908.8, 66288, 81667.02, 636, 1968, 3861.36, 3732, 80000, 21808.8, 
2101.56, 40884, 103104, 94070.03, 24336, 2494.8, 52176, 719100, 
1015.2, 48917.74, 2713.2, 27907.2, 19668, 15614.4, 5809.44, 300192, 
67564.8, 18423, 6038.4, 34224, 1170, 1630.8, 26523.31, 1390000, 
62763.2, 25671.09, 80592, 6134.69, 235176, 57648, 205999.78, 
50136, 2130, 16976.28, 0, 10127.4, 4483.51, 2688, 1644157.2, 
3336, 9930.12, 37378.8, 197143.2, 19668, 14664, 45651.6, 576, 
10519.13, 5603.78, 5011.2, 333024, 46920, 1782000, 147108, 18048, 
8345.88, 4580.06)), .Names = c("Region", "True_Net"), row.names = c(139252L, 
168993L, 50887L, 147555L, 42219L, 137207L, 39332L, 164003L, 129109L, 
76248L, 142612L, 183452L, 146662L, 96736L, 18778L, 27846L, 190910L, 
57055L, 121244L, 124567L, 168122L, 203483L, 79170L, 37398L, 38729L, 
132515L, 98739L, 129955L, 87175L, 106204L, 10986L, 17875L, 168417L, 
206082L, 104991L, 1090L, 141201L, 157185L, 96915L, 196929L, 28880L, 
211286L, 69145L, 70201L, 215507L, 57974L, 151559L, 113300L, 146977L, 
108231L, 125906L, 210328L, 189968L, 125125L, 78831L, 66537L, 
77616L, 47462L, 14840L, 175840L, 173340L, 66249L, 121586L, 177159L, 
40958L, 44729L, 143400L, 19238L, 150885L, 3012L, 196802L, 149237L, 
115877L, 136052L, 196532L, 73278L, 72437L, 126332L, 26803L, 187446L, 
186575L, 37180L, 192443L, 133701L, 203954L, 186105L, 47635L, 
155246L, 25619L, 167488L, 79686L, 44698L, 29360L, 14347L, 193909L, 
139130L, 185265L, 147108L, 157757L, 81261L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

我认为“一元运算符”错误是因为您的+符号位于新行,而不是之前的行尾。我还是犯了奇怪的错误。但最重要的是:为什么要手动设置binwidths和轴限制?下面的代码可以工作,并生成正确的直方图。如果这不是您想要/需要的,您能解释一下您需要的调整吗?

library(ggplot2)
library(dplyr)

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

  # Application title
  titlePanel("Contract Summary Application"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput('Region',
                  'Region',
                  choices = levels(NotUnique$Region), selected = "Americas")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

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

  #Make the histogram plot with ggplot2

  output$distPlot <- renderPlot({
    ggplot(NotUnique %>% filter(Region  == input$Region), aes(True_Net)) +
      geom_histogram(fill = "blue") +
      xlab("Amount Spent ($)") +
      ylab("Frequency")


  })
})

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