如何将RpostgreSQL查询设置为RpivotTable的数据框对象?

时间:2018-12-26 07:14:55

标签: r shiny rpostgresql rpivottable

我正在尝试使用RpostgreSQL来获取要馈入rpivotTable程序包并使用Shiny显示给用户的数据。

错误:数据应该是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形式的Titanic数据框以闪亮的形式显示数据透视表。

我只是想不出如何将两者结合在一起,并使用postgres sql查询以闪亮的方式显示可旋转数据表。

我在DEBUGGING部分中拆分了成功的示例,并尝试了一些失败的尝试

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要在renderRpivotTable函数之外创建R对象。

server.R

library(shiny)
library(rpivotTable)
library(RPostgreSQL)

function(input, output, session) {

  conn <- dbConnect(
    drv = dbDriver("PostgreSQL"),
    dbname = "store",
    host = "localhost",
    user = "postgres",
    password = "123456")

  on.exit(dbDisconnect(conn), add = TRUE)

  db_data <- dbGetQuery(conn, paste0("SELECT * FROM orders;"))

  output$pivot <- renderRpivotTable({
    rpivotTable(data = db_data)

  })

}

ui.R

library(shiny)
library(rpivotTable)

test_page <- fluidPage(
  rpivotTableOutput('pivot')
)

test_page