Rshiny throws error after publish the app

I implemented a simple stack line graph in Rshiny. The file read from the .csv file added to project folder. The graph is working and displayed fine while running the application from Rstudio. But once the app is published in www.shinyapps.io it is throwing an error Error in ggplot2: object 'data' not found . Could anyone help me why this is happening after app deployment. Thanks in advance.

ui.R

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(RColorBrewer)
data <- read.csv("pie.csv", header = TRUE)
recommendation <- read.csv("recommendation.csv", header = TRUE)
shinyUI(
  dashboardPage(
    dashboardHeader(title = "ONE", #dropdownMenuOutput("msgOutPut")
                    dropdownMenu(type = "message",
                                 messageItem(from = "Finance Update", message = "we are on threshold",icon = icon("dollar"),time = "10:00am"),
                                 messageItem(from = "Sales Update",  message = "Sales are at 55%", icon = icon("bar-chart"),time = "1:00pm"),
                                 messageItem(from = "Sales Update", message = "Sales meeting at 6pm on Monday", icon = icon("handshake-o"),time = "6:00pm")
                    ),dropdownMenu(type = "tasks", badgeStatus = "warning",
                                   notificationItem(icon = icon("users"), status = "info",
                                                    "5 new members joined today")
                    )),
    
    dashboardSidebar(
      #sliderInput("bins","Number of Breaks",1,100,50),
      sidebarMenu(
      menuItem("Summary", tabName = "Summary", icon = icon("dashboard")),
      menuItem("ASupport", tabName = "AppSupport", icon = icon("tablet")),
      menuItem("InSupport", tabName = "finance", icon = icon("fax")),
      menuItem("SeProductivity", tabName = "dashboard", icon = icon("file"))
    )),
    dashboardBody(
      # frow1 <- fluidRow(
      #   infoBoxOutput("progressBox"),
      #   infoBoxOutput("approvalBox")
      # ),
      tabItems(
        tabItem(tabName = "dashboard",
                h1("Service Productivity"),
                fluidRow(
                 # box(plotOutput("histogram"))
                  box(
                    title = "Left Shift Productivity"
                    ,status = "primary"
                    ,solidHeader = TRUE 
                    ,collapsible = TRUE
                    ,plotOutput("pieChart", height = "300px")
                  ),
                  box(
                    title = "Degree of Automation"
                    ,status = "primary"
                    ,solidHeader = TRUE 
                    ,collapsible = TRUE
                    ,plotOutput("degreeOfAutomation", height = "300px")
                  ),
                  box(
                    title = "Service Cost Efficiency"
                    ,status = "primary"
                    ,solidHeader = TRUE 
                    ,collapsible = TRUE
                    ,plotOutput("serviceCostEfficiency", height = "300px")
                  )
                )),
          tabItem(tabName = "finance",
                  h1("Digital Infra and Operations Metrics"),
                  fluidRow(
                    box(
                      title = "Availability"
                      ,status = "primary"
                      ,solidHeader = TRUE 
                      ,collapsible = TRUE 
                      ,plotOutput("revenuebyPrd", height = "300px")
                    ),
                    box(
                      title = "Backup Success Rate"
                      ,status = "primary"
                      ,solidHeader = TRUE 
                      ,collapsible = TRUE 
                      ,plotOutput("backupsuccessrate", height = "300px")
                    )
                  )
                  ),
          tabItem(tabName = "sales",
                  h2("Service Cost efficiency")
                  ),
        tabItem(tabName = "Summary",
                h2("Summary")
        ),tabItem(tabName = "AppSupport",
                  h2("App Support")
        )
        )
      )
        
    )
  )

server.R

library(shiny)
library(shinydashboard)

