diff --git a/R/tidyr-verbs.R b/R/tidyr-verbs.R index 1f7227b0..1c665e5d 100644 --- a/R/tidyr-verbs.R +++ b/R/tidyr-verbs.R @@ -167,13 +167,12 @@ unnest_check_tsibble <- function(data, key, index) { data } +#' Unnest a data frame consisting of tsibbles to a tsibble +#' #' @param data A data frame contains homogenous tsibbles in the list-columns. #' @param cols Names of columns to unnest. #' @inheritParams as_tsibble #' @keywords internal -#' @examples -#' nested_stock %>% -#' unnest_tsibble(cols = data, key = stock) #' @export unnest_tsibble <- function(data, cols, key = NULL, validate = TRUE) { if (missing(cols)) { @@ -195,13 +194,13 @@ unnest_tsibble <- function(data, cols, key = NULL, validate = TRUE) { new_lst <- unnested_names[map_lgl(unnested_data, is_list)] old_lst <- data_names[map_lgl(data, is_list)] lst_cols <- setdiff(old_lst, new_lst) - # checking if the nested columns has `tbl_ts` class (only for the first row) - first_nested <- data[lst_cols][1L, ] - eval_col <- purrr::imap(first_nested, dplyr::first) - tsbl_col <- map_lgl(eval_col, is_tsibble) + # checking if the nested columns has `tbl_ts` class + tsbl_col <- map_lgl(data[lst_cols], validate_list_of_tsibble) if (sum(tsbl_col) == 0) { - abort("Unnested columns contain no tsibble object.") + abort("Unnested columns contain no tsibble columns.") } + first_nested <- data[lst_cols][1L, ] + eval_col <- purrr::imap(first_nested, dplyr::first) tsbl <- eval_col[tsbl_col][[1L]] idx <- index(tsbl) diff --git a/man/unnest_tsibble.Rd b/man/unnest_tsibble.Rd new file mode 100644 index 00000000..67a07e35 --- /dev/null +++ b/man/unnest_tsibble.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidyr-verbs.R +\name{unnest_tsibble} +\alias{unnest_tsibble} +\title{Unnest a data frame consisting of tsibbles to a tsibble} +\usage{ +unnest_tsibble(data, cols, key = NULL, validate = TRUE) +} +\arguments{ +\item{data}{A data frame contains homogenous tsibbles in the list-columns.} + +\item{cols}{Names of columns to unnest.} + +\item{key}{Unquoted variable(s) that uniquely determine time indices. \code{NULL} for +empty key, and works with tidy selector (e.g. \code{\link[dplyr:starts_with]{dplyr::starts_with()}}).} + +\item{validate}{\code{TRUE} suggests to verify that each key or each combination +of key variables leads to unique time indices (i.e. a valid tsibble). If you +are sure that it's a valid input, specify \code{FALSE} to skip the checks.} +} +\description{ +Unnest a data frame consisting of tsibbles to a tsibble +} +\keyword{internal} diff --git a/tests/testthat/test-tidyr.R b/tests/testthat/test-tidyr.R index 475abe4c..ec223ad1 100644 --- a/tests/testthat/test-tidyr.R +++ b/tests/testthat/test-tidyr.R @@ -196,7 +196,7 @@ test_that("dplyr verbs for lst_ts", { # ) expect_error( unnest_tsibble(nest_t %>% mutate(data = 1), cols = data), - "contain no tsibble object." + "contain no tsibble columns." ) expect_is(nest_t %>% select(data2 = data), "lst_ts") expect_is(nest_t %>% group_by(State), "grouped_df")