The `PivotFilters` class allows multiple filter conditions relating to
different data frame columns to be combined, i.e. a `PivotFilters` object
typically contains multiple PivotFilter
objects.
R6Class
object.
As well as acting as a container for multiple filter conditions, the
`PivotFilters` class also contains logic for combining filter.
The `action` parameter in many of the methods controls how two filters
are combined.
Most common cases:
(1) When working out the rowColFilters for each pivot table cell, the
filters from the row and column leaf groups are combined using
`action="intersect"`.
(2) When combining the rowColFilters with calculation filters the
action could be any of (in order of most typical)
"intersect", "replace" or "union".
"intersect" would apply additional restrictions, e.g. see the
example in the Calculations vignette that has a measure for
weekend trains only.
"replace" would apply when doing things like percentage of row
total calculations - again, see example in the calculations vignette
"union" is probably much less likely (hard to envisage many
situations when that would be needed).
(3) In custom calculation functions, the action could be any of
"intersect", "replace" or "union".
NOTE: `pivottabler` does not allow complex conditions to be built up,
such as ((A=X) or (B=Y)) and (C=2) since there is complex precedence
involved and conditions like this are not typical of pivot tables.
If they were really needed, a workaround would be to use a custom
calculation function and include this logic in that function.
See Appendix 2 vignette for many more complex calculation details.
count
The number of `PivotFilter` objects in this `PivotFilters` object.
filters
A list of `PivotFilter` objects in this `PivotFilters` object.
isALL
If TRUE, this `PivotFilters` object matches all data.
isNONE
If TRUE, this `PivotFilters` object matches no data.
filteredVariables
The names of the variables that are filtered by this `PivotFilters` object.
filteredValues
A list of the criteria values for each of the variables filtered by this `PivotFilters` object, where the list element names are the variable names.
new()
Create a new `PivotFilters` object, optionally adding a filter.
PivotFilters$new(
parentPivot = NULL,
variableName = NULL,
type = "ALL",
values = NULL
)
parentPivot
The pivot table that this `PivotFilters` instance belongs to.
variableName
The name of the column in the data frame that this filter applies to. Specify `NULL` to skip adding a filter.
type
Must be either "ALL", "VALUES" or "NONE". "VALUES" is the most common type and means the data is filtered to a subset of values. "ALL" means there is no filtering, i.e. all values match. "NONE" means there can be no matching values/data.
values
A single data value or a vector of multiple data values that the filter will match on.
keepOnlyFiltersFor()
Remove the filters for all variables except those specified.
removeFiltersFor()
Remove the filters for the specified variables.
isFilterMatch()
Tests whether this `PivotFilters` object matches specified criteria.
PivotFilters$isFilterMatch(
matchMode = "simple",
variableNames = NULL,
variableValues = NULL
)
matchMode
Either "simple" (default) or "combinations".
"simple" is used when matching only one variable-value, multiple
variable-value combinations are effectively logical "OR", i.e.
any one single `PivotFilter` match means the `PivotFilters` object
is a match.
"combinations" is used when matching for combinations of variable
values, multiple variable-value combinations are effectively
logical "AND", i.e. there must be a matching `PivotFilter` for
every variable name / variable values criteria specified.
See the "Finding and Formatting" vignette for graphical examples.
variableNames
The variable name(s) to find a filter for. This can be a vector containing more than one variable name.
variableValues
A list specifying the variable names and values to find, e.g. `variableValues=list("PowerType"=c("DMU", "HST"))`.
setFilters()
Update the value of this `PivotFilters` object with the filters from the specified `PivotFilters` object, either intersecting, replacing or unioning the filter criteria.
setFilter()
Update the value of this `PivotFilters` object with the filters from the specified `PivotFilter` object, either intersecting, replacing or unioning the filter criteria.
setFilterValues()
Update the value of this `PivotFilters` object with additional filter criteria, either intersecting, replacing or unioning the filter criteria.
PivotFilters$setFilterValues(
variableName = NULL,
type = "ALL",
values = NULL,
action = "replace"
)
variableName
The name of the column in the data frame that this criteria applies to.
type
Must be either "ALL", "VALUES" or "NONE".
values
A single data value or a vector of multiple data values that comprise the additional filter criteria.
action
Specifies how the criteria defined in `filter` should be combined with the existing filter criteria. Must be one of "intersect", "replace" or "union".
addFilter()
Add a new `PivotFilter` object to the filter list in this `PivotFilters` object.
getFilteredDataFrame()
Filters the specified data frame using the filters defined in this `PivotFilters` object and returns the results as another data frame.
asString()
Return a representation of this object as a character value.
pt <- PivotTable$new()
# ...
# PivotFilters constructor allows a filter to be defined
filters <- PivotFilters$new(pt, variableName="Year", values=2017)
# Create a new filter
filter <- PivotFilter$new(pt, variableName="Country", values="England")
# Combine the filters
filters$setFilter(filter)
# filters now contains criteria for both Year and Country
# Now add another filter, this time via an alternative method
filters$setFilterValues(variableName="Product", values="Cadbury Dairy Milk
Chocolate 100g")
# filters now contains criteria for Year, Country and Product