ERROR: could not find function "ggplot"

1,617 views
Skip to first unread message

Borja Sanz

unread,
Feb 27, 2017, 9:48:53 AM2/27/17
to shinyapps.io Users
Hi.

I'm new with Shiny but not that new with R.
I'm building an app for work that will eventually help with forecasting.
For this, I will need several packages like ggplot2, forecast, etc.

However, I'm having trouble when deploying my app to shinyapps.io (even though it works locally).
I also wanted to use lubridate but it gave me the same error so I used another function. I don't know what I'm missing please help.

Here is my code (sorry for the bad syntax and unnecessary code, I will clean it later)

#Comienzo la aplicación

#Cargo las librerías
require(shiny)
require(rsconnect)
require(ggplot2)

#Cambio el directorio (solo lo uso si lo voy a correr local)
#setwd("C:/Users/borja.sanz/Desktop/Borja/R/Shiny/Directorio - Proyecciones Productos")

#Cargo los datos
data<-read.csv("./data/Pruebas.Shiny.csv")

#Cambio las fechas a formato de fecha
data$Fecha = as.POSIXct(data$Fecha, format = "%d/%m/%Y")

#Extraigo el año
data$Año = as.factor(format(data$Fecha, "%Y"))

#Quito comas de los numeros (si hay)
data$Unidades = as.numeric(gsub(",","",data$Unidades))


#-------------------------------------------------------------------
#Comienzo la interfaz de usuario
ui <-

    shinyUI(tabsetPanel(
   tabPanel("Producto Comparativo",pageWithSidebar(
   headerPanel("Proyecciones de Productos"),
   #Creo los inputs del sidebar
     sidebarPanel(uiOutput("pais"),
                  uiOutput("año"),
                  uiOutput("producto.maestro"),
                  uiOutput("producto"),
                  uiOutput("fecha.promo"),
                  uiOutput("duracion")),
                 
     mainPanel(textOutput("total.unidades"),
               plotOutput("grafica.comparativo"))
   
 )
),
   tabPanel("Producto a proyectar")
)
)

#Comienzo el archivo de servidor


