to_df: Ensure a data frame argument meets expectations

View source: R/to_df.R

to_dfR Documentation

Ensure a data frame argument meets expectations

Description

to_df() checks whether an argument can be coerced to a data frame, returning it silently if so. Otherwise an informative error message is signaled. to_data_frame() is a synonym of to_df().

Usage

to_df(
  x,
  ...,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

to_data_frame(
  x,
  ...,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

## S3 method for class ''NULL''
to_df(x, ..., allow_null = TRUE, x_arg = caller_arg(x), call = caller_env())

Arguments

x

The argument to stabilize.

...

Arguments passed to base::as.data.frame() or other methods.

x_arg

⁠(length-1 character)⁠ The name of the argument being stabilized to use in error messages. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.

call

(environment) The execution environment to mention as the source of error messages.

x_class

⁠(length-1 character)⁠ The class name of the argument being stabilized to use in error messages. Use this if you remove a special class from the object before checking its coercion, but want the error message to match the original class.

allow_null

⁠(length-1 logical)⁠ Is NULL an acceptable value?

Value

The argument as a data frame.

See Also

Other data frame functions: specify_df(), stabilize_df()

Examples

to_df(mtcars)
to_df(list(name = "Alice", age = 30L))
to_df(NULL)
try(to_df(NULL, allow_null = FALSE))
try(to_df(c("a", "b", "c")))
to_df(letters)

stbl documentation built on April 4, 2026, 5:06 p.m.