3

I need some clarification on how to properly send queries to my database within RShiny...

I have build-up an application in which anyone can create an account and then write some informations in a dataframe before saving those rows to my database.

The app works perfectly well when testing my it with a single user but shows some issues when several users send data to my database at the same time. All the informations sends are duplicated 2 to 10 times in postgresql...

For instance if I add an unique observation of 5 individuals of species "A" with an observation date on the 25th of february I will get 3 rows (sometimes it can be up to 10 duplicates) in my database instead of one. (like shown in the table below):

ID species      date       number     username   latitude    longitude
1     A     2022-02-25       5        Wanderzen   45.2         2.6
2     A     2022-02-25       5        Wanderzen   45.2         2.6
3     A     2022-02-25       5        Wanderzen   45.2         2.6

It's the first time I'm building a Shiny App interacting with a database and I'm pretty sure I'm not using the pool package properly...

** What have I to do to solve this issue ? Shall I open and close a connection for each query ?**

Here is a coarce code sample that shows my problem:

library(shiny)
library(leaflet)
library(pool)
library(DT)
library(shinycssloaders)
library(RPostgres)
library(shinyjs)

pool <- DBI::dbConnect(
  drv = dbDriver("PostgreSQL"),
  dbname = "my_database",
  host = "99.99.999.999",
  user = Sys.getenv("userid"),
  password = Sys.getenv("pwd")
)

ui <- fluidPage(
  fluidRow(column(width=10,
                  wellPanel(
                    leafletOutput(outputId = "map", height = 470) %>% withSpinner(color="#000000"),
                    wellPanel(useShinyjs(),
                      fluidRow(DT::dataTableOutput(outputId ="obs_user") %>% withSpinner(color="#000000"))
                    )))))

server <-  function(input, output, session){

  values <- reactiveVal(NULL)
  observe({
    invalidateLater(1000)
    query <- "select species, date, number, username, latitude, longitude from rshiny.data"
    ret <- dbGetQuery(pool, query)
    values(ret)})
  
  
  dataframe1 <- reactiveValues(species = character(), date= character(), number = integer(), username=character(), latitude=numeric(), longitude=numeric())
  
  observeEvent(input$map_click, {
    click <- input$map_click
    showModal(modalDialog(title = "add a new observation",
                          selectInput("species", "Species", choices = ''),
                          dateInput("date", "Observation date:"),
                          numericInput("number", "Number:",1),  
                          textInput("username", "Username:"), 
                          textInput("latitude", "Latitude:",click$lat), 
                          textInput("longitude", "Longitude:",click$lng),
                          actionButton(inputId = "save_BDD",label = "Send to the database", style = "width:250px",
                                       easyClose = TRUE, footer = NULL )))})
  
  observeEvent(input$map_click, {
    shinyjs::disable("latitude")
    shinyjs::disable("longitude")
  })
  
  
  observeEvent(input$save_BDD, {
    dataframe1$dm <- isolate({
      newLine <- data.frame(species=input$species, 
                            date=input$date,
                            number = input$number,
                            username = input$username,
                            latitude = input$latitude,
                            longitude =input$longitude)
      rbind(dataframe1 $dm,newLine)})})
  
  
  observeEvent(input$save_BDD,{
    dbWriteTable(pool, c("rshiny", "data"), dataframe1$dm, row.names=FALSE, append = T)
    dbExecute(pool, "UPDATE rshiny.data SET geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326);")})
  
  
  output$map <- renderLeaflet({
    leaflet(data=values()) %>%
      addTiles(group = "OSM") %>%
      addAwesomeMarkers(data = values(),
                        lng = ~as.numeric(longitude), lat = ~as.numeric(latitude)) %>%
      addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery") })
  
  output$obs_user <-  DT::renderDataTable({
    datatable(values())})
  
}

shinyApp(ui, server)
wanderzen
  • 119
  • 2
  • 12
  • 1
    There are mutliple things you need to sort out before we can help you: 1) You should not use the packages `RPostgreSQL` and `RPostgres` together, one of them is sufficient. Choose one. 2) The dataframe you want to write, `dataframe1$dm`, is not defined in your code. 3) What is the expected result in the database? 4) How is the respective table defined in the database? – AEF Mar 07 '22 at 11:55
  • Thanks for your reply AEF. I forgot to change the name of the dataframe when I created the code sample my question but I've edited it now. Regarding the expected result I just want to avoid duplicates when several users use my application (as shown in the dummy table in my question) – wanderzen Mar 07 '22 at 13:07
  • 1
    It is still very unclear to me what you would like to achieve. What should happen if a user wants to write a row that has already been written? Duplication should in any case be prevented in the database if that's needed and not (only) in R. – AEF Mar 07 '22 at 13:41
  • 1
    Also, your code as it is now does not even start. Not only is there a wild mixture of `dataframe1` and `empty_dataframe_test2`, but also both of those are used to access a column `dm` that does seemingly not exist. – AEF Mar 07 '22 at 13:43

1 Answers1

3

Below please find a reproducible example using library(RSQLite) - just switch back to your postgres connection / schema.

I don't think the issue is pool related. I guess (I can't verify without your DB) your call to rbind is problematic - as it sends multiple lines if the reactiveVal was used before.

