From f274f623967e4983eacb89b45360c889e4f89536 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Fri, 17 Nov 2023 16:07:17 -0600 Subject: [PATCH 1/2] separate & explain the new relational examples For something on the package's readme, it slowed me down. --- README.Rmd | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.Rmd b/README.Rmd index dc65efdb..d0481309 100644 --- a/README.Rmd +++ b/README.Rmd @@ -184,17 +184,20 @@ This package also provides generics, for which other packages may then implement ```{r extensibility} library(duckplyr) +# Create a relational to be used by examples below new_dfrel <- function(x) { stopifnot(is.data.frame(x)) new_relational(list(x), class = "dfrel") } mtcars_rel <- new_dfrel(mtcars[1:5, 1:4]) +# Example 1: return a data.frame rel_to_df.dfrel <- function(rel, ...) { unclass(rel)[[1]] } rel_to_df(mtcars_rel) +# Example 2: A (random) filter rel_filter.dfrel <- function(rel, exprs, ...) { df <- unclass(rel)[[1]] @@ -213,6 +216,7 @@ rel_filter( ) ) +# Example 3: A custom projection rel_project.dfrel <- function(rel, exprs, ...) { df <- unclass(rel)[[1]] @@ -226,6 +230,7 @@ rel_project( list(relexpr_reference("cyl"), relexpr_reference("disp")) ) +# Example 4: A custom ordering (eg, ascending by mpg) rel_order.dfrel <- function(rel, exprs, ...) { df <- unclass(rel)[[1]] @@ -238,6 +243,8 @@ rel_order( mtcars_rel, list(relexpr_reference("mpg")) ) + +# Example 5: A custom join rel_join.dfrel <- function(left, right, conds, join, ...) { left_df <- unclass(left)[[1]] right_df <- unclass(right)[[1]] @@ -251,6 +258,7 @@ rel_join.dfrel <- function(left, right, conds, join, ...) { rel_join(new_dfrel(data.frame(mpg = 21)), mtcars_rel) +# Example 6: Limit the maximum rows returned rel_limit.dfrel <- function(rel, n, ...) { df <- unclass(rel)[[1]] @@ -259,6 +267,8 @@ rel_limit.dfrel <- function(rel, n, ...) { rel_limit(mtcars_rel, 3) +# Example 7: Suppress duplicate rows +# (ignoring row names) rel_distinct.dfrel <- function(rel, ...) { df <- unclass(rel)[[1]] @@ -267,6 +277,7 @@ rel_distinct.dfrel <- function(rel, ...) { rel_distinct(new_dfrel(mtcars[1:3, 1:4])) +# Example 8: Return column names rel_names.dfrel <- function(rel, ...) { df <- unclass(rel)[[1]] From c73ef0362c3d83de3d5ab3fb848ba31626243125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 18 Nov 2023 12:46:48 +0100 Subject: [PATCH 2/2] Render --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index ced2293c..bddf509a 100644 --- a/README.md +++ b/README.md @@ -288,12 +288,14 @@ This package also provides generics, for which other packages may then implement
 library(duckplyr)
 
+# Create a relational to be used by examples below
 new_dfrel <- function(x) {
   stopifnot(is.data.frame(x))
   new_relational(list(x), class = "dfrel")
 }
 mtcars_rel <- new_dfrel(mtcars[1:5, 1:4])
 
+# Example 1: return a data.frame
 rel_to_df.dfrel <- function(rel, ...) {
   unclass(rel)[[1]]
 }
@@ -305,6 +307,7 @@ This package also provides generics, for which other packages may then implement
 #> Hornet 4 Drive    21.4   6  258 110
 #> Hornet Sportabout 18.7   8  360 175
 
+# Example 2: A (random) filter
 rel_filter.dfrel <- function(rel, exprs, ...) {
   df <- unclass(rel)[[1]]
 
@@ -331,6 +334,7 @@ This package also provides generics, for which other packages may then implement
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
 
+# Example 3: A custom projection
 rel_project.dfrel <- function(rel, exprs, ...) {
   df <- unclass(rel)[[1]]
 
@@ -354,6 +358,7 @@ This package also provides generics, for which other packages may then implement
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
 
+# Example 4: A custom ordering (eg, ascending by mpg)
 rel_order.dfrel <- function(rel, exprs, ...) {
   df <- unclass(rel)[[1]]
 
@@ -376,6 +381,8 @@ This package also provides generics, for which other packages may then implement
 #> 
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
+
+# Example 5: A custom join
 rel_join.dfrel <- function(left, right, conds, join, ...) {
   left_df <- unclass(left)[[1]]
   right_df <- unclass(right)[[1]]
@@ -398,6 +405,7 @@ This package also provides generics, for which other packages may then implement
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
 
+# Example 6: Limit the maximum rows returned
 rel_limit.dfrel <- function(rel, n, ...) {
   df <- unclass(rel)[[1]]
 
@@ -414,6 +422,8 @@ This package also provides generics, for which other packages may then implement
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
 
+# Example 7: Suppress duplicate rows
+#  (ignoring row names)
 rel_distinct.dfrel <- function(rel, ...) {
   df <- unclass(rel)[[1]]
 
@@ -429,6 +439,7 @@ This package also provides generics, for which other packages may then implement
 #> attr(,"class")
 #> [1] "dfrel"      "relational"
 
+# Example 8: Return column names
 rel_names.dfrel <- function(rel, ...) {
   df <- unclass(rel)[[1]]