Skip to content

Commit

Permalink
refactor: rewrite active bindings and sub-namespaces (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Feb 20, 2024
1 parent c2ef53b commit 41e3512
Show file tree
Hide file tree
Showing 55 changed files with 783 additions and 1,041 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ Collate:
'rust_result.R'
's3_methods.R'
'series__series.R'
'series__string.R'
'sql.R'
'vctrs.R'
'zzz.R'
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ S3method("$",RPolarsRThreadHandle)
S3method("$",RPolarsRollingGroupBy)
S3method("$",RPolarsSQLContext)
S3method("$",RPolarsSeries)
S3method("$",RPolarsSeriesStrNameSpace)
S3method("$",RPolarsStringCacheHolder)
S3method("$",RPolarsThen)
S3method("$",RPolarsVecDataFrame)
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
- New active bindings `$flags` for `DataFrame` to show the flags used internally
for each column. The output of `$flags` for `Series` was also improved and now
contains `FAST_EXPLODE` for `Series` of type `list` and `array` (#809).
- Add string methods `to_lowercase()` and `to_uppercase()` for `Series` (#810).
- `$str` sub namespace is added to `Series` (#819).
- `as_polars_df()` for `data.frame` is more memory-efficient and new arguments
`schema` and `schema_overrides` are added (#817).
- Use `polars_code_completion_activate()` to enable code suggestions and
autocompletion after `$` on polars objects. This is an experimental feature
that is disabled by default. For now, it is only supported in the native R
terminal and in RStudio (#597).

### Bug fixes

- `<Series>$list` sub namespace methods returns `Series` class object correctly (#819).

## Polars R Package 0.14.0

### Breaking changes due to Rust-polars update
Expand Down
85 changes: 49 additions & 36 deletions R/Field.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
#' Create Field
#'
#' A Field is composed of a name and a DataType. Fields are used in Structs-
#' datatypes and Schemas to represent everything of the Series/Column except the
#' raw values.
#' A Field is composed of a name and a data type.
#' Fields are used in Structs-datatypes and Schemas to represent everything of
#' the Series/Column except the raw values.
#'
#' @name RField_class
#' @section Active Bindings:
#'
#' ## datatype
#'
#' `$datatype` returns the [data type][pl_dtypes] of the Field.
#'
#' `$datatype = <RPolarsDataType>` sets the [data type][pl_dtypes] of the Field.
#'
#' ## name
#'
#' `$name` returns the name of the Field.
#'
#' `$name = "new_name"` sets the name of the Field.
#'
#' @aliases RField_class RPolarsRField
#'
#' @param name Field name
#' @param datatype DataType
#' @param datatype [DataType][pl_dtypes]
#'
#' @include after-wrappers.R
#' @return A object of with DataType `"RField"` containing its name and its
#' DataType.
#' @return An [RPolarsRField] object containing its name and its
#' [data type][pl_dtypes].
#' @examples
#' pl$Field("city_names", pl$String)
#' field = pl$Field("city_names", pl$String)
#'
#' field
#' field$datatype
#' field$name
#'
#' # Set the new data type
#' field$datatype = pl$Categorical
#' field$datatype
#'
#' # Set the new name
#' field$name = "CityPoPulations"
#' field
pl_Field = function(name, datatype) {
.pr$RField$new(name, datatype)
}


# Active bindings

RField_name = method_as_active_binding(
\() .pr$RField$get_name(self),
setter = TRUE
)


RField_datatype = method_as_active_binding(
\() .pr$RField$get_datatype(self),
setter = TRUE
)


#' S3 method to print a Field
#'
#' @noRd
Expand Down Expand Up @@ -67,42 +106,16 @@ RField_print = function() {
RField.property_setters = new.env(parent = emptyenv())


#' Get/set Field name
#'
#' @rdname RField_name
#' @examples
#' field = pl$Field("Cities", pl$String)
#' field$name
#'
#' field$name = "CityPoPulations" #<- is fine too
#' field
RField_name = method_as_property(function() {
.pr$RField$get_name(self)
}, setter = TRUE)

RField.property_setters$name = function(self, value) {
.pr$RField$set_name_mut(self, value)
}

#' Get/set Field datatype
#'
#' @rdname RField_datatype
#'
#' @keywords DataFrame
#' @examples
#' field = pl$Field("Cities", pl$String)
#' field$datatype
#'
#' field$datatype = pl$Categorical #<- is fine too
#' field$datatype
RField_datatype = method_as_property(function() {
.pr$RField$get_datatype(self)
}, setter = TRUE)

RField.property_setters$datatype = function(self, value) {
.pr$RField$set_datatype_mut(self, value)
}


#' @export
"$<-.RPolarsRField" = function(self, name, value) {
name = sub("<-$", "", name)
Expand Down
2 changes: 0 additions & 2 deletions R/PTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ time_unit_conv_factor = c(

#' Store Time in R
#'
#' @include after-wrappers.R
#'
#' @param x an integer or double vector of n epochs since midnight OR a char vector of char times
#' passed to as.POSIXct converted to seconds.
#' @param tu timeunit either "s","ms","us","ns"
Expand Down
6 changes: 2 additions & 4 deletions R/after-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## 2. ADD INTERNAL PROFILER, options(polars.debug_polars = TRUE)
## 3. ADD build_debug_print TO DEBUG CLASS CONSTRUCTION DURING PACKAGE BUILDTIME (rarely used)
## 4. ADD BETTER METHOD LOOKUP ERR MSGS macro_add_syntax_check_to_class(), HELPS END USER
## 5. ADD OPTION TO FLAG A METHOD TO BEHAVE LIKE A PROPERTY method_as_property()
## 5. ADD OPTION TO FLAG A METHOD TO BEHAVE LIKE A PROPERTY method_as_active_binding()


#' A dummy function to mark a function to replace extendr-wrapper function
Expand Down Expand Up @@ -69,7 +69,6 @@ extendr_method_to_pure_functions = function(env, class_name = NULL) {
}


#' @include extendr-wrappers.R
#' @title polars-API: private calls to rust-polars
#' @description `.pr`
#' Original extendr bindings converted into pure functions
Expand Down Expand Up @@ -124,7 +123,6 @@ extendr_method_to_pure_functions = function(env, class_name = NULL) {
##### ----- MACROS used at package build time

#' @title add syntax verification to a class
#' @include utils.R
#' @param Class_name string name of env class
#' @rdname macro_add_syntax_check_to
#' @noRd
Expand Down Expand Up @@ -182,7 +180,7 @@ if (build_debug_print) cat("\n")
#' @param f a function
#' @param setter bool, if true a property method can be modified by user
#' @return function subclassed into c("property","function") or c("setter","property","function")
method_as_property = function(f, setter = FALSE) {
method_as_active_binding = function(f, setter = FALSE) {
class(f) = if (setter) {
c("setter", "property", "function")
} else {
Expand Down
Loading

0 comments on commit 41e3512

Please sign in to comment.