need some wisdom from the community.
My objective is to build a primitive Shiny app, where I will insert some values. I am not super familiar with SQL, so I stumbled.
I have a remote PostgreSQL database and use Navicat 11.
My test database has only two columns - "id" and "message". I want to insert id and message through shiny app and store it remotely.
I used a tutorial Persistent data storage in Shiny apps by Dean Attali.
This is my code
# Set libraries
library(RPostgreSQL)
library(shiny)
# Define the fields we want to save from the form
fields <- c("id", "message")
# Shiny app with two fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
textInput("id", "ID", ""),
textInput("message", "MESSAGE", ""),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
databaseName <- "XXXXX"
table <- "XX_XXX"
psql <- dbDriver("PostgreSQL")
saveData <- function(data) {
# Connect to the database
pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXXXX", port = XXXX, user = "XXXX", password = "XXXXX")
# Construct the update query by looping over the data fields
query <- sprintf(
"INSERT INTO id (id) VALUES ('message')",
table,
paste(names(data), collapse = ", "),
paste(data, collapse = "', '")
)
# Submit the update query and disconnect
dbGetQuery(pcon, query)
dbDisconnect(pcon)
}
loadData <- function() {
# Connect to the database
pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXXXX", port = XXXX, user = "XXXX", password = "XXXXX")
# Construct the fetching query
query <- sprintf("SELECT * FROM id", table)
# Submit the fetch query and disconnect
data <- dbGetQuery(pcon, query)
dbDisconnect(pcon)
data
}
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
} )
This is my error:
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: relation "id" does not exist
LINE 1: SELECT * FROM id ^ )
Warning in postgresqlQuickSQL(conn, statement, ...) :
Could not create execute: SELECT * FROM id*
I as understand, I do a wrong sql query. Any ideas? Really appreciate you help!
idcolumn, when they should be referencing the table. Is the table obfuscated (XX_XXX)? Either way, the SQL would be something likeINSERT INTO "XX_XXX" VALUES (...), and the SELECT would beSELECT * FROM "XX_XXX"(substitute the real table name if XX_XXX is not the real name). - bmaresponses, so maybe try the same code in that article, eg."INSERT INTO %s (%s) VALUES ('%s')"- bmapsqlPostgres client, or PGAdmin, or whatever GUI you have at hand to query your Postgres databases. Once you have proven your SQL works, then you can run it in your app, with a better idea of what the failures mean. - bma