server <- function(input, output) {
require(ggplot2)
require(shiny)
 
 output$pais = renderUI(selectInput(inputId = "pais",
                                    label = "Producto a comparar",
                                    choices = c("Seleccione un país",levels(data$Pais)),
                                    selected = "Seleccione un país")
 )
 
 output$año = renderUI(selectInput(inputId = "año",
                                    label = NULL,
                                    choices = c("Seleccione un año",levels(data$Año)),
                                    selected = "Seleccione un año")
 )
 
 # output$fecha.comparativo = renderUI(
 #   dateRangeInput(inputId = "fecha.comparativo",
 #                  label = "¿Cuándo estuvo activo este producto?",
 #                  start = as.POSIXct(Sys.Date()-365),
 #                  end = as.POSIXct(Sys.Date()-1),  
 #                  min = as.POSIXct("2014-01-01"),
 #                  max = as.POSIXct(Sys.Date() -1),
 #                  format = "dd/mm/yyyy",
 #                  separator = "a",
 #                  language = "es")
 # )
 
 output$producto.maestro = renderUI(
       selectInput(inputId = "producto.maestro",
                       label = NULL,
                       choices = c("Seleccione un producto maestro",levels(factor(data$Producto.Maestro[which(data$Pais == input$pais & data$Año == input$año)]))),
                       selected = "Seleccione un producto maestro")
 )

  output$producto = renderUI(
   if(input$pais == "Seleccione un país" || input$año == "Seleccione un año" || input$producto.maestro == "Seleccione un producto maestro"){return(NULL)
    }else selectInput(inputId = "producto",
                    label = NULL,
                    choices = levels(factor(data$Producto[which(data$Pais == input$pais & data$Año == input$año & data$Producto.Maestro == input$producto.maestro)])),
                    selected = levels(factor(data$Producto[which(data$Pais == input$pais & data$Año == input$año & data$Producto.Maestro == input$producto.maestro)])),
                     multiple = TRUE)
 )
 # output$fecha.promo = renderUI(
 #   dateRangeInput(inputId = "fecha.promo",
 #                  label = "¿Cuándo va a lanzar su promoción?",
 #                  start = as.POSIXct(Sys.Date()),
 #                  end = as.POSIXct(Sys.Date() + 45),  
 #                  min = as.POSIXct(Sys.Date()),
 #                  max = as.POSIXct(Sys.Date() + 365),
 #                  format = "dd/mm/yyyy",
 #                  separator = "a",
 #                  language = "es")
 # )
 # output$duracion = renderText(paste("Duración de la promoción: ",input$fecha.promo[2] - input$fecha.promo[1]," días"))
 
 output$total.unidades = renderText(
   if(input$pais == "Seleccione un país" || input$año == "Seleccione un año" || input$producto.maestro == "Seleccione un producto maestro"){return()
   }else paste("Unidades vendidas del producto comparativo: ", prettyNum(sum(data$Unidades[which(data$Pais == input$pais & data$Año == input$año & data$Producto.Maestro == input$producto.maestro & data$Producto %in% input$producto)]),big.mark = ",")))
 
 df3 = reactive({(subset(data, data$Año == input$año & data$Pais == input$pais & data$Producto.Maestro == input$producto.maestro & data$Producto %in% input$producto))})

  df4 = reactive({(setNames(aggregate(df3()$Unidades, by=list(df3()$Producto.Maestro, df3()$Fecha),FUN =sum), c("Producto.Maestro","Fecha","Unidades")))})

output$grafica.comparativo = renderPlot(
 if(input$pais == "Seleccione un país" || input$producto.maestro == "Seleccione un producto maestro" || is.na(input$producto.maestro)){return()
   } else print(ggplot(df4(), aes(x = Fecha, y = Unidades)) + geom_line(color = "black",size = 1))
 )
}

#Corro la aplicación
shinyApp(ui = ui, server = server)


Joshua Spiewak

unread,
Feb 27, 2017, 10:03:26 AM2/27/17
to shinyapps.io Users
This section of the documentation should be helpful. You need to use library to indicate to rsconnect what packages are being used.

Borja Sanz

unread,
Feb 27, 2017, 10:12:49 AM2/27/17
to shinyapps.io Users
Hi.

I have read this section already but can't seem to fix my code. I have done both require and library but to no avail. I don't know what I'm missing.

Borja Sanz

unread,
Feb 27, 2017, 10:32:36 AM2/27/17
to shinyapps.io Users
This is my sessionInfo() if it helps you:

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1 rsconnect_0.7 shiny_1.0.0  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9      packrat_0.4.8-1  assertthat_0.1   digest_0.6.12    bitops_1.0-6    
 [6] mime_0.5         grid_3.3.2       R6_2.2.0         plyr_1.8.4       jsonlite_1.2    
[11] xtable_1.8-2     gtable_0.2.0     scales_0.4.1     lazyeval_0.2.0   labeling_0.3    
[16] RJSONIO_1.3-0    tools_3.3.2      RCurl_1.95-4.8   munsell_0.4.3    httpuv_1.3.3    
[21] colorspace_1.3-2 htmltools_0.3.5  tibble_1.2  

Joshua Spiewak

unread,
Feb 27, 2017, 10:44:15 AM2/27/17
to shinyapps.io Users
You only need the library calls, not the require calls.
You do not need either for the rsconnect package itself, as it should not be used by your running application.
Message has been deleted

Borja Sanz

unread,
Feb 27, 2017, 11:18:39 AM2/27/17
to shinyapps.io Users
Hi Joshua.

It's not working with library calls, only with require calls (for some reason).

This is the output I get when I deployApp()
Warning message:
In value[[3L]](cond) :
 Failed to parse C:/Users/borja.sanz/AppData/Local/Temp/Rtmpm0vyek/filedf8fdd6ffc/app.R ; dependencies in this file will not be discovered.
