Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing dbplyr lazy table as input data for PivotTable #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HafizKhusyairi
Copy link

@HafizKhusyairi HafizKhusyairi commented Jul 10, 2024

Issue #67 dbplyr processing library

Description

dbplyr uses dplyr syntax, so I don't think we need an additional processing library. By expanding the allowable class for the input data to include dbplyr lazy data that represent database table/query and adding the instruction to download the database calculation to R, pivottabler does work with table on databases.

The following code shows how a table in a database (SQLite) can be used to calculate a pivottable. I have also run a similar code using table in my organisation database (in Snowflake)

`library(dplyr)
library(dbplyr)
con <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:")
DBI::dbWriteTable(con, "CO2", as.data.frame(CO2))
co2_db <- tbl(con, "CO2")

pt <- PivotTable$new()
pt$addData(co2_db)
pt$addRowDataGroups("Plant")
pt$addColumnDataGroups("Type")
pt$addColumnDataGroups("Treatment")
pt$defineCalculation(calculationName="TotalUptake", summariseExpression="sum(uptake)")
pt$renderPivot()`

Changes made

  1. Allowing object of class tbl_lazy or tbl_dbi as an input to addData() and other relevant functions.
  2. Collecting the calculation result from the database

Checklist

Output of devtools::test()
ℹ Testing pivottabler
✔ | F W S OK | Context
✔ | 8 | BASIC TESTS [1.4s]
✔ | 5 | SMOKE TESTS [1.5s]
✔ | 8 | ILLEGAL NAME TESTS [2.4s]
✔ | 4 | QUICK-PIVOT TESTS [1.8s]
✔ | 24 | BASIC LAYOUT TESTS [2.1s]
✔ | 10 | DATA GROUP TESTS [3.8s]
✔ | 2 8 | CALCULATION TESTS [4.2s]
──────────────────────────────────────────────────────────────────────────────────────────────────────────────
Warning (test-A-07-calculationTests.R:80): calculation tests: calculate dply summarise: batch dplyr
package 'dplyr' was built under R version 4.2.3
Backtrace:

  1. base::library(dplyr)
    at test-A-07-calculationTests.R:80:6
  2. base (local) testRversion(pkgInfo, package, pkgpath)

Warning (test-A-07-calculationTests.R:81): calculation tests: calculate dply summarise: batch dplyr
package 'lubridate' was built under R version 4.2.3
Backtrace:

  1. base::library(lubridate)
    at test-A-07-calculationTests.R:81:6
  2. base (local) testRversion(pkgInfo, package, pkgpath)
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────
    ✔ | 2 | VISUAL TOTALS TESTS [0.5s]
    ✔ | 9 | THEMING TESTS [0.7s]
    ✔ | 2 | EMPTY GROUP TESTS [0.1s]
    ✔ | 1 6 | EXPORT TESTS [2.1s]
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────
    Warning (test-B-12-exportTests.R:284): export tests: as basictable: batch dplyr
    package 'basictabler' was built under R version 4.2.3
    Backtrace:
  3. base::library(basictabler)
    at test-B-12-exportTests.R:284:6
  4. base (local) testRversion(pkgInfo, package, pkgpath)
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────
    ✔ | 6 | LATEX TESTS [1.3s]
    ✔ | 6 | EXPORT OPTIONS TESTS [1.1s]
    ✔ | 6 | FIND GROUP TESTS [1.0s]
    ✔ | 4 | GET CELLS TESTS [2.5s]
    ✔ | 15 | FIND CELLS TESTS [2.1s]
    ✔ | 4 | IRREGULAR LAYOUT TESTS [0.5s]
    ✔ | 4 | CALCULATION CUSTOM FORMATTING TESTS [0.4s]
    ✔ | 4 | DATA GROUP CUSTOM FORMATTING TESTS [0.8s]
    ✔ | 3 | ALTERNATIVE LAYOUT TESTS [2.1s]
    ✔ | 2 | ROW GROUP HEADER BASIC LAYOUT TESTS [0.2s]
    ✔ | 4 | OUTLINE LAYOUT TESTS [1.8s]
    ✔ | 26 | PIVOT ROW/COLUMN TESTS [1.6s]
    ✔ | 9 | MAP STYLING TESTS [1.3s]
    ✔ | 12 | INTERSECTION TESTS
    ✔ | 12 | UNION TESTS
    ✔ | 8 | UNLIST TESTS

══ Results ═══════════════════════════════════════════════════════════════════════════════════════════════════
Duration: 41.0 s

[ FAIL 0 | WARN 3 | SKIP 0 | PASS 211 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant