diff --git a/DESCRIPTION b/DESCRIPTION index c7f71fc..9c3a709 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,9 +11,8 @@ LazyData: true URL: https://github.com/r-lib/rematch2#readme BugReports: https://github.com/r-lib/rematch2/issues RoxygenNote: 7.1.0 -Imports: - tibble Suggests: covr, - testthat + testthat, + tibble Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index f97b6d0..0c2479f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,5 +10,3 @@ export(re_exec) export(re_exec_all) export(re_match) export(re_match_all) -importFrom(tibble,new_tibble) -importFrom(tibble,tibble) diff --git a/R/all.R b/R/all.R index 5a37ad1..45823d5 100644 --- a/R/all.R +++ b/R/all.R @@ -69,12 +69,13 @@ re_match_all <- function(text, pattern, perl=TRUE, ...) { res, names = c(attr(match[[1]], "capture.names"), ".match"), row.names = seq_along(text), - class = c("tbl_df", "tbl", "data.frame") + class = "data.frame" ) res$.text <- text nc <- ncol(res) - res[, c(seq_len(nc - 2), nc, nc - 1)] + res <- res[, c(seq_len(nc - 2), nc, nc - 1)] + as_tibble(res) } match1 <- function(text1, match1) { diff --git a/R/bind_re_match.R b/R/bind_re_match.R index 1c8f269..fa42401 100644 --- a/R/bind_re_match.R +++ b/R/bind_re_match.R @@ -2,7 +2,8 @@ #' #' Taking a data frame and a column name as input, this function will run #' \code{\link{re_match}} and bind the results as new columns to the original -#' table., returning a \code{\link[tibble]{tibble}}. This makes it friendly for +#' table., returning a \code{\link[tibble]{tibble}} if the tibble package is +#' installed, a \code{data.frame} otherwise. This makes it friendly for #' pipe-oriented programming with \link[magrittr]{magrittr}. #' #' @note If named capture groups will result in multiple columns with the same @@ -23,7 +24,8 @@ #' suitable for programming. #' #' @examples -#' match_cars <- tibble::rownames_to_column(mtcars) +#' match_cars <- mtcars +#' match_cars$rowname <- rownames(mtcars) #' bind_re_match(match_cars, rowname, "^(?\\w+) ?(?.+)?$") #' #' @export @@ -36,7 +38,9 @@ bind_re_match <- function(df, from, ..., keep_match = FALSE) { bind_re_match_ <- function(df, from, ..., keep_match = FALSE) { stopifnot(is.data.frame(df)) - if (!tibble::has_name(df, from)) + stopifnot(is_installed("tibble")) + + if (!from %in% names(df)) stop(from, " is not present in the data frame.") res <- re_match(text = df[[from]], ...) diff --git a/R/exec.R b/R/exec.R index 06aa8af..e646727 100644 --- a/R/exec.R +++ b/R/exec.R @@ -81,10 +81,11 @@ re_exec <- function(text, pattern, perl=TRUE, ...) { }) ) - res <- new_tibble( + res <- structure( list(text, matchlist), names = c(".text", ".match"), - nrow = length(text) + row.names = seq_along(text), + class = "data.frame" ) if (!is.null(attr(match, "capture.start"))) { @@ -113,10 +114,11 @@ re_exec <- function(text, pattern, perl=TRUE, ...) { } ) - res <- new_tibble( + res <- structure( c(grouplists, res), names = c(attr(match, "capture.names"), ".text", ".match"), - nrow = length(res[[1]]) + row.names = seq_along(text), + class = "data.frame" ) } diff --git a/R/package.R b/R/package.R index 55bc558..495504d 100644 --- a/R/package.R +++ b/R/package.R @@ -5,7 +5,6 @@ #' groups from the match of a regular expression to a character vector. #' See \code{\link{re_match}}. #' -#' @importFrom tibble tibble new_tibble "_PACKAGE" #' Extract Regular Expression Matches Into a Data Frame @@ -78,6 +77,6 @@ re_match <- function(text, pattern, perl = TRUE, ...) { } names(res) <- c(attr(match, "capture.names"), ".text", ".match") - class(res) <- c("tbl_df", "tbl", class(res)) - res + + as_tibble(res) } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..3cd7d23 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,11 @@ +as_tibble <- function(x) { + if (is_installed("tibble")) { + tibble::new_tibble(x, nrow = NROW(x)) + } else { + x + } +} + +is_installed <- function(pkg) { + isTRUE(requireNamespace(pkg, quietly = TRUE)) +} diff --git a/man/bind_re_match.Rd b/man/bind_re_match.Rd index ea75fd3..2a45ce8 100644 --- a/man/bind_re_match.Rd +++ b/man/bind_re_match.Rd @@ -26,7 +26,8 @@ Defaults to \code{FALSE}, to avoid column name collisions in the case that \description{ Taking a data frame and a column name as input, this function will run \code{\link{re_match}} and bind the results as new columns to the original -table., returning a \code{\link[tibble]{tibble}}. This makes it friendly for +table., returning a \code{\link[tibble]{tibble}} if the tibble package is +installed, a \code{data.frame} otherwise. This makes it friendly for pipe-oriented programming with \link[magrittr]{magrittr}. } \section{Functions}{ @@ -40,7 +41,8 @@ If named capture groups will result in multiple columns with the same resulting table. } \examples{ -match_cars <- tibble::rownames_to_column(mtcars) +match_cars <- mtcars +match_cars$rowname <- rownames(mtcars) bind_re_match(match_cars, rowname, "^(?\\\\w+) ?(?.+)?$") }