Learn R Programming

chronicler (version 0.3.0)

as.data.frame.chronicle: Coerce a chronicle object to a data.frame

Description

This is an S3 method that allows for the easy coercion of a chronicle object into a standard data.frame. It automatically unwraps the object by calling unveil(.c, "value") and then attempts to convert the result into a data frame.

Usage

# S3 method for chronicle
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

Value

A data.frame if the value inside the chronicle object is a data.frame or can be successfully coerced into one.

Arguments

x

A chronicle object.

row.names

NULL or a character vector giving the row names for the data frame.

optional

logical. If TRUE, setting row names and converting column names is optional.

...

Additional arguments to be passed to or from other methods.

Details

The function will produce an error if the underlying value of the chronicle object cannot be coerced to a data frame by the base as.data.frame() method. The error message will be specific, indicating the class of the object that caused the failure.

Examples

Run this code
library(dplyr)

# --- Successful Example ---

# Create a chronicle object whose value is a data frame
starwars_chronicle <- starwars %>%
  record(filter)(species == "Human") %>%
  bind_record(record(select), name, height, mass)

# Now, you can use as.data.frame() directly on the chronicle object
sw_df <- as.data.frame(starwars_chronicle)

class(sw_df)
head(sw_df)


# --- Error Example ---

# Create a chronicle object whose value is a number
numeric_chronicle <- record(sqrt)(100)

# This will fail with a specific error message because a number
# cannot be turned into a data frame.
try(as.data.frame(numeric_chronicle))

Run the code above in your browser using DataLab