1

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!

1 Answer 1

1

You need to create the R object outside of the renderRpivotTable function.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.