I am trying to use RpostgreSQL to source data to be fed into the rpivotTable package and displayed to the user using shiny.
Error: data should be a data.frame, data.table, or table
Unfortunately I am new to R and I am unable to work out how to set my postgreSQL query to be set as a dataframe, table or data.table so that a rpivotTable can be created.
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)
I am able to display the table from postgres sql in the "tbl" format in shiny and a pivottable using the Titanic dataframe in R in shiny.
I just can not work out how to combine the two together and use the postgres sql query to display an rpivottable in shiny.
I`ve split my successful examples in the DEBUGGING section and my weak failed attempts
Thanks for your help!