需要社区的一些智慧。
我的目标是构建一个原始的Shiny应用程序,我将在其中插入一些值。 我对SQL并不是很熟悉,所以我偶然发现了。
我有一个远程PostgreSQL数据库并使用Navicat 11。
我的测试数据库只有两列 - " id"和"消息"。 我想通过闪亮的应用程序插入id和消息并远程存储。
我使用了Dean Attali的教程Persistent data storage in Shiny apps。
这是我的代码
# 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()
})
} )
这是我的错误:
postgresqlExecStatement(conn,statement,...)出错:
RS-DBI驱动程序:(无法检索结果:错误:关系&#34; id&#34;不存在
第1行:SELECT * FROM id ^ )
postgresqlQuickSQL中的警告(conn,statement,...):
无法创建执行:SELECT * FROM id *
我明白了,我做了一个错误的SQL查询。 有任何想法吗?真的很感谢你的帮助!
答案 0 :(得分:1)
解决。
# 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) {
psql <- dbDriver("PostgreSQL")
saveData <- function(data) {
# Connect to the database
pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXX", port = XXXX, user
= "UserX", password = "PaswordX")
# Construct the update query by looping over the data fields
query <- paste0("INSERT INTO table_name.schema_name (message) VALUES ( $1
)")
# Submit the update query and disconnect
dbSendQuery(pcon, query, params=data[["message"]])
dbDisconnect(pcon)
}
loadData <- function() {
# Connect to the database
pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXX", port = XXXX, user = "UserX", password = "PaswordX")
# Construct the fetching query
query <- sprintf("SELECT * FROM table_name.schema_name")
# 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()
})
}
)