shinyServer(function(input,output){
  
  output$progressBox <- renderInfoBox({
    infoBox(
      "Sales", paste0("75", "%"), icon = icon("usd"),
      color = "purple"
    )
  })
  output$approvalBox <- renderInfoBox({
    infoBox(
      "Satisfaction", "90%", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow"
    )
  })
  
  output$histogram <- renderPlot({
    hist(faithful$eruptions, breaks = input$bins)
  })
  output$hist <- renderPlot({
    hist(faithful$eruptions, breaks = input$bins)
  })
  output$pieChart <- renderPlot({
    colors = c("red", "yellow", "green", "violet", "orange")
    pie(table(pieChartValues$Shift),col=colors)
    legend(.9, 1, legend = pieChartValues$Shift, cex = 0.7, fill = colors)
    
  })
  
  output$serviceCostEfficiency <- renderPlot({
    ggplot() + geom_area(data=sujith, aes(x=year, y=cost, fill=z)) + 
      guides(fill = guide_legend(reverse=TRUE))
    
    # ggplot(data = diamonds, aes_string(x=Year, y=Value, fill=Sector)) +
    #   geom_area(colour="black", size=.2, alpha=.4) +
    #   scale_fill_brewer(palette="Greens", breaks=rev(levels(Sector)))
  })
  
  output$revenuebyPrd <- renderPlot({
    ggplot(data = recommendation, 
           aes(x=Product, y=Revenue, fill=factor(Revenue))) + 
      geom_bar(position = "dodge", stat = "identity") + ylab("") + 
      xlab("") + theme(legend.position="bottom" 
                              ,plot.title = element_text(size=15, face="bold")) + 
      ggtitle("") + labs(fill = "")
  })
  output$degreeOfAutomation <- renderPlot({
    ggplot(data = recommendation, 
           aes(x=Product, y=Revenue, fill=factor(Revenue))) + 
      geom_bar(position = "dodge", stat = "identity") + ylab("") + 
      xlab("") + theme(legend.position="bottom" 
                       ,plot.title = element_text(size=15, face="bold")) + 
      ggtitle("") + labs(fill = "")
  })
  output$backupsuccessrate <- renderPlot({
    ggplot(data = recommendation, 
           aes(x=Product, y=Revenue, fill=factor(Revenue))) + 
      geom_bar(position = "dodge", stat = "identity") + ylab("") + 
      xlab("") + theme(legend.position="bottom" 
                       ,plot.title = element_text(size=15, face="bold")) + 
      ggtitle("") + labs(fill = "")
  })
  
  output$msgOutPut <- renderMenu({
    msgs <- apply(read.csv("messages.csv"),1,function(row){
      messageItem(from = row[["from"]],message = row[["message"]])
    })
    
    dropdownMenu(type = "messages", .list = msgs)
  })
})

Without being able to see your data or your entire code within areproducible example, it's not possible to say for sure, but try changing aes_string to aes as a first step.

I tried aes_string but no luck. Thing is this is throwing error only after publishing the app. Run fine in simulator. I tried default data set "diamond" and it is woking both in simulator and after publishing. Issue is when reading from .csv file.

Ok, you've changed the original question and have replaced aes_string, so my previous comment no longer applies.

Where is recommendation being defined, which is being passed as the data parameter to ggplot?

1 Like

@josh recommendation is declared in ui.R and it is working fine even after publishing. The issue is with 'data' object.

Don't know why. But when i read the 'data' in server.R it is working fine even after publishing. Could anyone throw some light on this.

The code you posed doesn't use the data object anywhere other than the line defining it in ui.R. The data frames that are plotted in the posted code are: recommendation, sujith, and diamonds (in the commented out section). The sujith data frame isn't defined anywhere. If this code is working as-is on your local system, it's probably because you have objects lingering in your environment from previous work. It's good practice to test your code locally with a new session and a clean environment, to rule out that sort of bug.

It would be a good idea to take a look at our FAQ on debugging and creating reproducible examples for Shiny: Shiny debugging and reprex guide

This is the sort of problem that can be difficult to make a reproducible example for, but you can still do more to help your helpers. There's a lot in this code that doesn't seem to relate to your problem, and I'm afraid that it's asking a lot to expect people to sort through it all. Your best bet is to make a copy of your app and start stripping things out until you've got the simplest possible set of code that still exhibits the problem, then post that here (but please don't overwrite your original post again — as @martin.R noted, it makes the thread confusing to follow for later readers).

1 Like

Please close this thread. Thanks all for your help. When data is read in server.R it is working fine.