Jamey Copeland Jamey Copeland - 3 months ago 35
R Question

shiny app histogram error

Edit
The app starts up now, but I still have one warning:

Warning: Error in +: invalid argument to unary operator
Stack trace (innermost first):
76: renderPlot [C:\Users\jcopelan\Documents\Contracts/app.R#107]
68: output$distPlot
1: runApp

I believe that it has something to do with the call to ggplot but I can't figure out exactly what the problem is. I also added sample data that is 100 observations randomized from the entire data set.




I am creating a shiny app in R that takes a selection from a selectInput box, then subsets a data frame based on this. The selection which is now just a column of type double is then plotted as a histogram using ggplot2. Everything runs fine executing in Base R, but after I parse the sections into server and ui sections I get an error that 'x' must be numeric in hist (and I already know it to have type double). Any suggestions are appreciated!

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)


Here is the randomized 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")

Answer

I believe the "unary operator" error is because your + signs are on the new line instead of the end of the line before. I was still getting odd errors. The bottom line, however, is: Why are you manually setting binwidths and axis limits? The code below works, and generates the correct histograms. If this isn't what you want/need, can you explain what tweaks you need?

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)