2016-08-30 5 views
-1

bearbeiten Die App startet jetzt, aber ich habe immer noch eine Warnung:glänzend app Histogramm Fehler

Warnung: Fehler bei +: ungültig Argument unärer Operator Stapelüberwachung (innerste zuerst): 76: renderPlot [C: \ Benutzer \ jcopelan \ Dokumente \ Verträge/app.R # 107] 68: Ausgang $ distPlot 1: runapp

ich glaube, dass es etwas mit dem Anruf zu tun hat ggplot aber ich kann‘ t herauszufinden, was genau das Problem ist. Ich fügte auch Beispieldaten hinzu, die 100 Beobachtungen sind, die vom gesamten Datensatz randomisiert wurden.


I eine glänzende app in R bin schaffen, das eine Auswahl aus einer selectInput Box nimmt, dann Untergruppen einen Datenrahmen auf dieser Basis. Die Auswahl, die jetzt nur eine Spalte vom Typ double ist, wird dann als Histogramm mit ggplot2 dargestellt. Alles läuft gut in Base R ausgeführt, aber nachdem ich die Abschnitte in Server und UI Abschnitte analysiert habe, bekomme ich einen Fehler, dass 'x' in hist numerisch sein muss (und ich weiß bereits, dass es double hat). Irgendwelche Vorschläge werden geschätzt!

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) 

Hier ist die randomisierte Dataset

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

Ich vermute, Sie müssen 'NetVal()' anstelle von 'NetVal' verwenden. [Siehe hier.] (Http://shiny.rstudio.com/tutorial/lesson6/) – Axeman

+0

Danke für den Kommentar, der mich über diesen Fehler hinausbringt und mir diesen gibt: Warnung: Fehler in .getReactiveEnvironment() $ currentContext: Operation ohne aktiven reaktiven Kontext nicht erlaubt. (Sie haben versucht, etwas zu tun, die nur aus dem Inneren eines reaktiven Ausdruck oder Beobachter getan werden kann.) Stack-Trace (innerste zuerst): 44: .getReactiveEnvironment() $ Current 43: .dependents registrieren $ 42: NetVal 41 : seq 40: Server [C: \ Benutzer \ jcopelan \ Dokumente \ Verträge/app.R # 97] 1: runApp –

+0

Ich versuche, Ihren Code auszuführen, aber eine Zeile ist auskommentiert, die es bricht. Außerdem verstehe ich nicht, warum Sie 'hist' außerhalb von' renderPlot' aufrufen. Ein tatsächliches _minimal reproduzierbares_ Beispiel würde viel helfen. – Axeman

Antwort

1

ich die „unärer Operator“ Fehler glauben, weil Ihr + Zeichen auf der neuen Linie anstelle des Ende der Leitung vor sind. Ich bekam immer noch seltsame Fehler. Die Quintessenz ist jedoch: Warum setzen Sie manuell binwidths und Achsenlimits? Der folgende Code funktioniert und generiert die korrekten Histogramme. Wenn dies nicht das ist, was du willst/brauchst, kannst du erklären, welche Verbesserungen du brauchst?

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)