Skip to contents

check_dataframe_or_tibble() and check_is_dataframe_or_tibble() functions validate whether the input is a dataframe or tibble. Optionally, they check the dimensions (number of rows and columns). The function check_dataframe_or_tibble() raises an error if the input fails any of the checks, while check_is_dataframe_or_tibble() returns TRUE if all conditions are met, and FALSE otherwise.

Usage

check_dataframe_or_tibble(dat, nrows = NULL, ncols = NULL, par_name = "dat")

check_is_dataframe_or_tibble(dat, nrows = NULL, ncols = NULL)

Arguments

dat

The object to check.

nrows

An optional integer specifying the expected number of rows. Default is NULL.

ncols

An optional integer specifying the expected number of columns. Default is NULL.

par_name

An optional name of the parameter to display in error messages.

Value

  • check_dataframe_or_tibble(): Returns original value entered or raises an error message if an error occurs or any of the conditions is not met.

  • check_is_dataframe_or_tibble(): Logical value, TRUE if the input is a dataframe or tibble and meets the dimension requirements, and FALSE otherwise.

Examples

# Examples for check_dataframe_or_tibble()
# ----------------------------------------
library(tibble)
df <- data.frame(a = 1:3, b = 4:6)
print(df)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6
check_dataframe_or_tibble(df)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6

L <- list(a = 1:3, b = 4:6)
print(L)
#> $a
#> [1] 1 2 3
#> 
#> $b
#> [1] 4 5 6
#> 
try(check_dataframe_or_tibble(L))
#> Error in check_dataframe_or_tibble(L) : 
#>   Expected 'dat' to be a DataFrame or tibble but got list

tbl <- tibble::tibble(a = 1:3, b = 4:6)
print(tbl)
#> # A tibble: 3 × 2
#>       a     b
#>   <int> <int>
#> 1     1     4
#> 2     2     5
#> 3     3     6
check_dataframe_or_tibble(tbl)
#> # A tibble: 3 × 2
#>       a     b
#>   <int> <int>
#> 1     1     4
#> 2     2     5
#> 3     3     6

A <- matrix(1:6, nrow = 2, ncol = 3)
print(A)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6
try(check_dataframe_or_tibble(A))
#> Error in check_dataframe_or_tibble(A) : 
#>   Expected 'dat' to be a DataFrame or tibble but got matrix array

# Examples for check_is_dataframe_or_tibble()
# -------------------------------------------

check_is_dataframe_or_tibble(df)
#> [1] TRUE

check_is_dataframe_or_tibble(L)
#> [1] FALSE

check_is_dataframe_or_tibble(tbl)
#> [1] TRUE

check_is_dataframe_or_tibble(A)
#> [1] FALSE