2016-08-30 69 views
-1

编辑 的应用程序现在启动,但我仍然有一个警告:闪亮应用直方图错误

警告:错误+:无效的参数,以一元运算符 堆栈跟踪(最前): 76: renderPlot [C:\用户\ jcopelan \文件\合同/ app.R#107] 68:输出$ distPlot 1:runApp

我相信它有一些与呼叫做ggplot但我可以”弄清楚究竟是什么问题。我还添加了从整个数据集中随机分配的100个观测值的样本数据。


我建立R中有光泽的应用程序,需要从盒selectInput的选择,那么子集基于该数据帧。现在只是一个double类型的列的选择然后使用ggplot2绘制为直方图。一切运行良好执行在基地R,但后解析部分到服务器和用户界面部分我得到一个错误,'x'必须是数字在hist(我已经知道它有类型双)。任何建议表示赞赏!

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") 
+2

我猜你需要使用'NetVal()'而不是'NetVal'。 [见这里。](http://shiny.rstudio.com/tutorial/lesson6/) – Axeman

+0

感谢您的评论,这让我过去的错误,并给我这一个:警告:.getReactiveEnvironment()中的错误$ currentContext:没有激活的反应环境,不允许操作。 (你试图做一些事情,只能从反应性表达或观察者内完成的。) 堆栈跟踪(最内第一): 44:.getReactiveEnvironment()$ currentContext 43:.dependents $寄存器 42:NetVal 41 :seq 40:server [C:\ Users \ jcopelan \ Documents \ Contracts/app.R#97] 1:runApp –

+0

我试图运行你的代码,但是有一行被注释掉了。另外,我不明白你为什么要在'renderPlot'之外调用'hist'。一个实际的_minimal reproducible_例子会有很大的帮助。 – Axeman

回答

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)