我正在尝试使用 RpostgreSQL 来获取要输入 rpivotTable 包并使用闪亮显示给用户的数据。
错误:数据应该是 data.frame、data.table 或 table
不幸的是,我是 R 新手,我无法弄清楚如何将我的 postgreSQL 查询设置为数据框、表或 data.table,以便可以创建 rpivotTable。
library(shiny)
library(DBI)
library(RPostgreSQL)
library(DT)
library(plotly)
library(rjson)
library(pool)
library(dplyr)
library(dbplyr)
library(rpivotTable)
ui <- fluidPage(
##DEBUGGING##
tableOutput("tbl"),
rpivotTable(Titanic),
### sql & rpivottable attempts ###
rpivotTable("OverallPivot"),
rpivotTableOutput("tbl2"),
output$pivtbl2 <- renderRpivotTable(rpivotTable(data = DataSet,
aggregatorName = "Sum",
vals = "Count",
cols = "order_date",
rows = "product_id",
menuLimit = 1200,
rendererName = "Line Chart"))
)
server <- function(input, output, session) {
###DEBUGGING SERVER
output$tbl <- renderTable({
conn <- dbConnect(
drv = dbDriver("PostgreSQL"),
dbname = "store",
host = "localhost",
user = "postgres",
password = "123456")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT * FROM orders;"))
})
###Attempted sql & rpivotTable attempts SERVER
OverallPivot <- renderRpivotTable({ conn <- dbConnect(
drv = dbDriver("PostgreSQL"),
dbname = "store",
host = "localhost",
user = "postgres",
password = "123456")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT * FROM orders;"))
})
DataSet <- renderRpivotTable({ conn <- dbConnect(
drv = dbDriver("PostgreSQL"),
dbname = "store",
host = "localhost",
user = "postgres",
password = "123456")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT * FROM orders;"))
})
tbl2 <- renderRpivotTable({
conn <- dbConnect(
drv = dbDriver("PostgreSQL"),
dbname = "store",
host = "localhost",
user = "postgres",
password = "123456")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT * FROM orders;"))
})
}
shinyApp(ui, server)
我能够以闪亮的“tbl”格式显示来自postgres sql的表,并使用闪亮的R中的泰坦尼克数据框显示数据透视表。
我只是无法弄清楚如何将两者结合在一起并使用 postgres sql 查询以闪亮的方式显示 rpivottable。
我已经在调试部分中拆分了我的成功示例和我失败的失败尝试
谢谢你的帮助!