Publishing conundrum

Hi I'd be so grateful for any insights into this issue.
I have created my first useful Shiny app - it works locally, and I can publish the built-in Geyser app. But if I publish my own app, I get: "An error has occurred, The application failed to start. exit status 1" (after a decent wait).
Any idea what might be happening? Here's the R code:

library(shiny)
library(DBI)
library(RMySQL)
library(DT)
library(writexl)
setwd("H:/My Drive/Dashboard/Shiny/SpecificSearch/VID_Result_List")

# Define UI for the app
ui <- fluidPage(
  
  # App title
  titlePanel("VID Search"),
  
  # Main panel for displaying outputs
  mainPanel(
    
    # Output: DataTable
    DTOutput("vidTable"),
    
    # Download controls
    radioButtons(inputId = "filetype",
                 label = "File type:",
                 choices = c("CSV" = "csv", "Excel" = "xlsx"),
                 selected = "csv"),
    downloadButton(outputId = "downloadData", label = "Download Data")
  )
)

# Define server logic
server <- function(input, output, session) {
  
  # Database connection details
  con <- dbConnect(RMySQL::MySQL(), 
                   dbname = "****", 
                   host = "****",
                   port = 3306,
                   user = "*****",
                   password = "*****")
  
  # Initial data query
  query <- "SELECT idIncident,
       dateincident AS `Incident Date`, 
       title AS Title, 
       countryname AS Country, 
       statename AS `State/Province`, 
       cityname AS City, 
       otherDireccion AS `Other Address`, 
       driverperse AS `Responsible Actor Category`, 
       victimreligion AS `Religion of Victim(s)`
FROM tbl_incident2 
WHERE idStatus = 0 AND securitylevel = 'Public'
ORDER BY idIncident DESC;"
  
  VID <- dbGetQuery(con, query)
  
  # Close the database connection when the app stops
  onStop(function() {
    dbDisconnect(con)
  })
  
  # Render DataTable
  output$vidTable <- renderDT({
    datatable(VID,
              options = list(pageLength = 10),
              rownames = FALSE,
              extensions = 'Buttons',
              selection = 'none',
              class = 'cell-border stripe',
              style = 'bootstrap',
              width = "100%"
    ) 
  })
  
  # Download handler
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("VID_report-", Sys.Date(), ".", input$filetype, sep = "")
    },
    content = function(file) {
      if (input$filetype == "csv") {
        write.csv(VID, file, row.names = FALSE)
      } else {
        write_xlsx(VID, file)
      }
    }
  )
}


# Run the application
shinyApp(ui = ui, server = server)

You did not specify where you are publishing the app, but my initial guess is that the "H" drive does not exist/is not accessible on the server where the app is being published. One way to test that would be to comment out everything other than the structural stuff (server and ui functions) and setwd command, creating an app that just has an empty UI (give or take a title and maybe a "Hello world" text message) and see if it still fails to publish.

2 Likes

The path you reference in setwd() means nothing on the shiny server you published to. It is probably throwing an error there. Just comment out that line. It is good to avoid using setwd() at all, but especially in a shiny app that you want to run on some other machine (server or someone else's machine). On the server, the working directory will be the folder where 'app.R' is. No need to try to set it. TIP: You can and should avoid setwd() in all your R work by always containing your R work in a folder, and making that folder an R Project. Do this in RStudio with File>>New Project>>browse to your working folder>>OK. This will create a .RProj file in that directory and you no longer need to state what the working directory is. Using setwd() causes lots of issues like this.

1 Like

Thank you Peernisse and Prubin, you were both on the right track. The reason I thought I had to do a setwd was because without it the publish window would error when it summed the data in my default wd which was not the directory of the app (much higher up). This default wd is set in the global settings. I have since reset that to my app's directory and although clearly a workaround for now, it got be an inch closer: the app published but fails to access the database online. This may be due to it applying a new IP address when accessing the MySQL database. In the meantime, working with a csv file in the same directory is working. VID Search

Thanks so much for your help,
John

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.