How to load data from postgreSQL within a shiny app deployed to Shiny server?

Viewed 114

Following those explanations here and here I've been able to install shiny server.

Now I would like to deploy a shiny application on this shiny server. I did some tests with shapefile, csv and so on and everything works well. It gets tricky when I try to deploy an app that fetchs tables from postgreSQL. My app is deployed but the screen turns greyish and there is a message at the left bottom part of the page saying "Disconnected from the server. Reload"

I tried to look at the log messages in /var/log/shiny-server but the folder remains empty.

I really don't know what to do... Any useful help would be greatly appreciated...

Here is a simplified sample of my app:

UI:

library(shiny)
library(shinydashboard)
library(tidyr)
library(dplyr)
library(leaflet)
library(shinyWidgets)
library(shinycustomloader)
library(shinydashboardPlus)
library(raster)
library(DT)
library(sf)
library(RPostgreSQL)
library(DT)
library(pool)
library(shinythemes)
library(odbc)
library(RODBC)

shinyUI(navbarPage(id="navbarid",
                   "My shiny App",  theme = shinytheme("cosmo"),
                   header = tagList(
                     useShinydashboard()),
        tabPanel("Analysis", value="analysis",
                fluidRow(leafletOutput(outputId = "map", height = 470),
                      DT::dataTableOutput(outputId ="table1"))))) 

Server:

library(shiny)
library(shinydashboard)
library(tidyr)
library(dplyr)
library(leaflet)
library(shinyWidgets)
library(shinycustomloader)
library(shinydashboardPlus)
library(raster)
library(DT)
library(sf)
library(RPostgreSQL)
library(DT)
library(pool)
library(shinythemes)
library(odbc)
library(RODBC)


shinyServer(function(input, output, session){
  
  pool <- dbConnect(odbc::odbc(),
                    Driver = "PostgreSQL",  
                    Database = "db_name",
                    Server  = "my_server",
                    UID  = "user",
                    PWD  = "password",
                    port = "5432")
  
  test <- dbGetQuery(pool, "SELECT * FROM myschema.mytable")

  # https://rstudio.github.io/DT/options.html
  output$table1<-  DT::renderDataTable({
    datatable(test,
              selection = 'single',
              rownames = FALSE, 
              editable = TRUE,
              extensions = c("Scroller"), 
              filter = 'top') })
  

  output$map <- renderLeaflet({
    leaflet(test) %>%
      addMarkers(lng = ~as.numeric(longitude), lat = ~as.numeric(latitude))  %>%
      addTiles(group = "OSM par défaut") %>%
      addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery")
  })
})```
0 Answers
Related