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)