Here's a hack to create an empty data frame with no rows and no columns:
iris[FALSE, FALSE]
#> data frame with 0 columns and 0 rows
Smarter-looking code creates a spurious column:
x <- list(NULL)
class(x) <- c("data.frame")
attr(x, "row.names") <- integer(0)
str(x)
#> 'data.frame': 0 obs. of 1 variable:
#> $ : NULL
Is there a non-hack alternative?
The reason to create such a thing is to satisfy a function that can handle empty data frames but not NULLs.
This is different from similar questions because it is about having no columns as well as no rows.
structure(list(),class="data.frame")would be a way to go your original method of trying to add a class to a list.inherits(x, "data.frame")? which will pass for adata.frame(empty or not) but will fail forNULL. If they're trying to pass data into an existing function, thendata.frame()should bypass the test (which could very likely be the above one anyway).unnestintidyrpackage ;)