TheFallOfTroy: Hiding S4 objects as S3 vectors

Description Usage Arguments Details Value Functions Examples

Description

There are some limitations to getting S4 objects past the internals of the ggplot2 system. One of these is a hard check with rlang::is_vector on data.frame columns, which will throw an error when the column is an S4 object. To work around these limitations, we disguise S4 Vectors classes as S3 vectors from the vctrs package.

Usage

1
2
3
4
5
6
7
8
GreekSoldier(x)

Nightfall(x, na.rm = FALSE)

## S4 method for signature 'OakHorse'
Nightfall(x, na.rm = FALSE)

HelenOfTroy(x, what = NULL)

Arguments

x

For GreekSoldier(), an S4 Vector class object.
For Nightfall() and HelenOfTroy(), an S3 WoodenHorse class object.

na.rm

For Nightfall(), should the result be returned for non-NA entries?

what

For HelenaOfTroy, an optional class name to check for inheritance.

Details

Calling GreekSoldier() on an S4 Vector object will generate an object of the class WoodenHorse of the same length as the input, where the original S4 Vector is inside the WoodenHorse object as an attribute.

Encoding the WoodenHorse object as a vctrs_vctr object allows us to pass the rlang::is_vector check and gives us nice options to preserve attributes over subsetting and concatenation operations.

Value

For Nightfall(), a Vector object. For GreekSoldier(), a WoodenHorse object. For HelenaOfTroy(), a character,

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Making an S4 object
Anticlus <- Rle(1:10, 10:1)

# Rle object does not pass is_vector test
rlang::is_vector(Anticlus)

# Disguising the object as S3 vector
VictoryTrophy <- GreekSoldier(Anticlus)
class(VictoryTrophy) # A WoodenHorse

# WoodenHorse passes is_vector test
rlang::is_vector(VictoryTrophy)

# Inspecting the class of WoodenHorse
HelenOfTroy(VictoryTrophy) # Rle

# Restoring WoodenHorse to S4 object
Nightfall(VictoryTrophy)

teunbrand/ggnomics documentation built on Aug. 2, 2020, 7:34 p.m.