Introducir código aquí...

Joshua Spiewak

unread,
Feb 27, 2017, 11:21:05 AM2/27/17
to shinyapps.io Users
In your original post you have require(ggplot2) in two locations, did you remove the first one in addition to changing the second?
Given that you have a message saying it failed to parse app.R, you may be having an issue with rsconnect itself. What is the full output of deploying?

Borja Sanz

unread,
Feb 27, 2017, 11:26:58 AM2/27/17
to shinyapps.io Users
Hi.

I did remove the one that was called outside of the ui and outside of the server and I only left one call to library(ggplot2) inside of server function. When I deployApp() now it tells me "Disconnected from the server"

This is the full output of DeployApp()
Preparing to deploy application...DONE
Uploading bundle for application: 159676...DONE
Deploying bundle: 726855 for application: 159676 ...
Waiting for task: 349328209
  building: Parsing manifest
  building: Building image: 716453
  building: Fetching packages
  building: Installing packages
  building: Installing files
  building: Pushing image: 716453
  deploying: Starting instances
  rollforward: Activating new instances
  terminating: Stopping old instances
Warning message:
In value[[3L]](cond) :
  Failed to parse C:/Users/borja.sanz/AppData/Local/Temp/RtmpiSzsdR/file20149523cd2/app.R ; dependencies in this file will not be discovered.