Furthermore, in a case like this it is much more efficient to create a cross-session reactive (here reactivePoll) to share the DB information among sessions, instead of having each session query the DB every second.

library(shiny)
library(leaflet)
library(pool)
library(DT)
library(shinycssloaders)
library(RPostgres)
library(shinyjs)

library(RSQLite) # for MRE only

# pool <- DBI::dbConnect(
#   drv = Postgres(),
#   dbname = "my_database",
#   host = "99.99.999.999",
#   user = Sys.getenv("userid"),
#   password = Sys.getenv("pwd")
# )

# local postgres test:
# pool <- DBI::dbConnect(
#   drv = Postgres(),
#   dbname = "test",
#   host = "localhost",
#   user = "postgres",
#   password = "postgres"
# )

pool <- dbConnect(RSQLite::SQLite(), ":memory:")

# cross-session reactivePoll
RP <- reactivePoll(intervalMillis = 1000, session = NULL, checkFunc = function(){
  if (dbIsValid(pool) && dbExistsTable(pool, "dbtable")) {
    query <- "SELECT count(*) FROM dbtable;"
    dbGetQuery(pool, query)[[1]]
  } else {
    0L
  }
}, valueFunc = function(){
  if (dbIsValid(pool) && dbExistsTable(pool, "dbtable")) {
    query <- "SELECT species, date, number, username, latitude, longitude FROM dbtable;"
    dbGetQuery(pool, query)
  } else {
    NULL
  }
})


ui <- fluidPage(fluidRow(column(
  width = 10,
  wellPanel(
    leafletOutput(outputId = "map", height = 470) %>% withSpinner(color = "#000000"),
    wellPanel(useShinyjs(),
              fluidRow(
                DT::dataTableOutput(outputId = "obs_user") %>% withSpinner(color = "#000000")
              ))
  )
)))

server <- function(input, output, session) {
  
  observeEvent(input$map_click, {
    click <- input$map_click
    showModal(
      modalDialog(
        title = "add a new observation",
        selectInput("species", "Species", choices = ''),
        dateInput("date", "Observation date:"),
        numericInput("number", "Number:", 1),
        textInput("username", "Username:"),
        textInput("latitude", "Latitude:", click$lat),
        textInput("longitude", "Longitude:", click$lng),
        actionButton(
          inputId = "save_BDD",
          label = "Send to the database",
          style = "width:250px",
          easyClose = TRUE,
          footer = NULL
        )
      )
    )
  })
  
  observeEvent(input$map_click, {
    shinyjs::disable("latitude")
    shinyjs::disable("longitude")
  })
  
  observeEvent(input$save_BDD, {
    newLine <- data.frame(
      species = input$species,
      date = input$date,
      number = input$number,
      username = input$username,
      latitude = input$latitude,
      longitude = input$longitude
    )
    
    if (dbExistsTable(pool, "dbtable")) {
      dbWriteTable(pool,
                   "dbtable",
                   newLine,
                   row.names = FALSE,
                   append = TRUE,
                   overwrite = FALSE)
    } else {
      dbWriteTable(pool,
                   "dbtable",
                   newLine,
                   row.names = FALSE,
                   append = FALSE,
                   overwrite = TRUE)
    }
    # dbExecute(pool, "UPDATE rshiny.data SET geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326);")
    removeModal(session)
  })
  
  output$map <- renderLeaflet({
    if(!is.null(RP())){
      leaflet(data = RP()) %>%
        addTiles(group = "OSM") %>%
        addAwesomeMarkers(
          data = RP(),
          lng = ~ as.numeric(longitude),
          lat = ~ as.numeric(latitude)
        ) %>%
        addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery")
    } else {
      leaflet() %>%
        addTiles(group = "OSM") %>%
        addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery")
    }
  })
  
  output$obs_user <-  DT::renderDataTable({
    req(RP())
    datatable(RP())
  })
}

shinyApp(ui, server, onStart = function() {
  cat("Doing application setup\n")
  onStop(function() {
    cat("Doing application cleanup\n")
    dbDisconnect(pool)
    # poolClose(pool)
  })
})

Multi-session usage: result

To avoid duplicated entries from the DB perspective please use table constraints. You could create a primary key spanning all (ID) relevant columns of the table.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Thanks for your detailled reply @ismirsehregal and the hint concerning the issue with the `rbind()` function. However my app stop working and throw the following log message: `unable to find an inherited method for function ‘dbWriteTable’ for signature ‘"PostgreSQLConnection", "character", "reactivevalues"’ ` when I replace `dataframe1$dm <- isolate({newLine <- data.frame(` by `newLine <- data.frame(`. :( – wanderzen Mar 07 '22 at 15:32
  • @wanderzen please copy my code and only change the connection. I just checked with a local PostgreSQL server and it is working fine. – ismirsehregal Mar 07 '22 at 15:54
  • 1
    You were right: rbind was the issue. I've changed ```newLine <- data.frame(``` by ```newLine <- isolate({data.frame(``` and it seems to be working with 5 users saving data at the same time :) ! Thanks for your help ! – wanderzen Mar 08 '22 at 11:27