and these are the logs from shinyapps:
2017-02-27T16:17:08.133898+00:00 shinyapps[159676]:     112: print
2017-02-27T16:17:08.133899+00:00 shinyapps[159676]:     111: renderPlot
2017-02-27T16:17:08.133899+00:00 shinyapps[159676]:     101: <reactive:plotObj>
2017-02-27T16:17:08.133899+00:00 shinyapps[159676]:      90: plotObj
2017-02-27T16:17:08.133900+00:00 shinyapps[159676]:      89: origRenderFunc
2017-02-27T16:17:08.133900+00:00 shinyapps[159676]:      88: output$grafica.comparativo
2017-02-27T16:17:08.133901+00:00 shinyapps[159676]:      12: fn
2017-02-27T16:17:08.133902+00:00 shinyapps[159676]:      11: doTryCatch
2017-02-27T16:17:08.133902+00:00 shinyapps[159676]:       9: tryCatchList
2017-02-27T16:17:08.133903+00:00 shinyapps[159676]:       8: tryCatch
2017-02-27T16:17:08.133903+00:00 shinyapps[159676]:       7: connect$retry
2017-02-27T16:17:08.133903+00:00 shinyapps[159676]:       6: eval
2017-02-27T16:17:08.133904+00:00 shinyapps[159676]:       5: eval
2017-02-27T16:17:08.133901+00:00 shinyapps[159676]:      13: runApp
2017-02-27T16:17:08.133902+00:00 shinyapps[159676]:      10: tryCatchOne
2017-02-27T16:17:08.133905+00:00 shinyapps[159676]:       1: local
2017-02-27T16:17:08.133905+00:00 shinyapps[159676]:       2: eval.parent
2017-02-27T16:17:08.133904+00:00 shinyapps[159676]:       4: eval
2017-02-27T16:17:08.133904+00:00 shinyapps[159676]:       3: eval
2017-02-27T16:23:59.136874+00:00 shinyapps[159676]: Server version: 1.1.0-116
2017-02-27T16:23:59.136922+00:00 shinyapps[159676]: LANG: es_GT.UTF-8
2017-02-27T16:23:59.136925+00:00 shinyapps[159676]: R version: 3.3.2
2017-02-27T16:23:59.136944+00:00 shinyapps[159676]: shiny version: 1.0.0
2017-02-27T16:23:59.136945+00:00 shinyapps[159676]: rmarkdown version: NA
2017-02-27T16:23:59.136971+00:00 shinyapps[159676]: knitr version: NA
2017-02-27T16:23:59.136997+00:00 shinyapps[159676]: jsonlite version: 1.2
2017-02-27T16:23:59.137025+00:00 shinyapps[159676]: RJSONIO version: NA
2017-02-27T16:23:59.137041+00:00 shinyapps[159676]: htmltools version: 0.3.5
2017-02-27T16:23:59.293974+00:00 shinyapps[159676]: Using jsonlite for JSON processing
2017-02-27T16:23:59.317145+00:00 shinyapps[159676]: 
2017-02-27T16:23:59.317151+00:00 shinyapps[159676]: Listening on http://0.0.0.0:43642
2017-02-27T16:23:59.299967+00:00 shinyapps[159676]: Starting R with process ID: '17'
2017-02-27T16:23:59.299965+00:00 shinyapps[159676]: 
2017-02-27T16:24:01.162411+00:00 shinyapps[159676]: Warning: Error in library: there is no package called ‘ggplot2’
2017-02-27T16:24:01.166589+00:00 shinyapps[159676]: Stack trace (innermost first):
2017-02-27T16:24:01.166591+00:00 shinyapps[159676]:     55: library
2017-02-27T16:24:01.166592+00:00 shinyapps[159676]:     54: server [/srv/connect/apps/directorio_-_proyecciones_productos/app.R#50]
2017-02-27T16:24:01.166592+00:00 shinyapps[159676]:     13: runApp
2017-02-27T16:24:01.166593+00:00 shinyapps[159676]:     12: fn
2017-02-27T16:24:01.166593+00:00 shinyapps[159676]:     11: doTryCatch
2017-02-27T16:24:01.166594+00:00 shinyapps[159676]:     10: tryCatchOne
2017-02-27T16:24:01.166594+00:00 shinyapps[159676]:      9: tryCatchList
2017-02-27T16:24:01.166595+00:00 shinyapps[159676]:      8: tryCatch
2017-02-27T16:24:01.166595+00:00 shinyapps[159676]:      7: connect$retry
2017-02-27T16:24:01.166596+00:00 shinyapps[159676]:      5: eval
2017-02-27T16:24:01.166596+00:00 shinyapps[159676]:      4: eval
2017-02-27T16:24:01.166597+00:00 shinyapps[159676]:      3: eval
2017-02-27T16:24:01.166598+00:00 shinyapps[159676]:      2: eval.parent
2017-02-27T16:24:01.166598+00:00 shinyapps[159676]:      1: local
2017-02-27T16:24:01.166795+00:00 shinyapps[159676]: Error in library(ggplot2) : there is no package called ‘ggplot2’
2017-02-27T16:24:01.166595+00:00 shinyapps[159676]:      6: eval

Thank you so much for your help.

Borja Sanz

unread,
Feb 27, 2017, 11:30:18 AM2/27/17
to shinyapps.io Users
And if I require(ggplot2) instead of library(ggplot2) it works (still says "could not find function ggplot" but at least it lets me in the app instead of disconnecting from server)

This is weird.

Joshua Spiewak

unread,
Feb 27, 2017, 11:35:43 AM2/27/17
to shinyapps.io Users
The "warning" about failing to parse app.R is the problem. Because of that, the dependencies are not discovered, and your deployed app does not have ggplot2 available to it.
Could you please open an issue in the rsconnect GitHub repo and include your latest app.R?
It's a wild guess, but perhaps the data$Año and output$año are the trigger for the parsing problem, try changing to data$ano and output$ano and see if that helps.

Borja Sanz

unread,
Feb 27, 2017, 11:46:49 AM2/27/17
to shinyapps.io Users
Wow! That fixed it. 
I would not have thought of that.

Thanks so much Joshua!

Joshua Spiewak

unread,
Feb 27, 2017, 11:51:18 AM2/27/17
to shinyapps.io Users
You are very welcome.
I created an issue, in case you want to add more information, or follow it.
Reply all
Reply to author
Forward
0 new messages